Split xml tag writer from content writer interface object hierachy.

This commit is contained in:
Willem Cazander 2014-03-14 19:03:56 +01:00
parent 80ade0b3f4
commit 82d966b849
17 changed files with 294 additions and 212 deletions

View file

@ -58,9 +58,6 @@ import org.xml.sax.helpers.AttributesImpl;
*/
public class EldXsdWriterElement {
static public final String SCHEMA_URI = XMLConstants.XML_SCHEMA_NS_URI;
private PropertyConfig propertyConfig;
protected X4OLanguage language = null;
protected ContentWriterXsd xsdWriter = null;
@ -155,7 +152,7 @@ public class EldXsdWriterElement {
return;
}
//xsdWriter.ignorableWhitespace(XMLConstants.CHAR_NEWLINE);
xsdWriter.comment(COMMENT_SEPERATOR);
xsdWriter.printComment(COMMENT_SEPERATOR);
String byValue = propertyConfig.getPropertyString(EldXsdWriter.PROLOG_GENERATED_BY, EldXsdWriter.class.getSimpleName());
if (!byValue.endsWith(".")) {
@ -175,8 +172,8 @@ public class EldXsdWriterElement {
}
b.append(COMMENT_TEXT);
b.append(" ");
xsdWriter.comment(b.toString());
xsdWriter.comment(COMMENT_SEPERATOR);
xsdWriter.printComment(b.toString());
xsdWriter.printComment(COMMENT_SEPERATOR);
}
private void prologWriteProvider(ElementNamespace ns) throws SAXException {
@ -202,7 +199,7 @@ public class EldXsdWriterElement {
b.append(String.format(formatLine,"Created on:",new Date()));
b.append("\n");
xsdWriter.comment(b.toString());
xsdWriter.printComment(b.toString());
}
public void startSchema(ElementNamespace ns) throws SAXException {
@ -212,10 +209,9 @@ public class EldXsdWriterElement {
prologWriteGenerator();
prologWriteProvider(ns);
xsdWriter.startPrefixMapping("", SCHEMA_URI);
for (String uri:namespaces.keySet()) {
String prefix = namespaces.get(uri);
xsdWriter.startPrefixMapping(prefix, uri);
xsdWriter.getContentWriterWrapped().startPrefixMapping(prefix, uri);
}
AttributesImpl atts = new AttributesImpl();
@ -223,7 +219,7 @@ public class EldXsdWriterElement {
atts.addAttribute ("", "elementFormDefault", "", "", "qualified");
atts.addAttribute ("", "attributeFormDefault", "", "", "unqualified");
atts.addAttribute ("", "targetNamespace", "", "", ns.getUri());
xsdWriter.startElement (SCHEMA_URI, "schema", "", atts);
xsdWriter.printTagStart(Tag.schema, atts);
for (String uri:namespaces.keySet()) {
if (ns.getUri().equals(uri)) {
@ -237,8 +233,8 @@ public class EldXsdWriterElement {
}
public void endSchema() throws SAXException {
xsdWriter.endElement (SCHEMA_URI, "schema" , "");
xsdWriter.ignorableWhitespace(XMLConstants.CHAR_NEWLINE);
xsdWriter.printTagEnd(Tag.schema);
xsdWriter.getContentWriterWrapped().ignorableWhitespace(XMLConstants.CHAR_NEWLINE);
xsdWriter.endDocument();
}
@ -256,13 +252,13 @@ public class EldXsdWriterElement {
AttributesImpl atts = new AttributesImpl();
if (nsWrite.getLanguageRoot()!=null && nsWrite.getLanguageRoot()) {
atts.addAttribute ("", "name", "", "", ec.getId());
xsdWriter.startElement (SCHEMA_URI, "element", "", atts); // Only in the language root xsd there is an element.
xsdWriter.printTagStart(Tag.element, atts);// Only in the language root xsd there is an element.
atts = new AttributesImpl();
xsdWriter.startElement (SCHEMA_URI, "complexType", "", atts);
xsdWriter.printTagStart(Tag.complexType, atts);
} else {
atts.addAttribute ("", "name", "", "", ec.getId()+"Type");
xsdWriter.startElement (SCHEMA_URI, "complexType", "", atts);
xsdWriter.printTagStart(Tag.complexType, atts);
}
if (ec.getSchemaContentBase()!=null) {
@ -271,27 +267,27 @@ public class EldXsdWriterElement {
if (ec.getSchemaContentMixed()!=null && ec.getSchemaContentMixed()) {
atts.addAttribute ("", "mixed", "", "", "true");
}
xsdWriter.startElement (SCHEMA_URI, "complexContent", "", atts);
xsdWriter.printTagStart(Tag.complexContent, atts);
} else {
xsdWriter.startElement (SCHEMA_URI, "simpleContent", "", atts);
xsdWriter.printTagStart(Tag.simpleContent, atts);
}
atts = new AttributesImpl();
atts.addAttribute ("", "base", "", "", ec.getSchemaContentBase());
xsdWriter.startElement (SCHEMA_URI, "extension", "", atts);
xsdWriter.printTagStart(Tag.extension, atts);
}
if (ec.getSchemaContentBase()==null) {
atts = new AttributesImpl();
atts.addAttribute ("", "minOccurs", "", "", "0"); // TODO: make unordered elements
atts.addAttribute ("", "maxOccurs", "", "", "unbounded");
xsdWriter.startElement (SCHEMA_URI, "choice", "", atts);
xsdWriter.printTagStart(Tag.choise, atts);
for (X4OLanguageModule mod:language.getLanguageModules()) {
for (ElementNamespace ns:mod.getElementNamespaces()) {
writeElementClassNamespaces(ec,nsWrite,ns);
}
}
xsdWriter.endElement(SCHEMA_URI, "choice", "");
xsdWriter.printTagEnd(Tag.choise);
}
List<String> attrNames = new ArrayList<String>(30);
@ -316,7 +312,7 @@ public class EldXsdWriterElement {
if (ec.getAutoAttributes()!=null && ec.getAutoAttributes()==false) {
// oke, reverse this if and rm whitespace.
xsdWriter.ignorableWhitespace(' ');
xsdWriter.getContentWriterWrapped().ignorableWhitespace(' ');
} else {
@ -354,27 +350,25 @@ public class EldXsdWriterElement {
} else {
atts.addAttribute ("", "type", "", "", "string");
}
xsdWriter.startElement (SCHEMA_URI, "attribute", "", atts);
xsdWriter.endElement(SCHEMA_URI, "attribute", "");
xsdWriter.printTagStartEnd(Tag.attribute, atts);
}
}
} else {
atts = new AttributesImpl();
xsdWriter.startElement (SCHEMA_URI, "anyAttribute", "", atts);
xsdWriter.endElement(SCHEMA_URI, "anyAttribute", "");
xsdWriter.printTagStartEnd(Tag.anyAttribute, atts);
}
}
if (ec.getSchemaContentBase()!=null) {
xsdWriter.endElement(SCHEMA_URI, "extension", "");
xsdWriter.printTagEnd(Tag.extension);
if (ec.getSchemaContentComplex()!=null && ec.getSchemaContentComplex()) {
xsdWriter.endElement(SCHEMA_URI, "complexContent", "");
xsdWriter.printTagEnd(Tag.complexContent);
} else {
xsdWriter.endElement(SCHEMA_URI, "simpleContent", "");
xsdWriter.printTagEnd(Tag.simpleContent);
}
}
xsdWriter.endElement(SCHEMA_URI, "complexType", "");
xsdWriter.printTagEnd(Tag.complexType);
if (nsWrite.getLanguageRoot()!=null && nsWrite.getLanguageRoot()) {
xsdWriter.endElement(SCHEMA_URI, "element", "");
xsdWriter.printTagEnd(Tag.element);
}
}
@ -426,8 +420,7 @@ public class EldXsdWriterElement {
atts.addAttribute ("", "name", "", "", refElement);
atts.addAttribute ("", "type", "", "", prefix+":"+refElement+"Type");
}
xsdWriter.startElement (SCHEMA_URI, "element", "", atts);
xsdWriter.endElement (SCHEMA_URI, "element", "");
xsdWriter.printTagStartEnd(Tag.element,atts);
}
}
}
@ -440,9 +433,9 @@ public class EldXsdWriterElement {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "name", "", "", ec.getId());
atts.addAttribute ("", "type", "", "", "this:"+ec.getId()+"Type");
xsdWriter.startElement(SCHEMA_URI, "element", "", atts);
xsdWriter.printTagStart(Tag.element,atts);
writeElementMetaBase(ec);
xsdWriter.endElement(SCHEMA_URI, "element", "");
xsdWriter.printTagEnd(Tag.element);
}
private void writeElementAttribute(ElementMetaBase base,AttributesImpl atts) throws SAXException {

View file

@ -61,7 +61,6 @@ import org.xml.sax.helpers.AttributesImpl;
*/
public class X4ODebugWriter {
static public final String DEBUG_URI = "http://language.x4o.org/xml/ns/debug-output";
protected ContentWriter contentWriter = null;
@ -74,6 +73,12 @@ public class X4ODebugWriter {
return contentWriter;
}
enum X4ODebugTag {
execute222Phase,
testTasg,
stekel
}
public X4OPhaseListener createDebugX4OPhaseListener() {
return new DebugX4OPhaseListener();
}
@ -94,7 +99,7 @@ public class X4ODebugWriter {
if (elementLanguage!=null) {
atts.addAttribute("", "language","","", elementLanguage.getLanguage().getLanguageName());
}
contentWriter.startElement (DEBUG_URI, "executePhase", "", atts);
contentWriter.startElement (DEBUG_URI, X4ODebugTag.execute222Phase.name(), "", atts);
} catch (SAXException e) {
throw new X4OPhaseException(phase,e);
}
@ -110,7 +115,7 @@ public class X4ODebugWriter {
contentWriter.startElement (DEBUG_URI, "executePhaseDone", "", atts);
contentWriter.endElement (DEBUG_URI, "executePhaseDone" , "");
contentWriter.endElement (DEBUG_URI, "executePhase" , "");
contentWriter.endElement (DEBUG_URI, X4ODebugTag.execute222Phase.name() , "");
} catch (SAXException e) {
throw new X4OPhaseException(phase,e);
}

View file

@ -55,7 +55,6 @@ import org.xml.sax.helpers.AttributesImpl;
*/
public class AbstractContentWriterHandler implements ContentHandler {
protected final Attributes EMPTY_ATTRIBUTES = new AttributesImpl();
private final PropertyConfig propertyConfig;
private Writer out = null;
private int indent = 0;
@ -65,7 +64,7 @@ public class AbstractContentWriterHandler implements ContentHandler {
private boolean printReturn = false;
private String lastElement = null;
private Stack<String> elements = null;
private final static String PROPERTY_CONTEXT_PREFIX = PropertyConfig.X4O_PROPERTIES_PREFIX+PropertyConfig.X4O_PROPERTIES_WRITER_XML;
public final static PropertyConfig DEFAULT_PROPERTY_CONFIG;
public final static String OUTPUT_ENCODING = PROPERTY_CONTEXT_PREFIX+"output/encoding";
@ -90,7 +89,7 @@ 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_CHAR_NULL, String.class, "NULL"),
new PropertyConfigItem(OUTPUT_COMMENT_ENABLE, Boolean.class, true),
new PropertyConfigItem(OUTPUT_COMMENT_AUTO_SPACE, Boolean.class, true),
new PropertyConfigItem(OUTPUT_LINE_BREAK_WIDTH, Integer.class, -1),

View file

@ -46,13 +46,38 @@ public interface ContentWriter extends ContentHandler,LexicalHandler {
*/
void startElementEnd(String uri, String localName, String name,Attributes atts) throws SAXException;
/**
* Writes a String as xml comment.
* @param text The text to have embedded as comment in xml.
* @throws SAXException On error.
*/
void comment(String text) throws SAXException;
/**
* Writes a whitespace String to the body.
* @param text The String of whitespace to write.
* @throws SAXException On error.
*/
void ignorableWhitespace(String text) throws SAXException;
/**
* Writes a whitespace character to the body.
* @param c The character to write.
* @throws SAXException On error;
*/
void ignorableWhitespace(char c) throws SAXException;
/**
* Writes a String in the body.
* @param text The text to write.
* @throws SAXException On error.
*/
void characters(String text) throws SAXException;
/**
* Writes a single character in the body body.
* @param c The character to write.
* @throws SAXException On error.
*/
void characters(char c) throws SAXException;
}

View file

@ -31,19 +31,27 @@ import org.xml.sax.SAXException;
* @author Willem Cazander
* @version 1.0 May 3, 2013
*/
public interface ContentWriterTag<T extends Enum<?>> extends ContentWriter {
public interface ContentWriterTag<TAG extends Enum<?>> {
String getTagNamespaceUri();
void printTagStartEnd(T tag) throws SAXException;
void startDocument() throws SAXException;
void printTagStartEnd(T tag,Attributes atts) throws SAXException;
void endDocument() throws SAXException;
void printTagStart(T tag) throws SAXException;
void printTagStartEnd(TAG tag) throws SAXException;
void printTagStart(T tag,Attributes atts) throws SAXException;
void printTagStartEnd(TAG tag,Attributes atts) throws SAXException;
void printTagEnd(T tag) throws SAXException;
void printTagStart(TAG tag) throws SAXException;
void printTagCharacters(T tag,String text) throws SAXException;
void printTagStart(TAG tag,Attributes atts) throws SAXException;
void printTagEnd(TAG tag) throws SAXException;
void printTagCharacters(TAG tag,String text) throws SAXException;
void printCharacters(String text) throws SAXException;
void printComment(String text) throws SAXException;
}

View file

@ -0,0 +1,121 @@
/*
* 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.ext;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
/**
* ContentWriterXmlTag can write enum based xml events.
*
* @author Willem Cazander
* @version 1.0 May 3, 2013
*/
public class ContentWriterTagWrapper<TAG extends Enum<?>,TAG_WRITER extends ContentWriter> implements ContentWriterTag<TAG> {
private final Attributes EMPTY_ATTRIBUTES = new AttributesImpl();
private final TAG_WRITER contentWriter;
private final String tagNamespaceUri;
private final String tagNamespacePrefix;
public ContentWriterTagWrapper(TAG_WRITER contentWriter,String tagNamespaceUri,String tagNamespacePrefix) {
if (contentWriter==null) {
throw new NullPointerException("Can't create wrapper on null ContentWriter");
}
if (tagNamespaceUri==null) {
throw new NullPointerException("Can't create wrapper with null tagNamespaceUri");
}
if (tagNamespacePrefix==null) {
throw new NullPointerException("Can't create wrapper with null tagNamespacePrefix");
}
this.contentWriter=contentWriter;
this.tagNamespaceUri=tagNamespaceUri;
this.tagNamespacePrefix=tagNamespacePrefix;
}
public TAG_WRITER getContentWriterWrapped() {
return contentWriter;
}
public String getTagNamespaceUri() {
return tagNamespaceUri;
}
public void startDocument() throws SAXException {
contentWriter.startDocument();
contentWriter.startPrefixMapping(tagNamespacePrefix, getTagNamespaceUri());
}
public void endDocument() throws SAXException {
contentWriter.endDocument();
}
public void printTagStartEnd(TAG tag,Attributes atts) throws SAXException {
printTagStart(tag,atts);
printTagEnd(tag);
}
public void printTagStartEnd(TAG tag) throws SAXException {
printTagStart(tag);
printTagEnd(tag);
}
public void printTagStart(TAG tag) throws SAXException {
printTagStart(tag,EMPTY_ATTRIBUTES);
}
public void printTagStart(TAG tag,Attributes atts) throws SAXException {
contentWriter.startElement (getTagNamespaceUri(), toTagString(tag), "", atts);
}
public void printTagEnd(TAG tag) throws SAXException {
contentWriter.endElement (getTagNamespaceUri(),toTagString(tag) , "");
}
private String toTagString(TAG tag) {
String result = tag.name();
if (result.startsWith("_"))
{
result = result.substring(1); // remove _
}
return result;
}
public void printTagCharacters(TAG tag,String text) throws SAXException {
printTagStart(tag);
if (text==null) {
text = " ";
}
printCharacters(text);
printTagEnd(tag);
}
public void printCharacters(String text) throws SAXException {
contentWriter.characters(text);
}
public void printComment(String text) throws SAXException {
contentWriter.comment(text);
}
}

View file

@ -1,76 +0,0 @@
/*
* 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.ext;
import java.io.Writer;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
/**
* ContentWriterXml writes SAX content handler events to XML.
*
* @author Willem Cazander
* @version 1.0 May 3, 2013
*/
public class ContentWriterXmlTag<T extends Enum<?>> extends ContentWriterXml implements ContentWriterTag<T> {
public ContentWriterXmlTag(Writer out,String encoding) {
super(out, encoding);
}
public String getTagNamespaceUri() {
return "";
}
public void printTagStartEnd(T tag,Attributes atts) throws SAXException {
printTagStart(tag,atts);
printTagEnd(tag);
}
public void printTagStartEnd(T tag) throws SAXException {
printTagStart(tag);
printTagEnd(tag);
}
public void printTagStart(T tag) throws SAXException {
printTagStart(tag,EMPTY_ATTRIBUTES);
}
public void printTagStart(T tag,Attributes atts) throws SAXException {
startElement (getTagNamespaceUri(), tag.name(), "", atts);
}
public void printTagEnd(T tag) throws SAXException {
endElement (getTagNamespaceUri(),tag.name() , "");
}
public void printTagCharacters(T tag,String text) throws SAXException {
printTagStart(tag);
if (text==null) {
text = " ";
}
characters(text);
printTagEnd(tag);
}
}

View file

@ -34,22 +34,21 @@ import org.xml.sax.helpers.AttributesImpl;
* @author Willem Cazander
* @version 1.0 May 3, 2013
*/
public class ContentWriterXsd extends ContentWriterXmlTag<ContentWriterXsd.Tag> {
public class ContentWriterXsd extends ContentWriterTagWrapper<ContentWriterXsd.Tag,ContentWriterXml> {
public ContentWriterXsd(Writer out,String encoding) {
super(out,encoding);
super(new ContentWriterXml(out, encoding),XMLConstants.XML_SCHEMA_NS_URI, XMLConstants.NULL_NS_URI);
}
public String getTagNamespaceUri() {
return XMLConstants.XML_SCHEMA_NS_URI;
public PropertyConfig getPropertyConfig() {
return getContentWriterWrapped().getPropertyConfig();
}
public void printXsdImport(String namespace,String schemaLocation) throws SAXException {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "namespace", "", "", namespace);
atts.addAttribute ("", "schemaLocation", "", "", schemaLocation);
startElement (getTagNamespaceUri(), "import", "", atts);
endElement (getTagNamespaceUri(), "import", ""); // import is keyword
printTagStartEnd(Tag._import, atts);
}
public void printXsdDocumentation(String description) throws SAXException {
@ -60,8 +59,8 @@ public class ContentWriterXsd extends ContentWriterXmlTag<ContentWriterXsd.Tag>
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "xml:lang", "", "", "en");
printTagStart(Tag.documentation,atts);
characters(description);
printTagEnd(Tag.documentation);
printCharacters(description);
printTagEnd(Tag.documentation);
printTagEnd(Tag.annotation);
}
@ -70,16 +69,15 @@ public class ContentWriterXsd extends ContentWriterXmlTag<ContentWriterXsd.Tag>
atts.addAttribute ("", "name", "", "", name);
atts.addAttribute ("", "type", "", "", type);
printTagStart(Tag.attribute,atts);
printXsdDocumentation(description);
printXsdDocumentation(description);
printTagEnd(Tag.attribute);
}
public enum Tag {
all,annotation,any,anyAttribute,appinfo,attribute,attributeGroup,
choise,complexContent,complexType,documentation,element,extension,
field,group,/*_import,*/include,key,keyref,list,notation,
field,group,_import,include,key,keyref,list,notation,
redefine,restriction,schema,selector,sequence,
simpleContent,simpleType,unoin,unique
}
}