MATSIM
ObjectAttributesXmlWriter.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2010 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package org.matsim.utils.objectattributes;
21 
22 import java.io.UncheckedIOException;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.TreeMap;
27 
28 import com.google.inject.Inject;
29 import org.apache.logging.log4j.LogManager;
30 import org.apache.logging.log4j.Logger;
33 
40 
41  private final static Logger log = LogManager.getLogger(ObjectAttributesXmlWriter.class);
42 
43  /*package*/ final static String TAG_OBJECT_ATTRIBUTES = "objectAttributes";
44  /*package*/ final static String TAG_OBJECT = "object";
45  /*package*/ final static String TAG_ATTRIBUTE = "attribute";
46  /*package*/ final static String ATTR_OBJECTID = "id";
47  /*package*/ final static String ATTR_ATTRIBUTENAME = "name";
48  /*package*/ final static String ATTR_ATTRIBUTECLASS = "class";
49 
50 
51 
54 
55  public ObjectAttributesXmlWriter(final ObjectAttributes attributes) {
56  this.attributes = attributes;
57  }
58 
59  public void writeFile(final String filename) throws UncheckedIOException {
60  openFile(filename);
61  writeXmlHead();
62  writeDoctype(TAG_OBJECT_ATTRIBUTES, "http://matsim.org/files/dtd/objectattributes_v1.dtd");
63  writeStartTag(TAG_OBJECT_ATTRIBUTES, null);
64  List<Tuple<String, String>> xmlAttributes = new LinkedList<Tuple<String, String>>();
65  for (Map.Entry<String, Map<String, Object>> entry : this.attributes.attributes.entrySet()) {
66  xmlAttributes.add(super.createTuple(ATTR_OBJECTID, entry.getKey()));
67  writeStartTag(TAG_OBJECT, xmlAttributes);
68  xmlAttributes.clear();
69  // sort attributes by name
70  Map<String, Object> objAttributes = new TreeMap<String, Object>();
71  objAttributes.putAll(entry.getValue());
72  // write attributes
73  for (Map.Entry<String, Object> objAttribute : objAttributes.entrySet()) {
74  Class<?> clazz = objAttribute.getValue().getClass();
75  String value = converter.convertToString(objAttribute.getValue());
76  if (value != null) {
77  xmlAttributes.add(super.createTuple(ATTR_ATTRIBUTENAME, objAttribute.getKey()));
78  xmlAttributes.add(super.createTuple(ATTR_ATTRIBUTECLASS, clazz.getCanonicalName()));
79  writeStartTag(TAG_ATTRIBUTE, xmlAttributes);
80  xmlAttributes.clear();
81  writeContent(value, false);
82  writeEndTag(TAG_ATTRIBUTE);
83  }
84  }
85  writeEndTag(TAG_OBJECT);
86  }
87  writeEndTag(TAG_OBJECT_ATTRIBUTES);
88  close();
89  }
90 
98  public AttributeConverter putAttributeConverter(final Class<?> clazz, final AttributeConverter converter) {
99  return this.converter.putAttributeConverter(clazz, converter);
100  }
101 
102  @Inject
103  public void putAttributeConverters( final Map<Class<?>, AttributeConverter<?>> converters ) {
104  this.converter.putAttributeConverters(converters);
105  }
106 
113  public AttributeConverter removeAttributeConverter(final Class<?> clazz) {
114  return this.converter.removeAttributeConverter(clazz);
115  }
116 
117 }
void putAttributeConverters(final Map< Class<?>, AttributeConverter<?>> converters)
final void writeContent(String content, boolean allowWhitespaces)
final void writeStartTag(String tagname, List< Tuple< String, String >> attributes)
AttributeConverter putAttributeConverter(final Class<?> clazz, final AttributeConverter converter)
void putAttributeConverters(final Map< Class<?>, AttributeConverter<?>> converters)
final void writeDoctype(String rootTag, String dtdUrl)