21 package org.matsim.facilities;
24 import java.util.Stack;
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
39 import org.xml.sax.Attributes;
47 final class FacilitiesReaderMatsimV1
extends MatsimXmlParser {
48 private static final Logger log = LogManager.getLogger(FacilitiesReaderMatsimV1.class);
50 private final static String FACILITIES =
"facilities";
51 private final static String FACILITY =
"facility";
52 private final static String ACTIVITY =
"activity";
53 private final static String CAPACITY =
"capacity";
54 private final static String OPENTIME =
"opentime";
55 private static final String ATTRIBUTES =
"attributes";
56 private static final String ATTRIBUTE =
"attribute";
58 private final ActivityFacilities facilities;
59 private final ActivityFacilitiesFactory factory;
60 private final AttributesXmlReaderDelegate attributesReader =
new AttributesXmlReaderDelegate();
61 private ActivityFacility currfacility = null;
62 private ActivityOption curractivity = null;
65 private final String externalInputCRS;
66 private final String targetCRS;
67 private CoordinateTransformation coordinateTransformation =
new IdentityTransformation();
69 FacilitiesReaderMatsimV1(
70 final String externalInputCRS,
71 final String targetCRS,
72 final ActivityFacilities facilities) {
73 super(ValidationType.DTD_ONLY);
74 this.externalInputCRS = externalInputCRS;
75 this.targetCRS = targetCRS;
76 this.facilities = facilities;
77 this.factory = this.facilities.getFactory();
78 if (externalInputCRS != null && targetCRS != null) {
79 this.coordinateTransformation = TransformationFactory.getCoordinateTransformation(externalInputCRS, targetCRS);
80 ProjectionUtils.putCRS(this.facilities, targetCRS);
84 public void putAttributeConverter(Class<?> clazz, AttributeConverter<?> converter) {
85 this.attributesReader.putAttributeConverter(clazz, converter);
88 public void putAttributeConverters(Map<Class<?>, AttributeConverter<?>> converters) {
89 this.attributesReader.putAttributeConverters(converters);
93 public void startTag(
final String name,
final org.xml.sax.Attributes atts,
final Stack<String> context) {
94 if (FACILITIES.equals(name)) {
95 startFacilities(atts);
96 }
else if (FACILITY.equals(name)) {
98 }
else if (ACTIVITY.equals(name)) {
100 }
else if (CAPACITY.equals(name)) {
102 }
else if (OPENTIME.equals(name)) {
104 }
else if (ATTRIBUTE.equals(name)) {
105 this.attributesReader.startTag(name, atts, context, this.currAttributes);
106 }
else if (ATTRIBUTES.equals(name)) {
107 currAttributes = context.peek().equals(FACILITIES) ? this.facilities.getAttributes() : this.currfacility.getAttributes();
108 attributesReader.startTag(name, atts, context, currAttributes);
113 public void endTag(
final String name,
final String content,
final Stack<String> context) {
114 if (FACILITY.equals(name)) {
115 this.facilities.addActivityFacility(this.currfacility);
116 this.currfacility = null;
117 }
else if (ACTIVITY.equals(name)) {
118 this.curractivity = null;
119 }
else if (ATTRIBUTES.equalsIgnoreCase(name)) {
120 if (context.peek().equals(FACILITIES)) {
121 String inputCRS = (String) currAttributes.getAttribute(ProjectionUtils.INPUT_CRS_ATT);
123 if (inputCRS != null && targetCRS != null) {
124 if (externalInputCRS != null) {
126 log.warn(
"coordinate transformation defined both in config and in input file: setting from input file will be used");
128 coordinateTransformation = TransformationFactory.getCoordinateTransformation(inputCRS, targetCRS);
129 currAttributes.putAttribute(ProjectionUtils.INPUT_CRS_ATT, targetCRS);
132 this.currAttributes = null;
133 }
else if (ATTRIBUTE.equalsIgnoreCase(name)) {
134 this.attributesReader.endTag(name, content, context);
138 private void startFacilities(
final Attributes atts) {
139 this.facilities.setName(atts.getValue(
"name"));
140 this.currAttributes = facilities.getAttributes();
141 if (atts.getValue(
"aggregation_layer") != null) {
142 LogManager.getLogger(FacilitiesReaderMatsimV1.class).warn(
"aggregation_layer is deprecated.");
146 private void startFacility(
final Attributes atts) {
147 if ( atts.getValue(
"x") !=null && atts.getValue(
"y") !=null ) {
148 if (atts.getValue(
"linkId") !=null) {
150 this.factory.createActivityFacility(
151 Id.create(atts.getValue(
"id"), ActivityFacility.class),
152 coordinateTransformation.transform(
154 Double.parseDouble(atts.getValue(
"x")),
155 Double.parseDouble(atts.getValue(
"y")))),
156 Id.create(atts.getValue(
"linkId"),Link.class));
159 this.factory.createActivityFacility(
160 Id.create(atts.getValue(
"id"), ActivityFacility.class),
161 coordinateTransformation.transform(
163 Double.parseDouble(atts.getValue(
"x")),
164 Double.parseDouble(atts.getValue(
"y")))));
167 if (atts.getValue(
"linkId") !=null) {
169 this.factory.createActivityFacility(
170 Id.create(atts.getValue(
"id"), ActivityFacility.class),
171 Id.create(atts.getValue(
"linkId"),Link.class));
173 throw new RuntimeException(
"Neither coordinate nor linkId are available for facility id "+ atts.getValue(
"id")+
". Aborting....");
177 ((ActivityFacilityImpl) this.currfacility).setDesc(atts.getValue(
"desc"));
180 private void startActivity(
final Attributes atts) {
181 this.curractivity = this.factory.createActivityOption(atts.getValue(
"type"));
182 this.currfacility.addActivityOption(this.curractivity);
185 private void startCapacity(
final Attributes atts) {
186 double cap = Double.parseDouble(atts.getValue(
"value"));
187 this.curractivity.setCapacity(cap);
190 private void startOpentime(
final Attributes atts) {
191 this.curractivity.addOpeningTime(OpeningTimeImpl.createFromOptionalTimes(
192 Time.parseOptionalTime(atts.getValue(
"start_time")),
193 Time.parseOptionalTime(atts.getValue(
"end_time"))));
abstract void startTag(String name, Attributes atts, Stack< String > context)