MATSIM
ConfigReaderMatsimV2.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * NonFlatConfigReader.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2013 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 package org.matsim.core.config;
21 
22 import static org.matsim.core.config.ConfigV2XmlNames.MODULE;
23 import static org.matsim.core.config.ConfigV2XmlNames.NAME;
24 import static org.matsim.core.config.ConfigV2XmlNames.PARAMETER;
25 import static org.matsim.core.config.ConfigV2XmlNames.PARAMETER_SET;
26 import static org.matsim.core.config.ConfigV2XmlNames.TYPE;
27 import static org.matsim.core.config.ConfigV2XmlNames.VALUE;
28 
29 import java.util.ArrayDeque;
30 import java.util.Deque;
31 import java.util.Stack;
32 
33 import org.apache.logging.log4j.LogManager;
34 import org.apache.logging.log4j.Logger;
36 import org.xml.sax.Attributes;
37 
42 class ConfigReaderMatsimV2 extends MatsimXmlParser {
43  private final static Logger LOG = LogManager.getLogger(ConfigReaderMatsimV2.class);
44 
45  private final Config config;
46 
47  private final ConfigAliases aliases;
48  private final Deque<ConfigGroup> moduleStack = new ArrayDeque<>();
49  private final Deque<String> pathStack = new ArrayDeque<>();
50 
51  ConfigReaderMatsimV2(final Config config) {
52  super(ValidationType.DTD_ONLY);
53  this.config = config;
54  this.aliases = new ConfigAliases();
55  }
56 
57  ConfigReaderMatsimV2(final Config config, final ConfigAliases aliases) {
58  super(ValidationType.DTD_ONLY);
59  this.config = config;
60  this.aliases = aliases;
61  }
62 
63  public ConfigAliases getConfigAliases() {
64  return this.aliases;
65  }
66 
67  @Override
68  public void startTag(
69  final String name,
70  final Attributes atts,
71  final Stack<String> context) {
72 
73  if ( name.equals( MODULE ) ) {
74  startModule(atts);
75  }
76  else if ( name.equals( PARAMETER_SET ) ) {
77  startParameterSet(atts);
78  }
79  else if ( name.equals( PARAMETER ) ) {
80  startParameter(atts);
81  }
82  else if ( !name.equals( "config" ) ) {
83  // this is the job of the dtd validation,
84  // but better too much safety than too little...
85  throw new IllegalArgumentException( "unknown tag "+name );
86  }
87  }
88 
89  private void startParameter(final Attributes atts) {
90  String name = this.aliases.resolveAlias(atts.getValue(NAME), this.pathStack);
91  this.moduleStack.getFirst().addParam(
92  name,
93  atts.getValue( VALUE ) );
94  }
95 
96  private void startParameterSet(final Attributes atts) {
97  String type = this.aliases.resolveAlias(atts.getValue(TYPE), this.pathStack);
98  final ConfigGroup m = this.moduleStack.getFirst().createParameterSet( type );
99  this.moduleStack.addFirst(m);
100  this.pathStack.addFirst(m.getName());
101  }
102 
103  private void startModule(final Attributes atts) {
104  String name = this.aliases.resolveAlias(atts.getValue(NAME), this.pathStack);
105  ConfigGroup m = this.config.getModule(name);
106  if (m == null) {
107  m = this.config.createModule(name);
108  }
109  this.moduleStack.addFirst(m);
110  this.pathStack.addFirst(m.getName());
111  }
112 
113  @Override
114  public void endTag(
115  final String name,
116  final String content,
117  final Stack<String> context) {
118  if ( name.equals( MODULE ) || name.equals( PARAMETER_SET ) ) {
119  final ConfigGroup head = this.moduleStack.removeFirst();
120  this.pathStack.removeFirst();
121 
122  if ( !this.moduleStack.isEmpty() ) this.moduleStack.getFirst().addParameterSet( head );
123  }
124  }
125 
126 }
127