public final void writeAttributes(final String indentation, final BufferedWriter writer, final Attributes attributes) { if (attributes.size() == 0) { return; } try { writer.write(indentation); writer.write("<attributes>"); writer.newLine(); // write attributes for (Map.Entry<String, Object> objAttribute : attributes.getAsMap().entrySet()) { Class<?> clazz = objAttribute.getValue().getClass(); String converted = converter.convertToString(objAttribute.getValue()); if (converted != null) { writer.write(indentation + "\t"); writer.write("<attribute name=\"" + XmlUtils.encodeAttributeValue(objAttribute.getKey()) + "\" "); writer.write("class=\"" + clazz.getCanonicalName() + "\" >"); writer.write(XmlUtils.encodeContent(converted)); writer.write("</attribute>"); writer.newLine(); } } writer.write(indentation); writer.write("</attributes>"); writer.newLine(); } catch (IOException e) { throw new UncheckedIOException(e); } }
protected final void writeStartTag(String tagname, List<Tuple<String, String>> attributes, boolean closeElement) throws UncheckedIOException { try { if (doPrettyPrint) { this.writer.write(NL); indent(); this.indentationLevel++; } this.writer.write("<" + tagname); if (attributes != null) { for (Tuple<String, String> t : attributes){ this.writer.write(" " + t.getFirst() + "=\"" + encodeAttributeValue(t.getSecond()) + "\""); } } if (closeElement) { this.writer.write("/>"); if (doPrettyPrint) { this.indentationLevel--; } } else { this.writer.write(">"); } } catch (IOException e) { throw new UncheckedIOException(e); } }
protected final void writeContent(String content, boolean allowWhitespaces) throws UncheckedIOException{ try { if (doPrettyPrint) { this.noWhitespaces = !allowWhitespaces; if (!this.noWhitespaces) { this.writer.write(NL); this.indentationLevel++; indent(); } } writer.write(encodeContent(content)); if (!this.noWhitespaces) { this.indentationLevel--; } } catch (IOException e) { throw new UncheckedIOException(e); } }
@Test public void testEncodeAttributeValue() { MatsimXmlWriter writer = new MatsimXmlWriter() { }; Assert.assertEquals("hello world!", XmlUtils.encodeAttributeValue("hello world!")); Assert.assertEquals("you & me", XmlUtils.encodeAttributeValue("you & me")); Assert.assertEquals("you & me & her", XmlUtils.encodeAttributeValue("you & me & her")); Assert.assertEquals("tick " tack", XmlUtils.encodeAttributeValue("tick \" tack")); Assert.assertEquals("tick " tack " tock", XmlUtils.encodeAttributeValue("tick \" tack \" tock")); Assert.assertEquals("this & that " these & those", XmlUtils.encodeAttributeValue("this & that \" these & those")); Assert.assertEquals("tick < tack > tock", XmlUtils.encodeAttributeValue("tick < tack > tock")); }
@Test public void testEncodedContent() { MatsimXmlWriter writer = new MatsimXmlWriter() { }; Assert.assertEquals("hello world!", XmlUtils.encodeContent("hello world!")); Assert.assertEquals("you & me", XmlUtils.encodeContent("you & me")); Assert.assertEquals("you & me & her", XmlUtils.encodeContent("you & me & her")); Assert.assertEquals("tick \" tack", XmlUtils.encodeContent("tick \" tack")); Assert.assertEquals("tick \" tack \" tock", XmlUtils.encodeContent("tick \" tack \" tock")); Assert.assertEquals("this & that \" these & those", XmlUtils.encodeContent("this & that \" these & those")); Assert.assertEquals("tick < tack > tock", XmlUtils.encodeContent("tick < tack > tock")); }