Java源码示例:com.helger.xml.serialize.write.XMLWriterSettings
示例1
/**
* Write the passed map to the passed output stream using the predefined XML
* layout.
*
* @param aMap
* The map to be written. May not be <code>null</code>.
* @param aOS
* The output stream to write to. The stream is closed independent of
* success or failure. May not be <code>null</code>.
* @return {@link ESuccess#SUCCESS} when everything went well,
* {@link ESuccess#FAILURE} otherwise.
*/
@Nonnull
public static ESuccess writeMap (@Nonnull final Map <String, String> aMap, @Nonnull @WillClose final OutputStream aOS)
{
ValueEnforcer.notNull (aMap, "Map");
ValueEnforcer.notNull (aOS, "OutputStream");
try
{
final IMicroDocument aDoc = createMapDocument (aMap);
return MicroWriter.writeToStream (aDoc, aOS, XMLWriterSettings.DEFAULT_XML_SETTINGS);
}
finally
{
StreamHelper.close (aOS);
}
}
示例2
/**
* Write the passed collection to the passed output stream using the
* predefined XML layout.
*
* @param aCollection
* The map to be written. May not be <code>null</code>.
* @param aOS
* The output stream to write to. The stream is closed independent of
* success or failure. May not be <code>null</code>.
* @return {@link ESuccess#SUCCESS} when everything went well,
* {@link ESuccess#FAILURE} otherwise.
*/
@Nonnull
public static ESuccess writeList (@Nonnull final Collection <String> aCollection,
@Nonnull @WillClose final OutputStream aOS)
{
ValueEnforcer.notNull (aCollection, "Collection");
ValueEnforcer.notNull (aOS, "OutputStream");
try
{
final IMicroDocument aDoc = createListDocument (aCollection);
return MicroWriter.writeToStream (aDoc, aOS, XMLWriterSettings.DEFAULT_XML_SETTINGS);
}
finally
{
StreamHelper.close (aOS);
}
}
示例3
private static void _testGetNodeAsXHTMLString (final IMicroNode aNode)
{
// try all permutations
final XMLWriterSettings aSettings = XMLWriterSettings.createForXHTML ();
for (int nCharSet = 0; nCharSet < 2; ++nCharSet)
{
aSettings.setCharset (nCharSet == 1 ? StandardCharsets.ISO_8859_1 : StandardCharsets.UTF_8);
for (final EXMLSerializeIndent eIndent : EXMLSerializeIndent.values ())
{
aSettings.setIndent (eIndent);
for (final EXMLSerializeDocType eDocType : EXMLSerializeDocType.values ())
{
aSettings.setSerializeDocType (eDocType);
assertNotNull (MicroWriter.getNodeAsString (aNode, aSettings));
}
}
}
}
示例4
private static void _testGetNodeAsXMLString (final IMicroNode aNode)
{
// try all permutations
final XMLWriterSettings aSettings = new XMLWriterSettings ();
for (int nCharSet = 0; nCharSet < 2; ++nCharSet)
{
aSettings.setCharset (nCharSet == 1 ? StandardCharsets.ISO_8859_1 : StandardCharsets.UTF_8);
for (final EXMLSerializeIndent eIndent : EXMLSerializeIndent.values ())
{
aSettings.setIndent (eIndent);
for (final EXMLSerializeDocType eDocType : EXMLSerializeDocType.values ())
{
aSettings.setSerializeDocType (eDocType);
assertNotNull (MicroWriter.getNodeAsString (aNode, aSettings));
}
}
}
}
示例5
@Test
@Ignore ("Takes too long and was already tested with JDK 1.8 runtime parser")
public void testSpecialCharactersXML10Text ()
{
final EXMLSerializeVersion eXMLSerializeVersion = EXMLSerializeVersion.XML_10;
final XMLWriterSettings aSettings = new XMLWriterSettings ().setSerializeVersion (eXMLSerializeVersion);
for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; ++i)
if (!XMLCharHelper.isInvalidXMLTextChar (eXMLSerializeVersion, (char) i))
{
final String sText = "abc" + (char) i + "def";
assertEquals (7, sText.length ());
final IMicroDocument aDoc = new MicroDocument ();
aDoc.appendElement ("root").appendText (sText);
final String sXML = MicroWriter.getNodeAsString (aDoc, aSettings);
final IMicroDocument aDoc2 = MicroReader.readMicroXML (sXML);
assertNotNull ("Failed to read with byte " + i + "\n" + sXML, aDoc2);
assertEquals ("Length for byte " + i, i == 0 ? 6 : 7, aDoc2.getDocumentElement ().getTextContent ().length ());
assertTrue ("Difference in byte 0x" + Integer.toHexString (i), aDoc.isEqualContent (aDoc2));
}
}
示例6
@Test
public void testOrderAttributes ()
{
XMLWriterSettings aSettings = new XMLWriterSettings ().setIndent (EXMLSerializeIndent.NONE)
.setUseDoubleQuotesForAttributes (false);
// default order
final IMicroElement e = new MicroElement ("a");
e.setAttribute ("c", "1");
e.setAttribute ("b", "2");
e.setAttribute ("a", "3");
assertEquals ("<a c='1' b='2' a='3' />", MicroWriter.getNodeAsString (e, aSettings));
// Lexicographic order
aSettings = aSettings.setOrderAttributesAndNamespaces (true);
assertEquals ("<a a='3' b='2' c='1' />", MicroWriter.getNodeAsString (e, aSettings));
}
示例7
@Test
public void testOrderNamespaces ()
{
XMLWriterSettings aSettings = new XMLWriterSettings ().setIndent (EXMLSerializeIndent.NONE)
.setUseDoubleQuotesForAttributes (false);
// default order
final IMicroElement e = new MicroElement ("urn:stringdefault", "a");
e.setAttribute ("urn:string3", "c", "1");
e.setAttribute ("urn:string2", "b", "2");
e.setAttribute ("urn:string1", "a", "3");
// Attributes are ordered automatically in DOM!
assertEquals ("<a xmlns='urn:stringdefault' xmlns:ns0='urn:string3' ns0:c='1' xmlns:ns1='urn:string2' ns1:b='2' xmlns:ns2='urn:string1' ns2:a='3' />",
MicroWriter.getNodeAsString (e, aSettings));
aSettings = aSettings.setOrderAttributesAndNamespaces (true);
assertEquals ("<a xmlns='urn:stringdefault' xmlns:ns0='urn:string3' xmlns:ns1='urn:string2' xmlns:ns2='urn:string1' ns2:a='3' ns1:b='2' ns0:c='1' />",
MicroWriter.getNodeAsString (e, aSettings));
}
示例8
private static void _testC14 (final String sSrc, final String sDst)
{
final IMicroDocument aDoc = MicroReader.readMicroXML (sSrc,
new SAXReaderSettings ().setEntityResolver ( (x,
y) -> "world.txt".equals (y) ? new StringSAXInputSource ("world")
: new StringSAXInputSource ("")));
assertNotNull (aDoc);
final MapBasedNamespaceContext aCtx = new MapBasedNamespaceContext ();
aCtx.addMapping ("a", "http://www.w3.org");
aCtx.addMapping ("b", "http://www.ietf.org");
final String sC14 = MicroWriter.getNodeAsString (aDoc,
XMLWriterSettings.createForCanonicalization ()
.setIndentationString (" ")
.setNamespaceContext (aCtx)
.setSerializeComments (EXMLSerializeComments.IGNORE));
assertEquals (sDst, sC14);
}
示例9
@Test
public void testSpecialXMLAttrs ()
{
final String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<root xml:lang=\"en\" xml:space=\"preserve\" xml:base=\"baseuri\" xml:id=\"4711\">" +
"Bla" +
"</root>";
final IMicroDocument aDoc = MicroReader.readMicroXML (s);
assertNotNull (aDoc);
final IMicroElement eRoot = aDoc.getDocumentElement ();
assertEquals ("en", eRoot.getAttributeValue (XMLConstants.XML_NS_URI, "lang"));
assertNull (eRoot.getAttributeValue ("lang"));
assertEquals ("preserve", eRoot.getAttributeValue (XMLConstants.XML_NS_URI, "space"));
assertEquals ("baseuri", eRoot.getAttributeValue (XMLConstants.XML_NS_URI, "base"));
assertEquals ("4711", eRoot.getAttributeValue (XMLConstants.XML_NS_URI, "id"));
// Ensure they are written as well
assertEquals (s, MicroWriter.getNodeAsString (aDoc, new XMLWriterSettings ().setIndent (EXMLSerializeIndent.NONE)));
}
示例10
@Test
public void testWriteWithNamespacePrefix () throws SchematronReadException
{
final IReadableResource aRes = SchematronTestHelper.getAllValidSchematronFiles ().getFirst ();
// Read existing Schematron
final PSSchema aSchema = new PSReader (aRes).readSchema ();
// Create the XML namespace context
final MapBasedNamespaceContext aNSCtx = PSWriterSettings.createNamespaceMapping (aSchema);
aNSCtx.removeMapping (XMLConstants.DEFAULT_NS_PREFIX);
aNSCtx.addMapping ("sch", CSchematron.NAMESPACE_SCHEMATRON);
// Create the PSWriter settings
final PSWriterSettings aPSWS = new PSWriterSettings ();
aPSWS.setXMLWriterSettings (new XMLWriterSettings ().setNamespaceContext (aNSCtx)
.setPutNamespaceContextPrefixesInRoot (true));
// Write the Schematron
new PSWriter (aPSWS).writeToFile (aSchema, new File ("target/test-with-nsprefix.xml"));
}
示例11
@Test
public void testIssue51 () throws SchematronException
{
final PSPreprocessor aPreprocessor = new PSPreprocessor (PSXPathQueryBinding.getInstance ()).setKeepTitles (true)
.setKeepDiagnostics (true);
final IReadableResource aRes = new FileSystemResource ("src/test/resources/issues/github51/test51.sch");
final IMicroDocument aDoc = SchematronHelper.getWithResolvedSchematronIncludes (aRes, true);
final PSReader aReader = new PSReader (aRes).setLenient (true);
final PSSchema aSchema = aReader.readSchemaFromXML (aDoc.getDocumentElement ());
final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema (aSchema);
assertNotNull (aPreprocessedSchema);
assertTrue (aPreprocessedSchema.isValid (new DoNothingPSErrorHandler ()));
final PSWriterSettings aPWS = new PSWriterSettings ();
aPWS.setXMLWriterSettings (new XMLWriterSettings ().setIndent (EXMLSerializeIndent.INDENT_AND_ALIGN)
.setPutNamespaceContextPrefixesInRoot (true)
.setNamespaceContext (PSWriterSettings.createNamespaceMapping (aPreprocessedSchema)));
LOGGER.info ("Preprocessed:\n" + new PSWriter (aPWS).getXMLString (aPreprocessedSchema));
}
示例12
public static void validateAndProduceSVRL (@Nonnull final File aSchematron, final File aXML) throws Exception
{
final PSSchema aSchema = new PSReader (new FileSystemResource (aSchematron)).readSchema ();
final PSPreprocessor aPreprocessor = new PSPreprocessor (PSXPathQueryBinding.getInstance ());
final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema (aSchema);
final String sSCH = new PSWriter (new PSWriterSettings ().setXMLWriterSettings (new XMLWriterSettings ())).getXMLString (aPreprocessedSchema);
if (false)
System.out.println (sSCH);
final SchematronResourceSCH aSCH = new SchematronResourceSCH (new ReadableResourceString (sSCH,
StandardCharsets.UTF_8));
// Perform validation
final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL (new FileSystemResource (aXML));
assertNotNull (aSVRL);
if (false)
System.out.println (new SVRLMarshaller ().getAsString (aSVRL));
}
示例13
/**
* @return The XML writer settings to be used based on this writer settings.
* Never <code>null</code>.
*/
@Nonnull
default IXMLWriterSettings getXMLWriterSettings ()
{
final XMLWriterSettings ret = new XMLWriterSettings ().setNamespaceContext (getNamespaceContext ())
.setIndent (isFormattedOutput () ? EXMLSerializeIndent.INDENT_AND_ALIGN
: EXMLSerializeIndent.NONE);
if (hasIndentString ())
ret.setIndentationString (getIndentString ());
if (hasCharset ())
ret.setCharset (getCharset ());
return ret.setNewLineMode (ENewLineMode.DEFAULT)
.setIncorrectCharacterHandling (EXMLIncorrectCharacterHandling.DO_NOT_WRITE_LOG_WARNING);
}
示例14
@Test
public void testXMLVersion ()
{
for (final EXMLSerializeVersion eVersion : EXMLSerializeVersion.values ())
{
final IMicroDocument aDoc = MicroReader.readMicroXML (TEST_XML);
final XMLWriterSettings aSettings = new XMLWriterSettings ();
aSettings.setSerializeVersion (eVersion);
final String sXML = MicroWriter.getNodeAsString (aDoc, aSettings);
assertNotNull (sXML);
assertTrue (sXML.contains ("version=\"" +
eVersion.getXMLVersionOrDefault (EXMLVersion.XML_10).getVersion () +
"\""));
}
}
示例15
@Test
@Ignore ("Takes too long and was already tested with JDK 1.8 runtime parser")
public void testSpecialCharactersXML10CDATA ()
{
final EXMLSerializeVersion eXMLSerializeVersion = EXMLSerializeVersion.XML_10;
final XMLWriterSettings aSettings = new XMLWriterSettings ().setSerializeVersion (eXMLSerializeVersion);
for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; ++i)
if (!XMLCharHelper.isInvalidXMLCDATAChar (eXMLSerializeVersion, (char) i))
{
final String sText = "abc" + (char) i + "def";
assertEquals (7, sText.length ());
final IMicroDocument aDoc = new MicroDocument ();
aDoc.appendElement ("root").appendCDATA (sText);
final String sXML = MicroWriter.getNodeAsString (aDoc, aSettings);
final IMicroDocument aDoc2 = MicroReader.readMicroXML (sXML);
assertNotNull ("Failed to read with byte " + i + "\n" + sXML, aDoc2);
assertEquals ("Length for byte " + i, i == 0 ? 6 : 7, aDoc2.getDocumentElement ().getTextContent ().length ());
// Difference between created "\r" and read "\n"
if (i != '\r')
if (!aDoc.isEqualContent (aDoc2))
{
final String sXML2 = MicroWriter.getNodeAsString (aDoc2, aSettings);
fail ("0x" + Integer.toHexString (i) + "\n" + sXML + "\n" + sXML2);
}
}
}
示例16
@Test
@Ignore ("Takes too long and was already tested with JDK 1.8 runtime parser")
public void testSpecialCharactersXML11Text ()
{
final EXMLSerializeVersion eXMLSerializeVersion = EXMLSerializeVersion.XML_11;
final XMLWriterSettings aSettings = new XMLWriterSettings ().setSerializeVersion (eXMLSerializeVersion);
for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; ++i)
if (!XMLCharHelper.isInvalidXMLTextChar (eXMLSerializeVersion, (char) i))
{
final String sText = "abc" + (char) i + "def";
assertEquals (7, sText.length ());
final IMicroDocument aDoc = new MicroDocument ();
aDoc.appendElement ("root").appendText (sText);
final String sXML = MicroWriter.getNodeAsString (aDoc, aSettings);
final IMicroDocument aDoc2 = MicroReader.readMicroXML (sXML);
assertNotNull ("Failed to read with byte " + i + "\n" + sXML, aDoc2);
assertEquals ("Length for byte " + i, i == 0 ? 6 : 7, aDoc2.getDocumentElement ().getTextContent ().length ());
// Difference between created "0x2028" and read "\n"
if (i != 0x2028)
if (!aDoc.isEqualContent (aDoc2))
{
final String sXML2 = MicroWriter.getNodeAsString (aDoc2, aSettings);
fail ("0x" + Integer.toHexString (i) + "\n" + sXML + "\n" + sXML2);
}
}
}
示例17
@Test
@Ignore ("Takes too long and was already tested with JDK 1.8 runtime parser")
public void testSpecialCharactersXML11CDATA ()
{
final EXMLSerializeVersion eXMLSerializeVersion = EXMLSerializeVersion.XML_11;
final XMLWriterSettings aSettings = new XMLWriterSettings ().setSerializeVersion (eXMLSerializeVersion);
for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; ++i)
if (!XMLCharHelper.isInvalidXMLCDATAChar (eXMLSerializeVersion, (char) i))
{
final String sText = "abc" + (char) i + "def";
assertEquals (7, sText.length ());
final IMicroDocument aDoc = new MicroDocument ();
aDoc.appendElement ("root").appendCDATA (sText);
final String sXML = MicroWriter.getNodeAsString (aDoc, aSettings);
final IMicroDocument aDoc2 = MicroReader.readMicroXML (sXML);
assertNotNull ("Failed to read with byte " + i + "\n" + sXML, aDoc2);
assertEquals ("Length for byte " + i, i == 0 ? 6 : 7, aDoc2.getDocumentElement ().getTextContent ().length ());
// Difference between created "\r" and read "\n"
// Difference between created "0x2028" and read "\n"
if (i != '\r' && i != 0x2028)
if (!aDoc.isEqualContent (aDoc2))
{
final String sXML2 = MicroWriter.getNodeAsString (aDoc2, aSettings);
fail ("0x" + Integer.toHexString (i) + "\n" + sXML + "\n" + sXML2);
}
}
}
示例18
/**
* Test: use namespaces all over the place and mix them quite complex
*/
@Test
public void testNamespaces ()
{
final XMLWriterSettings xs = new XMLWriterSettings ();
xs.setIndent (EXMLSerializeIndent.NONE);
final String s = "<?xml version=\"1.0\"?>" +
"<verrryoot>" +
"<root xmlns=\"myuri\" xmlns:a='foo'>" +
"<child xmlns=\"\">" +
"<a:child2>Value text - no entities!</a:child2>" +
"</child>" +
"</root>" +
"</verrryoot>";
final IMicroDocument aDoc = MicroReader.readMicroXML (s);
assertNotNull (aDoc);
final NonBlockingByteArrayOutputStream baos = new NonBlockingByteArrayOutputStream ();
new MicroSerializer (xs).write (aDoc, baos);
final String sXML = baos.getAsString (StandardCharsets.UTF_8);
assertEquals ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<verrryoot>" +
"<root xmlns=\"myuri\">" +
"<ns0:child xmlns:ns0=\"\">" +
"<ns1:child2 xmlns:ns1=\"foo\">Value text - no entities!</ns1:child2>" +
"</ns0:child>" +
"</root>" +
"</verrryoot>",
sXML);
}
示例19
/**
* Test: Use 2 different namespaces and use them both more than once
*/
@Test
public void testNamespaces2 ()
{
final XMLWriterSettings xs = new XMLWriterSettings ();
xs.setIndent (EXMLSerializeIndent.NONE);
final String s = "<?xml version=\"1.0\"?>" +
"<verrryoot xmlns='uri1'>" +
"<root>" +
"<child xmlns='uri2'>" +
"<child2>Value text - no entities!</child2>" +
"</child>" +
"</root>" +
"</verrryoot>";
final IMicroDocument aDoc = MicroReader.readMicroXML (s);
assertNotNull (aDoc);
final NonBlockingByteArrayOutputStream baos = new NonBlockingByteArrayOutputStream ();
new MicroSerializer (xs).write (aDoc, baos);
final String sXML = baos.getAsString (StandardCharsets.UTF_8);
assertEquals ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<verrryoot xmlns=\"uri1\">" +
"<root>" +
"<ns0:child xmlns:ns0=\"uri2\">" +
"<ns0:child2>Value text - no entities!</ns0:child2>" +
"</ns0:child>" +
"</root>" +
"</verrryoot>",
sXML);
}
示例20
/**
* Test: declare all namespaces in the root element and use them in nested
* elements
*/
@Test
public void testNamespaces3 ()
{
final XMLWriterSettings xs = new XMLWriterSettings ();
xs.setIndent (EXMLSerializeIndent.NONE);
xs.setUseDoubleQuotesForAttributes (false);
final String s = "<?xml version=\"1.0\"?>" +
"<verrryoot xmlns='uri1' xmlns:a='uri2'>" +
"<root>" +
"<a:child>" +
"<a:child2>Value text - no entities!</a:child2>" +
"</a:child>" +
"</root>" +
"</verrryoot>";
final IMicroDocument aDoc = MicroReader.readMicroXML (s);
assertNotNull (aDoc);
final NonBlockingByteArrayOutputStream baos = new NonBlockingByteArrayOutputStream ();
new MicroSerializer (xs).write (aDoc, baos);
final String sXML = baos.getAsString (StandardCharsets.UTF_8);
assertEquals ("<?xml version='1.0' encoding='UTF-8'?>" +
"<verrryoot xmlns='uri1'>" +
"<root>" +
"<ns0:child xmlns:ns0='uri2'>" +
"<ns0:child2>Value text - no entities!</ns0:child2>" +
"</ns0:child>" +
"</root>" +
"</verrryoot>",
sXML);
}
示例21
@Test
public void testSimple ()
{
final IMicroDocument aDoc = new MicroDocument ();
aDoc.appendElement ("test");
assertNotNull (new MicroDOMInputStreamProvider (aDoc, XMLWriterSettings.DEFAULT_XML_CHARSET_OBJ).getInputStream ());
}
示例22
/**
* @return The {@link IXMLWriterSettings} to be used to serialize the data.
*/
@Nonnull
@OverrideOnDemand
protected IXMLWriterSettings getXMLWriterSettings ()
{
return XMLWriterSettings.DEFAULT_XML_SETTINGS;
}
示例23
@Nonnull
public IPSWriterSettings setXMLWriterSettings (@Nonnull final IXMLWriterSettings aXMLWriterSettings)
{
ValueEnforcer.notNull (aXMLWriterSettings, "XMLWriterSettings");
m_aXMLWriterSettings = new XMLWriterSettings (aXMLWriterSettings);
return this;
}
示例24
@Test
public void testBasic () throws Exception
{
final PSPreprocessor aPreprocessor = new PSPreprocessor (PSXPathQueryBinding.getInstance ());
for (final IReadableResource aRes : SchematronTestHelper.getAllValidSchematronFiles ())
{
// Resolve all includes
final IMicroDocument aDoc = SchematronHelper.getWithResolvedSchematronIncludes (aRes, false);
assertNotNull (aDoc);
// Read to domain object
final PSReader aReader = new PSReader (aRes);
final PSSchema aSchema = aReader.readSchemaFromXML (aDoc.getDocumentElement ());
assertNotNull (aSchema);
// Ensure the schema is valid
final CollectingPSErrorHandler aErrHdl = new CollectingPSErrorHandler ();
assertTrue (aRes.getPath (), aSchema.isValid (aErrHdl));
assertTrue (aErrHdl.isEmpty ());
// Convert to minified schema if not-yet minimal
final PSSchema aPreprocessedSchema = aPreprocessor.getAsMinimalSchema (aSchema);
assertNotNull (aPreprocessedSchema);
if (false)
{
final String sXML = MicroWriter.getNodeAsString (aPreprocessedSchema.getAsMicroElement ());
SimpleFileIO.writeFile (new File ("test-minified",
FilenameHelper.getWithoutPath (aRes.getPath ()) + ".min-pure.sch"),
sXML,
XMLWriterSettings.DEFAULT_XML_CHARSET_OBJ);
}
// Ensure it is still valid and minimal
assertTrue (aRes.getPath (), aPreprocessedSchema.isValid (aErrHdl));
assertTrue (aRes.getPath (), aPreprocessedSchema.isMinimal ());
}
}
示例25
@Test
public void testSafeXMLStreamWriter ()
{
final com.helger.jaxb.mock.external.MockJAXBArchive aArc = new com.helger.jaxb.mock.external.MockJAXBArchive ();
aArc.setVersion ("1.23");
for (int i = 0; i < 2; ++i)
{
final com.helger.jaxb.mock.external.MockJAXBCollection aCollection = new com.helger.jaxb.mock.external.MockJAXBCollection ();
aCollection.setDescription ("InternalDesc-" + i);
aCollection.setID (i);
final com.helger.jaxb.mock.external.MockJAXBIssue aIssue = new com.helger.jaxb.mock.external.MockJAXBIssue ();
aIssue.setTitle (BigDecimal.valueOf (10000 + i));
aIssue.setSubTitle ("Test" + i);
aCollection.getIssue ().add (aIssue);
aArc.getCollection ().add (aCollection);
}
final MockExternalArchiveWriterBuilder aWriter = new MockExternalArchiveWriterBuilder ();
aWriter.setNamespaceContext (new MapBasedNamespaceContext ().addDefaultNamespaceURI ("http://urn.example.org/bla"));
final NonBlockingStringWriter aSW = new NonBlockingStringWriter ();
aWriter.write (aArc,
SafeXMLStreamWriter.create (aSW,
new XMLWriterSettings ().setIndent (EXMLSerializeIndent.ALIGN_ONLY)
.setNewLineMode (ENewLineMode.UNIX))
.setDebugMode (true));
assertEquals ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<Root Version=\"1.23\">\n" +
"<Collection ID=\"0\" Description=\"InternalDesc-0\">\n" +
"<Issue ID=\"0\" CollectionID=\"0\" PageCount=\"0\" ArticleCount=\"0\" DirAbsolute=\"0\">\n" +
"<Title>10000</Title>\n" +
"<SubTitle>Test0</SubTitle>\n" +
"</Issue>\n" +
"</Collection>\n" +
"<Collection ID=\"1\" Description=\"InternalDesc-1\">\n" +
"<Issue ID=\"0\" CollectionID=\"0\" PageCount=\"0\" ArticleCount=\"0\" DirAbsolute=\"0\">\n" +
"<Title>10001</Title>\n" +
"<SubTitle>Test1</SubTitle>\n" +
"</Issue>\n" +
"</Collection>\n" +
"</Root>",
aSW.getAsString ());
}
示例26
public SettingsPersistenceXML (@Nonnull final ISettingsFactory <T> aSettingsFactory)
{
this (aSettingsFactory, XMLWriterSettings.DEFAULT_XML_SETTINGS);
}
示例27
public MicroSerializer ()
{
this (XMLWriterSettings.DEFAULT_XML_SETTINGS);
}
示例28
@Test
public void testSpecialChars () throws Exception
{
final EXMLVersion eXMLVersion = EXMLVersion.XML_10;
final EXMLSerializeVersion eXMLSerializeVersion = EXMLSerializeVersion.getFromXMLVersionOrThrow (eXMLVersion);
final StringBuilder aAttrVal = new StringBuilder ();
final StringBuilder aText = new StringBuilder ();
for (char i = 0; i < 256; ++i)
{
if (!XMLCharHelper.isInvalidXMLAttributeValueChar (eXMLSerializeVersion, i))
aAttrVal.append (i);
if (!XMLCharHelper.isInvalidXMLTextChar (eXMLSerializeVersion, i))
aText.append (i);
}
final Document aDoc = XMLFactory.newDocument (eXMLVersion);
final Element eRoot = (Element) aDoc.appendChild (aDoc.createElement ("root"));
eRoot.setAttribute ("test", aAttrVal.toString ());
final Element e1 = (Element) eRoot.appendChild (aDoc.createElement ("a"));
e1.appendChild (aDoc.createTextNode (aText.toString ()));
final Element e2 = (Element) eRoot.appendChild (aDoc.createElement ("b"));
e2.appendChild (aDoc.createCDATASection ("aaaaaaaaaaa]]>bbbbbbbbbbb]]>ccccccccc"));
final Element e3 = (Element) eRoot.appendChild (aDoc.createElement ("c"));
e3.appendChild (aDoc.createCDATASection ("]]>"));
if (false)
e3.appendChild (aDoc.createComment ("<!--"));
e3.appendChild (aDoc.createTextNode ("abc"));
if (false)
e3.appendChild (aDoc.createComment ("-->"));
final NonBlockingStringWriter aSW = new NonBlockingStringWriter ();
XMLTransformerFactory.newTransformer ().transform (new DOMSource (aDoc), new StreamResult (aSW));
final String sTransform = aSW.getAsString ();
final Document aDoc2 = DOMReader.readXMLDOM (sTransform);
final Node e3a = aDoc2.getDocumentElement ().getChildNodes ().item (2);
aSW.reset ();
XMLTransformerFactory.newTransformer ().transform (new DOMSource (e3a), new StreamResult (aSW));
final String sXML = XMLWriter.getNodeAsString (aDoc,
new XMLWriterSettings ().setIncorrectCharacterHandling (EXMLIncorrectCharacterHandling.WRITE_TO_FILE_NO_LOG)
.setIndent (EXMLSerializeIndent.NONE));
assertNotNull (sXML);
assertNotNull ("Failed to read: " + sXML, DOMReader.readXMLDOM (sXML));
}
示例29
@Test
public void testWithNamespaceContext ()
{
final XMLWriterSettings aSettings = new XMLWriterSettings ().setIndent (EXMLSerializeIndent.NONE)
.setCharset (StandardCharsets.ISO_8859_1);
final IMicroDocument aDoc = new MicroDocument ();
final IMicroElement eRoot = aDoc.appendElement ("ns1url", "root");
eRoot.appendElement ("ns2url", "child1");
eRoot.appendElement ("ns2url", "child2").setAttribute ("attr1", "a");
eRoot.appendElement ("ns3url", "child3").setAttribute ("ns3url", "attr1", "a");
eRoot.appendElement ("ns3url", "child4").setAttribute ("ns4url", "attr1", "a");
String s = MicroWriter.getNodeAsString (aDoc, aSettings);
assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
"<root xmlns=\"ns1url\">" +
"<ns0:child1 xmlns:ns0=\"ns2url\" />" +
"<ns0:child2 xmlns:ns0=\"ns2url\" attr1=\"a\" />" +
"<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
"<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
"</root>",
s);
final MapBasedNamespaceContext aCtx = new MapBasedNamespaceContext ();
aCtx.addMapping ("a", "ns1url");
aSettings.setNamespaceContext (aCtx);
s = MicroWriter.getNodeAsString (aDoc, aSettings);
assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
"<a:root xmlns:a=\"ns1url\">" +
"<ns0:child1 xmlns:ns0=\"ns2url\" />" +
"<ns0:child2 xmlns:ns0=\"ns2url\" attr1=\"a\" />" +
"<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
"<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
"</a:root>",
s);
// Add mapping to namespace context
aCtx.addMapping ("xy", "ns2url");
s = MicroWriter.getNodeAsString (aDoc, aSettings);
assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
"<a:root xmlns:a=\"ns1url\">" +
"<xy:child1 xmlns:xy=\"ns2url\" />" +
"<xy:child2 xmlns:xy=\"ns2url\" attr1=\"a\" />" +
"<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
"<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
"</a:root>",
s);
// Put namespace context mappings in root
aSettings.setPutNamespaceContextPrefixesInRoot (true);
s = MicroWriter.getNodeAsString (aDoc, aSettings);
assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
"<a:root xmlns:a=\"ns1url\" xmlns:xy=\"ns2url\">" +
"<xy:child1 />" +
"<xy:child2 attr1=\"a\" />" +
"<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
"<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
"</a:root>",
s);
eRoot.appendElement ("ns3url", "zz");
s = MicroWriter.getNodeAsString (aDoc, aSettings);
assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
"<a:root xmlns:a=\"ns1url\" xmlns:xy=\"ns2url\">" +
"<xy:child1 />" +
"<xy:child2 attr1=\"a\" />" +
"<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
"<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
"<ns0:zz xmlns:ns0=\"ns3url\" />" +
"</a:root>",
s);
}
示例30
@Test
public void testWithoutEmitNamespaces ()
{
final XMLWriterSettings aSettings = new XMLWriterSettings ().setIndent (EXMLSerializeIndent.NONE)
.setCharset (StandardCharsets.ISO_8859_1);
final IMicroDocument aDoc = new MicroDocument ();
final IMicroElement eRoot = aDoc.appendElement ("ns1url", "root");
eRoot.appendElement ("ns2url", "child1");
eRoot.appendElement ("ns2url", "child2").setAttribute ("attr1", "a");
eRoot.appendElement ("ns3url", "child3").setAttribute ("ns3url", "attr1", "a");
eRoot.appendElement ("ns3url", "child4").setAttribute ("ns4url", "attr1", "a");
String s = MicroWriter.getNodeAsString (aDoc, aSettings);
assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
"<root xmlns=\"ns1url\">" +
"<ns0:child1 xmlns:ns0=\"ns2url\" />" +
"<ns0:child2 xmlns:ns0=\"ns2url\" attr1=\"a\" />" +
"<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
"<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
"</root>",
s);
aSettings.setPutNamespaceContextPrefixesInRoot (true);
s = MicroWriter.getNodeAsString (aDoc, aSettings);
assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
"<root xmlns=\"ns1url\">" +
"<ns0:child1 xmlns:ns0=\"ns2url\" />" +
"<ns0:child2 xmlns:ns0=\"ns2url\" attr1=\"a\" />" +
"<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
"<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
"</root>",
s);
aSettings.setPutNamespaceContextPrefixesInRoot (false);
aSettings.setEmitNamespaces (false);
s = MicroWriter.getNodeAsString (aDoc, aSettings);
assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
"<root>" +
"<child1 />" +
"<child2 attr1=\"a\" />" +
"<child3 attr1=\"a\" />" +
"<child4 attr1=\"a\" />" +
"</root>",
s);
aSettings.setPutNamespaceContextPrefixesInRoot (true);
s = MicroWriter.getNodeAsString (aDoc, aSettings);
assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
"<root>" +
"<child1 />" +
"<child2 attr1=\"a\" />" +
"<child3 attr1=\"a\" />" +
"<child4 attr1=\"a\" />" +
"</root>",
s);
}