From: <lh...@us...> - 2008-11-14 13:39:26
|
Revision: 190 http://tinytim.svn.sourceforge.net/tinytim/?rev=190&view=rev Author: lheuer Date: 2008-11-14 13:39:21 +0000 (Fri, 14 Nov 2008) Log Message: ----------- - Fixed #2276392 -- Prettify XML (XTM) output - XTM10Writer didn't used xlink:href for topicRefs. Fixed. - Added logger to XTM10Writer / XTM20Writer - Associations with no roles are omitted now (1.0 and 2.0) Modified Paths: -------------- tinytim-mio/trunk/src/main/java/org/tinytim/mio/XMLWriter.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/XTM10Writer.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/XTM20Writer.java Modified: tinytim-mio/trunk/src/main/java/org/tinytim/mio/XMLWriter.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/XMLWriter.java 2008-11-14 13:35:18 UTC (rev 189) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/XMLWriter.java 2008-11-14 13:39:21 UTC (rev 190) @@ -39,6 +39,9 @@ private final String _encoding; + private int _depth; + + public XMLWriter(OutputStream out) throws IOException { this(out, "utf-8"); } @@ -55,18 +58,18 @@ _out.write("<?xml version=\"1.0\" encoding=\""); _out.write(_encoding); _out.write("\" standalone=\"yes\"?>"); - newline(); + _newline(); _out.write("<!-- Generated by tinyTiM v"); _out.write(Version.RELEASE); _out.write(" - http://tinytim.sourceforge.net/ -->"); - newline(); + _newline(); } /** * @see org.xml.sax.DocumentHandler#endDocument() */ public void endDocument() throws IOException { - newline(); + _newline(); try { _out.flush(); } @@ -83,22 +86,43 @@ * @see org.xml.sax.DocumentHandler#startElement(java.lang.String, org.xml.sax.AttributeList) */ public void startElement(String name, Attributes attrs) throws IOException { + if (_depth > 0) { + _newline(); + } + _indent(); _out.write('<'); _out.write(name); _writeAttributes(attrs); _out.write('>'); + _depth++; } /** * @see org.xml.sax.DocumentHandler#endElement(java.lang.String) */ public void endElement(String name) throws IOException { + _endElement(name, true); + } + + /** + * @see org.xml.sax.DocumentHandler#endElement(java.lang.String) + */ + public void _endElement(String name, boolean indent) throws IOException { + _depth--; + if (indent && _depth >= 0) { + _newline(); + _indent(); + } _out.write("</"); _out.write(name); _out.write('>'); } public void emptyElement(String name, Attributes attrs) throws IOException { + if (_depth > 0) { + _newline(); + } + _indent(); _out.write('<'); _out.write(name); _writeAttributes(attrs); @@ -112,7 +136,7 @@ public void dataElement(String name, Attributes attrs, String data) throws IOException { startElement(name, attrs); characters(data); - endElement(name); + _endElement(name, false); } public void _writeAttributes(Attributes attrs) throws IOException { @@ -132,10 +156,18 @@ * * @throws IOException If an error occurs. */ - public void newline() throws IOException { + private void _newline() throws IOException { _out.write(_NL); } + private void _indent() throws IOException { + char[] indent = new char[_depth*2]; + for (int i=0; i<indent.length; i++) { + indent[i] = ' '; + } + _out.write(indent); + } + /** * Writes the specified characters to the output. * Modified: tinytim-mio/trunk/src/main/java/org/tinytim/mio/XTM10Writer.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/XTM10Writer.java 2008-11-14 13:35:18 UTC (rev 189) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/XTM10Writer.java 2008-11-14 13:39:21 UTC (rev 190) @@ -18,6 +18,7 @@ import java.io.IOException; import java.io.OutputStream; import java.util.Set; +import java.util.logging.Logger; import org.tinytim.internal.api.IScope; import org.tinytim.internal.api.IScoped; @@ -48,8 +49,9 @@ */ public class XTM10Writer extends AbstractXTMWriter { + private static final Logger LOG = Logger.getLogger(XTM10Writer.class.getName()); + //TODO: Export iids, - // warn if len(slos) > 1, // warn if name.type != default name type, // warn if datatype not in (xsd:string, xsd:anyURI) @@ -120,50 +122,45 @@ _out.startElement("topic", _attrs); _writeIdentities(topic); for (Topic type: topic.getTypes()) { - _out.newline(); _out.startElement("instanceOf"); _writeTopicRef(type); _out.endElement("instanceOf"); - _out.newline(); } for (Name name: topic.getNames()) { - _out.newline(); _writeName(name); - _out.newline(); } for (Occurrence occ: topic.getOccurrences()) { - _out.newline(); _writeOccurrence(occ); - _out.newline(); } _out.endElement("topic"); - _out.newline(); } protected void _writeAssociation(final Association assoc) throws IOException { + Set<Role> roles = assoc.getRoles(); + if (roles.isEmpty()) { + LOG.info("Omitting association id " + assoc.getId() + " since it has no roles"); + return; + } _attrs.clear(); _addId(_attrs, assoc); _out.startElement("association", _attrs); _writeType(assoc); _writeScope(assoc); - for (Role role: assoc.getRoles()) { + for (Role role: roles) { _writeRole(role); } _out.endElement("association"); - _out.newline(); } protected void _writeRole(final Role role) throws IOException { _attrs.clear(); _addId(_attrs, role); - _out.newline(); _out.startElement("member", _attrs); _out.startElement("roleSpec"); _writeTopicRef(role.getType()); _out.endElement("roleSpec"); _writeTopicRef(role.getPlayer()); _out.endElement("member"); - _out.newline(); } protected void _writeName(final Name name) throws IOException { @@ -173,9 +170,7 @@ _writeScope(name); _out.dataElement("baseNameString", name.getValue()); for (Variant variant: name.getVariants()) { - _out.newline(); _writeVariant(variant); - _out.newline(); } _out.endElement("baseName"); } @@ -184,13 +179,11 @@ _attrs.clear(); _addId(_attrs, variant); _out.startElement("variant", _attrs); - _out.newline(); _out.startElement("parameters"); for (Topic theme: variant.getScope()) { _writeTopicRef(theme); } _out.endElement("parameters"); - _out.newline(); _writeDatatypeAware(variant); _out.endElement("variant"); } @@ -218,18 +211,14 @@ private void _writeTopicRef(final Topic topic) throws IOException { _attrs.clear(); - _attrs.addAttribute("", "href", "", "CDATA", "#" + _getId(topic)); - _out.newline(); + _attrs.addAttribute("", "xlink:href", "", "CDATA", "#" + _getId(topic)); _out.emptyElement("topicRef", _attrs); - _out.newline(); } private void _writeType(final Typed typed) throws IOException { - _out.newline(); _out.startElement("type"); _writeTopicRef(typed.getType()); _out.endElement("type"); - _out.newline(); } private void _writeScope(final Scoped scoped) throws IOException { @@ -237,13 +226,11 @@ if (scope.isUnconstrained()) { return; } - _out.newline(); _out.startElement("scope"); for (Topic theme: scope) { _writeTopicRef(theme); } _out.endElement("scope"); - _out.newline(); } protected void _writeIdentities(final Topic topic) throws IOException { @@ -256,8 +243,10 @@ return; } _out.startElement("subjectIdentity"); - _out.newline(); if (!slos.isEmpty()) { + if (slos.size() > 1) { + LOG.warning("The topic " + topic.getId() + " has more than one subject locator, exporting just one"); + } // Choose one subject locator Locator slo = slos.iterator().next(); _attrs.clear(); @@ -275,7 +264,6 @@ _out.emptyElement("subjectIndicatorRef", _attrs); } _out.endElement("subjectIdentity"); - _out.newline(); } } Modified: tinytim-mio/trunk/src/main/java/org/tinytim/mio/XTM20Writer.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/XTM20Writer.java 2008-11-14 13:35:18 UTC (rev 189) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/XTM20Writer.java 2008-11-14 13:39:21 UTC (rev 190) @@ -18,6 +18,7 @@ import java.io.IOException; import java.io.OutputStream; import java.util.Set; +import java.util.logging.Logger; import org.tinytim.internal.api.IConstruct; import org.tinytim.internal.api.IScope; @@ -53,6 +54,8 @@ */ public class XTM20Writer extends AbstractXTMWriter { + private static final Logger LOG = Logger.getLogger(XTM20Writer.class.getName()); + /** * Creates a XTM 2.0 writer using "utf-8" encoding. * @@ -92,11 +95,9 @@ _writeItemIdentifiers(topicMap); for (Topic topic: topicMap.getTopics()) { _writeTopic(topic); - _out.newline(); } for (Association assoc: topicMap.getAssociations()) { _writeAssociation(assoc); - _out.newline(); } _out.endElement("topicMap"); _out.endDocument(); @@ -116,23 +117,25 @@ } for (Name name: topic.getNames()) { _writeName(name); - _out.newline(); } for (Occurrence occ: topic.getOccurrences()) { _writeOccurrence(occ); - _out.newline(); } _out.endElement("topic"); } protected void _writeAssociation(final Association assoc) throws IOException { + Set<Role> roles = assoc.getRoles(); + if (roles.isEmpty()) { + LOG.info("Omitting association id " + assoc.getId() + " since it has no roles"); + return; + } _out.startElement("association", _reifier(assoc)); _writeItemIdentifiers(assoc); _writeType(assoc); _writeScope(assoc); - for (Role role: assoc.getRoles()) { + for (Role role: roles) { _writeRole(role); - _out.newline(); } _out.endElement("association"); } @@ -142,7 +145,6 @@ _writeItemIdentifiers(role); _writeType(role); _writeTopicRef(role.getPlayer()); - _out.newline(); _out.endElement("role"); } @@ -154,7 +156,6 @@ _out.dataElement("value", name.getValue()); for (Variant variant: name.getVariants()) { _writeVariant(variant); - _out.newline(); } _out.endElement("name"); } @@ -178,7 +179,6 @@ private void _writeDatatypeAware(final DatatypeAware datatyped) throws IOException { _attrs.clear(); - _out.newline(); if (XSD.ANY_URI.equals(datatyped.getDatatype())) { _attrs.addAttribute("", "href", "", "CDATA", datatyped.locatorValue().toExternalForm()); _out.emptyElement("resourceRef", _attrs); @@ -190,7 +190,6 @@ } _out.dataElement("resourceData", _attrs, datatyped.getValue()); } - _out.newline(); } /** @@ -226,11 +225,9 @@ && type.getSubjectIdentifiers().contains(TMDM.TOPIC_NAME)) { return; } - _out.newline(); _out.startElement("type"); _writeTopicRef(type); _out.endElement("type"); - _out.newline(); } private void _writeScope(final Scoped scoped) throws IOException { @@ -238,13 +235,11 @@ if (scope.isUnconstrained()) { return; } - _out.newline(); _out.startElement("scope"); for (Topic theme: scope) { _writeTopicRef(theme); } _out.endElement("scope"); - _out.newline(); } private void _writeItemIdentifiers(final Construct construct) throws IOException { @@ -255,9 +250,7 @@ for (Locator loc: locs) { _attrs.clear(); _attrs.addAttribute("", "href", "", "CDATA", loc.toExternalForm()); - _out.newline(); _out.emptyElement(name, _attrs); - _out.newline(); } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |