MATSIM
AttributesXmlReaderDelegate.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * AttributesXmlReaderDelegate.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2019 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21  package org.matsim.utils.objectattributes.attributable;
22 
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Stack;
26 
27 import org.matsim.core.gbl.Gbl;
30 
37  private final Map<String, String> stringCache = new HashMap<>(1000);
38 
39  private Attributes currentAttributes = null;
40  private String currentAttribute = null;
41  private String currentAttributeClass = null;
42 
43  /*package*/ public final static String TAG_ATTRIBUTES = "attributes";
44  /*package*/ public final static String TAG_ATTRIBUTE = "attribute";
45  /*package*/ final static String ATTR_ATTRIBUTENAME = "name";
46  /*package*/ final static String ATTR_ATTRIBUTECLASS = "class";
47 
49  {
50  return this.converter;
51  }
52 
53  public void startTag(String name,
54  org.xml.sax.Attributes atts,
55  Stack<String> context,
56  Attributes currentAttributes ) {
57  if (TAG_ATTRIBUTE.equals(name)) {
58  String attributeName = atts.getValue(ATTR_ATTRIBUTENAME);
59  this.currentAttribute = this.stringCache.computeIfAbsent(attributeName, k -> attributeName);
60  this.currentAttributeClass = atts.getValue(ATTR_ATTRIBUTECLASS);
61  } else if (TAG_ATTRIBUTES.equals(name)) {
62  this.currentAttributes = currentAttributes;
63  }
64  }
65 
66  public void endTag(String name, String content, Stack<String> context) {
67  if (TAG_ATTRIBUTE.equals(name)) {
68  Object o = converter.convert(this.currentAttributeClass, content);
69  if (o == null) return;
70  Gbl.assertNotNull( this.currentAttributes );
71  this.currentAttributes.putAttribute( this.currentAttribute, o);
72  }
73  }
74 
76  return currentAttributes;
77  }
78 
86  public AttributeConverter<?> putAttributeConverter(final Class<?> clazz, final AttributeConverter<?> converter) {
87  return this.converter.putAttributeConverter(clazz, converter);
88  }
89 
90  public void putAttributeConverters( final Map<Class<?>, AttributeConverter<?>> converters ) {
91  for ( Map.Entry<Class<?>, AttributeConverter<?>> e : converters.entrySet() ) {
92  putAttributeConverter( e.getKey() , e.getValue() );
93  }
94  }
95 
102  public AttributeConverter<?> removeAttributeConverter(final Class<?> clazz) {
103  return this.converter.removeAttributeConverter(clazz);
104  }
105 }
Object putAttribute(final String attribute, final Object value)
static void assertNotNull(Object obj)
Definition: Gbl.java:212
void putAttributeConverters(final Map< Class<?>, AttributeConverter<?>> converters)
AttributeConverter<?> putAttributeConverter(final Class<?> clazz, final AttributeConverter<?> converter)
void startTag(String name, org.xml.sax.Attributes atts, Stack< String > context, Attributes currentAttributes)