MATSIM
ObjectAttributesXmlReader.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 static org.matsim.utils.objectattributes.ObjectAttributesXmlWriter.ATTR_ATTRIBUTECLASS;
23 import static org.matsim.utils.objectattributes.ObjectAttributesXmlWriter.ATTR_ATTRIBUTENAME;
24 import static org.matsim.utils.objectattributes.ObjectAttributesXmlWriter.ATTR_OBJECTID;
25 import static org.matsim.utils.objectattributes.ObjectAttributesXmlWriter.TAG_ATTRIBUTE;
27 
28 import java.util.Map;
29 import java.util.Stack;
30 
31 import com.google.inject.Inject;
32 import org.apache.logging.log4j.LogManager;
33 import org.apache.logging.log4j.Logger;
35 import org.xml.sax.Attributes;
36 import org.xml.sax.SAXException;
37 
47  private final static Logger log = LogManager.getLogger(ObjectAttributesXmlReader.class);
50  private boolean readCharacters = false;
51  private String currentObject = null;
52  private String currentAttribute = null;
53  private String currentAttributeClass = null;
54  private long count = 0;
55 
56 
57  public ObjectAttributesXmlReader(final ObjectAttributes attributes) {
59  this.attributes = attributes;
60  super.setValidating(false);
61  }
62 
63  @Override
64  public void startTag(String name, Attributes atts, Stack<String> context) {
65  if (TAG_ATTRIBUTE.equals(name)) {
66  this.currentAttribute = atts.getValue(ATTR_ATTRIBUTENAME);
67  this.currentAttributeClass = atts.getValue(ATTR_ATTRIBUTECLASS);
68  this.readCharacters = true;
69  } else if (TAG_OBJECT.equals(name)) {
70  this.currentObject = atts.getValue(ATTR_OBJECTID);
71  }
72  }
73 
74  @Override
75  public void endTag(String name, String content, Stack<String> context) {
76  if (TAG_ATTRIBUTE.equals(name)) {
77  this.readCharacters = false;
78 
79  Object o = converter.convert(this.currentAttributeClass, content);
80  this.attributes.putAttribute(this.currentObject, this.currentAttribute, o);
81  } else if (TAG_OBJECT.equals(name)) {
82  if (this.count % 100000 == 0) {
83  log.info("reading object #" + this.count);
84  }
85  this.count++;
86  this.currentObject = null;
87  }
88  }
89 
90  @Override
91  public void characters(char[] ch, int start, int length) throws SAXException {
92  if (this.readCharacters) {
93  super.characters(ch, start, length);
94  }
95  // ignore characters to prevent OutOfMemoryExceptions
96  /* non-validating files contain empty tags with attributes,
97  * but without the dtd or schema, all whitespace between tags is handled
98  * by characters and added up by super.characters, consuming huge
99  * amount of memory when large files are read in.
100  */
101  }
102 
110  public AttributeConverter<?> putAttributeConverter(final Class<?> clazz, final AttributeConverter<?> converter) {
111  return this.converter.putAttributeConverter(clazz, converter);
112  }
113 
114  @Inject
115  public void putAttributeConverters( final Map<Class<?>, AttributeConverter<?>> converters ) {
116  this.converter.putAttributeConverters(converters);
117  }
118 
125  public AttributeConverter<?> removeAttributeConverter(final Class<?> clazz) {
126  return this.converter.removeAttributeConverter(clazz);
127  }
128 
129 }
Object putAttribute(final String objectId, final String attribute, final Object value)
void endTag(String name, String content, Stack< String > context)
void putAttributeConverters(final Map< Class<?>, AttributeConverter<?>> converters)
void startTag(String name, Attributes atts, Stack< String > context)
AttributeConverter<?> putAttributeConverter(final Class<?> clazz, final AttributeConverter<?> converter)
AttributeConverter<?> removeAttributeConverter(final Class<?> clazz)
void putAttributeConverters(final Map< Class<?>, AttributeConverter<?>> converters)