added more xml writer options and improved api doc output.
This commit is contained in:
parent
380b829fcb
commit
5f08acb488
12 changed files with 571 additions and 250 deletions
|
|
@ -39,7 +39,7 @@ import org.x4o.xml.lang.X4OLanguage;
|
|||
import org.xml.sax.SAXException;
|
||||
|
||||
/**
|
||||
* EldSchemaGenerator Creates XML Schema for a namespace uri from a x4o language driver.
|
||||
* EldXsdWriter creates XML Schema files fom a x4o language.
|
||||
*
|
||||
* @author Willem Cazander
|
||||
* @version 1.0 Aug 8, 2012
|
||||
|
|
|
|||
|
|
@ -71,10 +71,11 @@ public class AbstractContentWriterHandler implements ContentHandler {
|
|||
public final static String OUTPUT_ENCODING = PROPERTY_CONTEXT_PREFIX+"output/encoding";
|
||||
public final static String OUTPUT_CHAR_TAB = PROPERTY_CONTEXT_PREFIX+"output/char-tab";
|
||||
public final static String OUTPUT_CHAR_NEWLINE = PROPERTY_CONTEXT_PREFIX+"output/char-newline";
|
||||
public final static String OUTPUT_CHAR_NULL = PROPERTY_CONTEXT_PREFIX+"output/char-null";
|
||||
public final static String OUTPUT_COMMENT_ENABLE = PROPERTY_CONTEXT_PREFIX+"output/comment-enable";
|
||||
public final static String OUTPUT_COMMENT_AUTO_SPACE = PROPERTY_CONTEXT_PREFIX+"output/comment-auto-space";
|
||||
//public final static String OUTPUT_LINE_BREAK_WIDTH = PROPERTY_CONTEXT_PREFIX+"output/line-break-width";
|
||||
//public final static String OUTPUT_LINE_PER_ATTRIBUTE = PROPERTY_CONTEXT_PREFIX+"output/line-per-attribute";
|
||||
public final static String OUTPUT_LINE_BREAK_WIDTH = PROPERTY_CONTEXT_PREFIX+"output/line-break-width";
|
||||
public final static String OUTPUT_LINE_PER_ATTRIBUTE = PROPERTY_CONTEXT_PREFIX+"output/line-per-attribute";
|
||||
public final static String PROLOG_LICENCE_FILE = PROPERTY_CONTEXT_PREFIX+"prolog/licence-file";
|
||||
public final static String PROLOG_LICENCE_RESOURCE = PROPERTY_CONTEXT_PREFIX+"prolog/licence-resource";
|
||||
public final static String PROLOG_LICENCE_ENCODING = PROPERTY_CONTEXT_PREFIX+"prolog/licence-encoding";
|
||||
|
|
@ -89,10 +90,11 @@ public class AbstractContentWriterHandler implements ContentHandler {
|
|||
new PropertyConfigItem(OUTPUT_ENCODING, String.class, XMLConstants.XML_DEFAULT_ENCODING),
|
||||
new PropertyConfigItem(OUTPUT_CHAR_TAB, String.class, XMLConstants.CHAR_TAB+""),
|
||||
new PropertyConfigItem(OUTPUT_CHAR_NEWLINE, String.class, XMLConstants.CHAR_NEWLINE+""),
|
||||
new PropertyConfigItem(OUTPUT_CHAR_NULL, String.class, "null"), // TODO: or "" ?? (or skip)
|
||||
new PropertyConfigItem(OUTPUT_COMMENT_ENABLE, Boolean.class, true),
|
||||
new PropertyConfigItem(OUTPUT_COMMENT_AUTO_SPACE, Boolean.class, true),
|
||||
// TODO new PropertyConfigItem(OUTPUT_LINE_BREAK_WIDTH, Integer.class, -1),
|
||||
// TODO new PropertyConfigItem(OUTPUT_LINE_PER_ATTRIBUTE, Boolean.class, false),
|
||||
new PropertyConfigItem(OUTPUT_LINE_BREAK_WIDTH, Integer.class, -1),
|
||||
new PropertyConfigItem(OUTPUT_LINE_PER_ATTRIBUTE, Boolean.class, false),
|
||||
new PropertyConfigItem(PROLOG_LICENCE_ENCODING, String.class, XMLConstants.XML_DEFAULT_ENCODING),
|
||||
new PropertyConfigItem(PROLOG_LICENCE_FILE, File.class ),
|
||||
new PropertyConfigItem(PROLOG_LICENCE_RESOURCE, String.class ),
|
||||
|
|
@ -289,7 +291,7 @@ public class AbstractContentWriterHandler implements ContentHandler {
|
|||
startElement.append(' ');
|
||||
startElement.append(XMLConstants.XMLNS_ATTRIBUTE);
|
||||
if ("".equals(prefix)==false) {
|
||||
startElement.append(':');
|
||||
startElement.append(XMLConstants.XMLNS_ASSIGN);
|
||||
startElement.append(prefix);
|
||||
}
|
||||
startElement.append("=\"");
|
||||
|
|
@ -333,17 +335,32 @@ public class AbstractContentWriterHandler implements ContentHandler {
|
|||
}
|
||||
}
|
||||
|
||||
private void printElementAttributeNewLineSpace() {
|
||||
startElement.append(XMLConstants.CHAR_NEWLINE);
|
||||
for (int ii = 0; ii < indent+1; ii++) {
|
||||
startElement.append(getPropertyConfig().getPropertyString(OUTPUT_CHAR_TAB));
|
||||
}
|
||||
}
|
||||
|
||||
private void startElementAttributes(Attributes atts) throws SAXException {
|
||||
int prevChars = 0;
|
||||
for (int i=0;i<atts.getLength();i++) {
|
||||
String attributeUri = atts.getURI(i);
|
||||
String attributeName = XMLConstants.escapeAttributeName(atts.getLocalName(i));
|
||||
String attributeValue = atts.getValue(i);
|
||||
if (attributeValue==null) {
|
||||
attributeValue = "null"; // TODO: Add null value key to config.
|
||||
attributeValue = propertyConfig.getPropertyString(OUTPUT_CHAR_NULL);
|
||||
}
|
||||
String attributeValueSafe = XMLConstants.escapeAttributeValue(attributeValue);
|
||||
|
||||
startElement.append(' ');
|
||||
if (propertyConfig.getPropertyBoolean(OUTPUT_LINE_PER_ATTRIBUTE)) {
|
||||
if (i==0) {
|
||||
printElementAttributeNewLineSpace();
|
||||
}
|
||||
} else {
|
||||
startElement.append(' ');
|
||||
}
|
||||
|
||||
if (XMLConstants.NULL_NS_URI.equals(attributeUri) | attributeUri ==null) {
|
||||
startElement.append(attributeName);
|
||||
} else {
|
||||
|
|
@ -354,11 +371,20 @@ public class AbstractContentWriterHandler implements ContentHandler {
|
|||
startElement.append("=\"");
|
||||
startElement.append(attributeValueSafe);
|
||||
startElement.append('"');
|
||||
boolean printNewLine = attributeValueSafe.length()>80; // TODO add config
|
||||
if (printNewLine) {
|
||||
startElement.append(XMLConstants.CHAR_NEWLINE);
|
||||
for (int ii = 0; ii < indent+1; ii++) {
|
||||
startElement.append(getPropertyConfig().getPropertyString(OUTPUT_CHAR_TAB));
|
||||
|
||||
if (propertyConfig.getPropertyBoolean(OUTPUT_LINE_PER_ATTRIBUTE)) {
|
||||
printElementAttributeNewLineSpace();
|
||||
}
|
||||
|
||||
int breakLines = propertyConfig.getPropertyInteger(OUTPUT_LINE_BREAK_WIDTH);
|
||||
if (breakLines>0) {
|
||||
if (prevChars==0 && startElement.length() > breakLines) {
|
||||
printElementAttributeNewLineSpace();
|
||||
prevChars = startElement.length();
|
||||
}
|
||||
if (prevChars>0 && (startElement.length()-prevChars) > breakLines) {
|
||||
printElementAttributeNewLineSpace();
|
||||
prevChars = startElement.length();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,133 @@
|
|||
/*
|
||||
* Copyright (c) 2004-2013, Willem Cazander
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
|
||||
* that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
|
||||
* following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
|
||||
* the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
||||
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package org.x4o.xml.io.sax;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.x4o.xml.io.sax.ext.ContentWriterXml;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.helpers.AttributesImpl;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* ContentWriterXmlAttributeTest test xml attribute printing. *
|
||||
* @author Willem Cazander
|
||||
* @version 1.0 S 17, 2012
|
||||
*/
|
||||
public class ContentWriterXmlAttributeTest extends TestCase {
|
||||
|
||||
public void testAttributeNormal() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
writer.startDocument();
|
||||
AttributesImpl atts = new AttributesImpl();
|
||||
atts.addAttribute ("", "attr", "", "", "foobar");
|
||||
writer.startElementEnd("", "test", "", atts);
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.length()>0);
|
||||
assertTrue(output,output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test attr=\"foobar\"/>"));
|
||||
}
|
||||
|
||||
public void testAttributeEscape() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
writer.startDocument();
|
||||
AttributesImpl atts = new AttributesImpl();
|
||||
atts.addAttribute ("", "attr", "", "", "<test/> & 'foobar' is \"quoted\"!");
|
||||
writer.startElementEnd("", "test", "", atts);
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.length()>0);
|
||||
assertTrue(output,output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test attr=\"<test/> & 'foobar' is "e;quoted"e;!\"/>"));
|
||||
}
|
||||
|
||||
private String createLongAttribute(Map<String,Object> para) throws SAXException {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
for (String key:para.keySet()) {
|
||||
Object value = para.get(key);
|
||||
writer.getPropertyConfig().setProperty(key, value);
|
||||
}
|
||||
|
||||
AttributesImpl atts = new AttributesImpl();
|
||||
String data = "_FOR_FOO_BAR";
|
||||
String dataValue = "LOOP";
|
||||
for (int i=0;i<15;i++) {
|
||||
atts.addAttribute("", "attr"+i, "", "", dataValue+=data);
|
||||
}
|
||||
writer.startDocument();
|
||||
writer.startElement("", "test", "", atts);
|
||||
writer.startElement("", "testNode", "", new AttributesImpl());
|
||||
writer.endElement("", "testNode", "");
|
||||
writer.endElement("", "test", "");
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
return output;
|
||||
}
|
||||
|
||||
public void testAttributeLongNormal() throws Exception {
|
||||
Map<String,Object> para = new HashMap<String,Object>();
|
||||
String output = createLongAttribute(para);
|
||||
int newlines = output.split("\n").length;
|
||||
assertNotNull(output);
|
||||
assertTrue("outputs: "+newlines,newlines==4);
|
||||
}
|
||||
|
||||
public void testAttributeLongPerLine() throws Exception {
|
||||
Map<String,Object> para = new HashMap<String,Object>();
|
||||
para.put(ContentWriterXml.OUTPUT_LINE_PER_ATTRIBUTE, true);
|
||||
String output = createLongAttribute(para);
|
||||
int newlines = output.split("\n").length;
|
||||
assertNotNull(output);
|
||||
assertTrue("outputs: "+newlines,newlines==20);
|
||||
}
|
||||
|
||||
public void testAttributeLongSplit80() throws Exception {
|
||||
Map<String,Object> para = new HashMap<String,Object>();
|
||||
para.put(ContentWriterXml.OUTPUT_LINE_BREAK_WIDTH, 80);
|
||||
String output = createLongAttribute(para);
|
||||
int newlines = output.split("\n").length;
|
||||
assertNotNull(output);
|
||||
assertTrue("outputs: "+newlines,newlines==16);
|
||||
}
|
||||
|
||||
public void testAttributeLongSplit180() throws Exception {
|
||||
Map<String,Object> para = new HashMap<String,Object>();
|
||||
para.put(ContentWriterXml.OUTPUT_LINE_BREAK_WIDTH, 180);
|
||||
String output = createLongAttribute(para);
|
||||
int newlines = output.split("\n").length;
|
||||
assertNotNull(output);
|
||||
assertTrue("outputs: "+newlines,newlines==11);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,162 @@
|
|||
/*
|
||||
* Copyright (c) 2004-2013, Willem Cazander
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
|
||||
* that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
|
||||
* following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
|
||||
* the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
||||
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package org.x4o.xml.io.sax;
|
||||
|
||||
import java.io.StringWriter;
|
||||
|
||||
import org.x4o.xml.io.sax.ext.ContentWriterXml;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* ContentWriterXmlCDataTest tests cdata xml escaping.
|
||||
*
|
||||
* @author Willem Cazander
|
||||
* @version 1.0 Sep 17, 2013
|
||||
*/
|
||||
public class ContentWriterXmlCDataTest extends TestCase {
|
||||
|
||||
public void testCDATANone() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
writer.startDocument();
|
||||
writer.characters("foobar");
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.length()>0);
|
||||
assertTrue(output,output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>foobar"));
|
||||
}
|
||||
|
||||
public void testCDATANoneTagEscape() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
writer.startDocument();
|
||||
writer.characters("foobar<test/>");
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.length()>0);
|
||||
assertTrue(output,output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>foobar<test/>"));
|
||||
}
|
||||
|
||||
public void testCDATANormal() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
writer.startDocument();
|
||||
writer.startCDATA();
|
||||
writer.characters("foobar");
|
||||
writer.endCDATA();
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.length()>0);
|
||||
assertTrue(output,output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><![CDATA[foobar]]>"));
|
||||
}
|
||||
|
||||
public void testCDATAEscapeTag() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
writer.startDocument();
|
||||
writer.startCDATA();
|
||||
writer.characters("foobar<test/>");
|
||||
writer.endCDATA();
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.length()>0);
|
||||
assertTrue(output,output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><![CDATA[foobar<test/>]]>"));
|
||||
}
|
||||
|
||||
public void testCDATAEscapeStart() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
writer.startDocument();
|
||||
writer.startCDATA();
|
||||
writer.characters("<![CDATA[foobar");
|
||||
writer.endCDATA();
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.length()>0);
|
||||
assertTrue(output,output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><![CDATA[foobar]]>"));
|
||||
}
|
||||
|
||||
public void testCDATAEscapeEnd() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
writer.startDocument();
|
||||
writer.startCDATA();
|
||||
writer.characters("foobar]]>");
|
||||
writer.endCDATA();
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.length()>0);
|
||||
assertTrue(output,output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><![CDATA[foobar]]>"));
|
||||
}
|
||||
|
||||
public void testCDATAEscapeInvalid() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
writer.startDocument();
|
||||
writer.startCDATA();
|
||||
writer.characters("<![CDATA[tokens like ']]>' are <invalid>]]>");
|
||||
writer.endCDATA();
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.length()>0);
|
||||
assertTrue(output,output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><![CDATA[tokens like \'\' are <invalid>]]>"));
|
||||
}
|
||||
|
||||
public void testCDATAEscapeValid() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
writer.startDocument();
|
||||
writer.startCDATA();
|
||||
writer.characters("<![CDATA[tokens like ']]]]><![CDATA[>' are <valid>]]>");
|
||||
writer.endCDATA();
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.length()>0);
|
||||
assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><![CDATA[tokens like \']]>\' are <valid>]]>"));
|
||||
}
|
||||
}
|
||||
|
|
@ -39,130 +39,6 @@ import junit.framework.TestCase;
|
|||
*/
|
||||
public class ContentWriterXmlTest extends TestCase {
|
||||
|
||||
public void testCDATANone() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
writer.startDocument();
|
||||
writer.characters("foobar");
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.length()>0);
|
||||
assertTrue(output,output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>foobar"));
|
||||
}
|
||||
|
||||
public void testCDATANoneTagEscape() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
writer.startDocument();
|
||||
writer.characters("foobar<test/>");
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.length()>0);
|
||||
assertTrue(output,output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>foobar<test/>"));
|
||||
}
|
||||
|
||||
public void testCDATANormal() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
writer.startDocument();
|
||||
writer.startCDATA();
|
||||
writer.characters("foobar");
|
||||
writer.endCDATA();
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.length()>0);
|
||||
assertTrue(output,output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><![CDATA[foobar]]>"));
|
||||
}
|
||||
|
||||
public void testCDATAEscapeTag() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
writer.startDocument();
|
||||
writer.startCDATA();
|
||||
writer.characters("foobar<test/>");
|
||||
writer.endCDATA();
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.length()>0);
|
||||
assertTrue(output,output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><![CDATA[foobar<test/>]]>"));
|
||||
}
|
||||
|
||||
public void testCDATAEscapeStart() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
writer.startDocument();
|
||||
writer.startCDATA();
|
||||
writer.characters("<![CDATA[foobar");
|
||||
writer.endCDATA();
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.length()>0);
|
||||
assertTrue(output,output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><![CDATA[foobar]]>"));
|
||||
}
|
||||
|
||||
public void testCDATAEscapeEnd() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
writer.startDocument();
|
||||
writer.startCDATA();
|
||||
writer.characters("foobar]]>");
|
||||
writer.endCDATA();
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.length()>0);
|
||||
assertTrue(output,output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><![CDATA[foobar]]>"));
|
||||
}
|
||||
|
||||
public void testCDATAEscapeInvalid() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
writer.startDocument();
|
||||
writer.startCDATA();
|
||||
writer.characters("<![CDATA[tokens like ']]>' are <invalid>]]>");
|
||||
writer.endCDATA();
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.length()>0);
|
||||
assertTrue(output,output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><![CDATA[tokens like \'\' are <invalid>]]>"));
|
||||
}
|
||||
|
||||
public void testCDATAEscapeValid() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
writer.startDocument();
|
||||
writer.startCDATA();
|
||||
writer.characters("<![CDATA[tokens like ']]]]><![CDATA[>' are <valid>]]>");
|
||||
writer.endCDATA();
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.length()>0);
|
||||
assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><![CDATA[tokens like \']]>\' are <valid>]]>"));
|
||||
}
|
||||
|
||||
public void testCharactersNormal() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
|
@ -190,39 +66,6 @@ public class ContentWriterXmlTest extends TestCase {
|
|||
assertTrue(output.length()>0);
|
||||
assertTrue(output,output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><test/> & 'foobar' is "e;quoted"e;!"));
|
||||
}
|
||||
|
||||
public void testAttributeNormal() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
writer.startDocument();
|
||||
AttributesImpl atts = new AttributesImpl();
|
||||
atts.addAttribute ("", "attr", "", "", "foobar");
|
||||
writer.startElementEnd("", "test", "", atts);
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.length()>0);
|
||||
assertTrue(output,output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test attr=\"foobar\"/>"));
|
||||
}
|
||||
|
||||
public void testAttributeEscape() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
writer.startDocument();
|
||||
AttributesImpl atts = new AttributesImpl();
|
||||
atts.addAttribute ("", "attr", "", "", "<test/> & 'foobar' is \"quoted\"!");
|
||||
writer.startElementEnd("", "test", "", atts);
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.length()>0);
|
||||
assertTrue(output,output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test attr=\"<test/> & 'foobar' is "e;quoted"e;!\"/>"));
|
||||
}
|
||||
|
||||
|
||||
public void testCommentNormal() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
|
|
@ -394,26 +237,4 @@ public class ContentWriterXmlTest extends TestCase {
|
|||
assertTrue(e.getMessage().contains("invalid char"));
|
||||
assertTrue(e.getMessage().contains("isInvalidChar=55296"));
|
||||
}
|
||||
|
||||
public void testAttributeValueLongData() throws Exception {
|
||||
StringWriter outputWriter = new StringWriter();
|
||||
ContentWriterXml writer = new ContentWriterXml(outputWriter);
|
||||
|
||||
AttributesImpl atts = new AttributesImpl();
|
||||
String data = "_FOR_FOO_BAR";
|
||||
String dataValue = "LOOP";
|
||||
for (int i=0;i<15;i++) {
|
||||
atts.addAttribute("", "attr"+i, "", "", dataValue+=data);
|
||||
}
|
||||
writer.startDocument();
|
||||
writer.startElement("", "test", "", atts);
|
||||
writer.startElement("", "testNode", "", new AttributesImpl());
|
||||
writer.endElement("", "testNode", "");
|
||||
writer.endElement("", "test", "");
|
||||
writer.endDocument();
|
||||
|
||||
String output = outputWriter.toString();
|
||||
assertNotNull(output);
|
||||
assertTrue(output.split("\n").length==13);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue