summaryrefslogtreecommitdiff
path: root/xmloff/source
diff options
context:
space:
mode:
authorTor Lillqvist <tml@collabora.com>2013-09-30 15:23:35 +0300
committerTor Lillqvist <tml@collabora.com>2013-09-30 17:40:53 +0300
commitc1015fdd51909495cefdc0258c3899cd73d4d2df (patch)
tree901f0fb94a93dbd614c18d3320bcfa414672e441 /xmloff/source
parent32e0f451d69b68d69a4aa91c271b81ea6264d3c1 (diff)
Add hack to optionally get stable ODF output from the same input
To be used in regression testing and similar scenarios, where the output ODF is *not* intended to be further manipulated in LibreOffice. An environment variable LIBO_ONEWAY_STABLE_ODF_EXPORT is used to toggle this behaviour. I am not 100% sure whether the generated ODF with the hack toggled on is even fully correct, but correctness is not the purpose of the hack anyway. Two classes of issues handled: 1) Automatic style names and 2) use of randomness. For class 1), when the hack toggle is in effect, we generate the names at first as strings based on all the properties of the style, and sort them based on those, and then rename them (for brevity in the output) to the "normal" form of a short prefix plus a number (like "P12"). Sure, it would have been better to just figure out *why* the automatic style naming currently is not stable in the first place, but outputs the styles in different order (with some styles being assigned different numbers) in separate invokations of LibreOffice), but I was unable to understand that. Possibly this code could be used in all cases, except that it does break some unit test (can't recall which right now). I don't know whether that is simply because the unit test assumes too much knowledge of the internal workings of the automatic style name generation, or whether the generated ODF is actually invalid. For 2), I found a handful of places where randomness was used to generated various kinds of identifiers in ODF output. I changed those to just use large (64-bit) non-overlapping integers instead. I assume there *is* a point in the original code in each case that explains why randomness is needed, so the hack definitely needs to be optional and used only for the above mentioned scenarios. Change-Id: I17b657197e38bcf24abdfe61ad4a277f4339eeae
Diffstat (limited to 'xmloff/source')
-rw-r--r--xmloff/source/style/impastpl.cxx230
-rw-r--r--xmloff/source/style/impastpl.hxx4
-rw-r--r--xmloff/source/text/txtlists.cxx24
3 files changed, 230 insertions, 28 deletions
diff --git a/xmloff/source/style/impastpl.cxx b/xmloff/source/style/impastpl.cxx
index 9b54e94db591..8aca0b1b3f1d 100644
--- a/xmloff/source/style/impastpl.cxx
+++ b/xmloff/source/style/impastpl.cxx
@@ -60,24 +60,190 @@ void XMLAutoStyleFamily::ClearEntries()
maParentSet.clear();
}
+static OUString
+data2string(void *data,
+ const typelib_TypeDescriptionReference *type);
+
+static OUString
+struct2string(void *data,
+ const typelib_TypeDescription *type)
+{
+ assert(type->eTypeClass == typelib_TypeClass_STRUCT);
+
+ OUStringBuffer result;
+
+ result.append("{");
+
+ const typelib_CompoundTypeDescription *compoundType =
+ &((const typelib_StructTypeDescription*) type)->aBase;
+
+ for (int i = 0; i < compoundType->nMembers; i++)
+ {
+ if (i > 0)
+ result.append(":");
+ result.append(compoundType->ppMemberNames[i]);
+ result.append("=");
+ result.append(data2string(((char *)data)+compoundType->pMemberOffsets[i],
+ compoundType->ppTypeRefs[i]));
+ }
+
+ result.append("}");
+
+ return result.makeStringAndClear();
+}
+
+static OUString
+data2string(void *data,
+ const typelib_TypeDescriptionReference *type)
+{
+ OUStringBuffer result;
+
+ switch (type->eTypeClass)
+ {
+ case typelib_TypeClass_VOID:
+ break;
+ case typelib_TypeClass_BOOLEAN:
+ result.append((*reinterpret_cast<const sal_Bool*>(data) == sal_False ) ? OUString("false") : OUString("true"));
+ break;
+ case typelib_TypeClass_BYTE:
+ result.append(OUString::number((*reinterpret_cast<const sal_Int8*>(data))));
+ break;
+ case typelib_TypeClass_SHORT:
+ result.append(OUString::number((*reinterpret_cast<const sal_Int16*>(data))));
+ break;
+ case typelib_TypeClass_LONG:
+ result.append(OUString::number((*reinterpret_cast<const sal_Int32*>(data))));
+ break;
+ case typelib_TypeClass_HYPER:
+ result.append(OUString::number((*reinterpret_cast<const sal_Int64*>(data))));
+ break;
+ case typelib_TypeClass_UNSIGNED_SHORT:
+ result.append(OUString::number((*reinterpret_cast<const sal_uInt16*>(data))));
+ break;
+ case typelib_TypeClass_UNSIGNED_LONG:
+ result.append(OUString::number((*reinterpret_cast<const sal_uInt32*>(data)), 16));
+ break;
+ case typelib_TypeClass_UNSIGNED_HYPER:
+ result.append(OUString::number((*reinterpret_cast<const sal_uInt64*>(data)), 16));
+ break;
+ case typelib_TypeClass_FLOAT:
+ result.append(OUString::number((*reinterpret_cast<const float*>(data))));
+ break;
+ case typelib_TypeClass_DOUBLE:
+ result.append(OUString::number((*reinterpret_cast<const double*>(data))));
+ break;
+ case typelib_TypeClass_CHAR:
+ result.append("U+");
+ result.append(OUString::number((*reinterpret_cast<const sal_uInt16*>(data))));
+ break;
+ case typelib_TypeClass_STRING:
+ result.append(*reinterpret_cast<OUString*>(data));
+ break;
+ case typelib_TypeClass_TYPE:
+ case typelib_TypeClass_SEQUENCE:
+ case typelib_TypeClass_EXCEPTION:
+ case typelib_TypeClass_INTERFACE:
+ result.append("wtf");
+ break;
+ case typelib_TypeClass_STRUCT:
+ result.append(struct2string(data, type->pType));
+ break;
+ case typelib_TypeClass_ENUM:
+ result.append(OUString::number((*reinterpret_cast<const sal_Int32*>(data))));
+ break;
+ default:
+ assert(false); // this cannot happen I hope
+ break;
+ }
+
+ return result.makeStringAndClear();
+}
+
+static OUString
+any2string(uno::Any any)
+{
+ return data2string((void*)any.getValue(), any.pType);
+}
+
// Class SvXMLAutoStylePoolProperties_Impl
// ctor class SvXMLAutoStylePoolProperties_Impl
-XMLAutoStylePoolProperties::XMLAutoStylePoolProperties( XMLAutoStyleFamily& rFamilyData, const vector< XMLPropertyState >& rProperties )
+XMLAutoStylePoolProperties::XMLAutoStylePoolProperties( XMLAutoStyleFamily& rFamilyData, const vector< XMLPropertyState >& rProperties, OUString& rParentName )
: maProperties( rProperties ),
mnPos ( rFamilyData.mnCount )
{
- // create a name that hasn't been used before. The created name has not
- // to be added to the array, because it will never tried again
- OUStringBuffer sBuffer( 7 );
- do
+ static bool bHack = (getenv("LIBO_ONEWAY_STABLE_ODF_EXPORT") != NULL);
+
+ if (bHack)
{
- rFamilyData.mnName++;
- sBuffer.append( rFamilyData.maStrPrefix );
- sBuffer.append( OUString::number( rFamilyData.mnName ) );
- msName = sBuffer.makeStringAndClear();
+ OUStringBuffer aStemBuffer(32);
+ aStemBuffer.append( rFamilyData.maStrPrefix );
+
+ if (rParentName != "")
+ {
+ aStemBuffer.append("-");
+ aStemBuffer.append(rParentName);
+ }
+
+ // Create a name based on the properties used
+ for( size_t i = 0, n = maProperties.size(); i < n; ++i )
+ {
+ XMLPropertyState& rState = maProperties[i];
+ if (rState.mnIndex == -1)
+ continue;
+ OUString sXMLName(rFamilyData.mxMapper->getPropertySetMapper()->GetEntryXMLName(rState.mnIndex));
+ if (sXMLName == "")
+ continue;
+ aStemBuffer.append("-");
+ aStemBuffer.append(OUString::number(rFamilyData.mxMapper->getPropertySetMapper()->GetEntryNameSpace(rState.mnIndex)));
+ aStemBuffer.append(":");
+ aStemBuffer.append(sXMLName);
+ aStemBuffer.append("=");
+ aStemBuffer.append(any2string(rState.maValue));
+ }
+
+#if 0
+ // Finally append an incremental counter in an attempt to make identical
+ // styles always come out in the same order. Will see if this works.
+ aStemBuffer.append("-z");
+ static sal_Int32 nCounter = 0;
+ aStemBuffer.append(OUString::number(nCounter++));
+#endif
+
+ // create a name that hasn't been used before. The created name has not
+ // to be added to the array, because it will never tried again
+ OUStringBuffer aTry( aStemBuffer );
+
+ msName = aTry.makeStringAndClear();
+ bool bWarned = false;
+ while (rFamilyData.maNameSet.find(msName) !=
+ rFamilyData.maNameSet.end())
+ {
+ if (!bWarned)
+ SAL_WARN("xmloff", "Overlapping style name for " << msName);
+ bWarned = true;
+ rFamilyData.mnName++;
+ aTry.append( aStemBuffer );
+ aTry.append( "-" );
+ aTry.append( OUString::number( rFamilyData.mnName ) );
+ msName = aTry.makeStringAndClear();
+ }
+ rFamilyData.maNameSet.insert(msName);
+ }
+ else
+ {
+ // create a name that hasn't been used before. The created name has not
+ // to be added to the array, because it will never tried again
+ OUStringBuffer sBuffer( 7 );
+ do
+ {
+ rFamilyData.mnName++;
+ sBuffer.append( rFamilyData.maStrPrefix );
+ sBuffer.append( OUString::number( rFamilyData.mnName ) );
+ msName = sBuffer.makeStringAndClear();
+ }
+ while (rFamilyData.maNameSet.find(msName) != rFamilyData.maNameSet.end());
}
- while (rFamilyData.maNameSet.find(msName) != rFamilyData.maNameSet.end());
}
bool operator<( const XMLAutoStyleFamily& r1, const XMLAutoStyleFamily& r2)
@@ -119,7 +285,7 @@ sal_Bool XMLAutoStylePoolParent::Add( XMLAutoStyleFamily& rFamilyData, const vec
if( !pProperties )
{
- pProperties = new XMLAutoStylePoolProperties( rFamilyData, rProperties );
+ pProperties = new XMLAutoStylePoolProperties( rFamilyData, rProperties, msParent );
PropertiesListType::iterator it = maPropertiesList.begin();
::std::advance( it, i );
maPropertiesList.insert( it, pProperties );
@@ -158,7 +324,7 @@ sal_Bool XMLAutoStylePoolParent::AddNamed( XMLAutoStyleFamily& rFamilyData, cons
if (rFamilyData.maNameSet.find(rName) == rFamilyData.maNameSet.end())
{
XMLAutoStylePoolProperties* pProperties =
- new XMLAutoStylePoolProperties( rFamilyData, rProperties );
+ new XMLAutoStylePoolProperties( rFamilyData, rProperties, msParent );
// ignore the generated name
pProperties->SetName( rName );
PropertiesListType::iterator it = maPropertiesList.begin();
@@ -274,7 +440,10 @@ void SvXMLAutoStylePoolP_Impl::RegisterName( sal_Int32 nFamily, const OUString&
DBG_ASSERT( aFind != maFamilySet.end(),
"SvXMLAutoStylePool_Impl::RegisterName: unknown family" );
if (aFind != maFamilySet.end())
+ {
+ // SAL_DEBUG("SvXMLAutoStylePoolP_Impl::RegisterName: " << nFamily << ", '" << rName << "'");
aFind->maNameSet.insert(rName);
+ }
}
//
@@ -418,7 +587,7 @@ namespace {
struct AutoStylePoolExport
{
const OUString* mpParent;
- const XMLAutoStylePoolProperties* mpProperties;
+ XMLAutoStylePoolProperties* mpProperties;
AutoStylePoolExport() : mpParent(NULL), mpProperties(NULL) {}
};
@@ -450,21 +619,20 @@ void SvXMLAutoStylePoolP_Impl::exportXML(
// which contains a parent-name and a SvXMLAutoStylePoolProperties_Impl
std::vector<AutoStylePoolExport> aExpStyles(nCount);
- sal_uInt32 i;
- for( i=0; i < nCount; i++ )
+ for( size_t i=0; i < nCount; i++ )
{
aExpStyles[i].mpParent = 0;
aExpStyles[i].mpProperties = 0;
}
- XMLAutoStyleFamily::ParentSetType::const_iterator it = rFamily.maParentSet.begin(), itEnd = rFamily.maParentSet.end();
+ XMLAutoStyleFamily::ParentSetType::iterator it = rFamily.maParentSet.begin(), itEnd = rFamily.maParentSet.end();
for (; it != itEnd; ++it)
{
- const XMLAutoStylePoolParent& rParent = *it;
+ XMLAutoStylePoolParent& rParent = *it;
size_t nProperties = rParent.GetPropertiesList().size();
for( size_t j = 0; j < nProperties; j++ )
{
- const XMLAutoStylePoolProperties* pProperties =
+ XMLAutoStylePoolProperties* pProperties =
&rParent.GetPropertiesList()[j];
sal_uLong nPos = pProperties->GetPos();
DBG_ASSERT( nPos < nCount,
@@ -479,12 +647,36 @@ void SvXMLAutoStylePoolP_Impl::exportXML(
}
}
+ static bool bHack = (getenv("LIBO_ONEWAY_STABLE_ODF_EXPORT") != NULL);
+
+ if (bHack)
+ {
+ struct {
+ bool operator() (AutoStylePoolExport a, AutoStylePoolExport b)
+ {
+ return (a.mpProperties->GetName() < b.mpProperties->GetName() ||
+ (a.mpProperties->GetName() == b.mpProperties->GetName() && *a.mpParent < *b.mpParent));
+ }
+ } aComparator;
+
+ std::sort(aExpStyles.begin(), aExpStyles.end(), aComparator);
+
+ for (size_t i = 0; i < nCount; i++)
+ {
+ OUString oldName = aExpStyles[i].mpProperties->GetName();
+ sal_Int32 dashIx = oldName.indexOf('-');
+ OUString newName = (dashIx > 0 ? oldName.copy(0, dashIx) : oldName) + OUString::number(i);
+ // SAL_DEBUG("renaming '" << oldName << "' -> '" << newName << "'");
+ aExpStyles[i].mpProperties->SetName(newName);
+ }
+ }
+
//
// create string to export for each XML-style. That means for each property-list
//
OUString aStrFamilyName = rFamily.maStrFamilyName;
- for( i=0; i<nCount; i++ )
+ for( size_t i = 0; i < nCount; i++ )
{
DBG_ASSERT( aExpStyles[i].mpProperties,
"SvXMLAutoStylePool_Impl::exportXML: empty position" );
diff --git a/xmloff/source/style/impastpl.hxx b/xmloff/source/style/impastpl.hxx
index f8e2e28baea2..409ac0cdb6a5 100644
--- a/xmloff/source/style/impastpl.hxx
+++ b/xmloff/source/style/impastpl.hxx
@@ -80,7 +80,7 @@ class XMLAutoStylePoolProperties
public:
- XMLAutoStylePoolProperties( XMLAutoStyleFamily& rFamilyData, const ::std::vector< XMLPropertyState >& rProperties );
+ XMLAutoStylePoolProperties( XMLAutoStyleFamily& rFamilyData, const ::std::vector< XMLPropertyState >& rProperties, OUString& rParentname );
~XMLAutoStylePoolProperties()
{
@@ -120,7 +120,7 @@ public:
const OUString& GetParent() const { return msParent; }
- const PropertiesListType& GetPropertiesList() const
+ PropertiesListType& GetPropertiesList()
{
return maPropertiesList;
}
diff --git a/xmloff/source/text/txtlists.cxx b/xmloff/source/text/txtlists.cxx
index 1c35a54fcfaa..853286ff864c 100644
--- a/xmloff/source/text/txtlists.cxx
+++ b/xmloff/source/text/txtlists.cxx
@@ -228,13 +228,23 @@ const OUString& XMLTextListsHelper::GetListStyleOfLastProcessedList() const
OUString XMLTextListsHelper::GenerateNewListId() const
{
- // Value of xml:id in element <text:list> has to be a valid ID type (#i92478#)
- OUString sTmpStr( "list" );
- sal_Int64 n = Time( Time::SYSTEM ).GetTime();
- n += Date( Date::SYSTEM ).GetDate();
- n += rand();
- // Value of xml:id in element <text:list> has to be a valid ID type (#i92478#)
- sTmpStr += OUString::number( n );
+ static bool bHack = (getenv("LIBO_ONEWAY_STABLE_ODF_EXPORT") != NULL);
+ OUString sTmpStr( "list" );
+
+ if (bHack)
+ {
+ static sal_Int64 nIdCounter = SAL_CONST_INT64(5000000000);
+ sTmpStr += OUString::number(nIdCounter++);
+ }
+ else
+ {
+ // Value of xml:id in element <text:list> has to be a valid ID type (#i92478#)
+ sal_Int64 n = Time( Time::SYSTEM ).GetTime();
+ n += Date( Date::SYSTEM ).GetDate();
+ n += rand();
+ // Value of xml:id in element <text:list> has to be a valid ID type (#i92478#)
+ sTmpStr += OUString::number( n );
+ }
OUString sNewListId( sTmpStr );
if ( mpProcessedLists != 0 )