MATSIM
AbstractHouseholdsReaderV10.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * AbstractHouseholdsReaderV10
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2012 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.households;
21 
22 import java.util.*;
23 
24 import com.google.inject.Inject;
25 import org.matsim.api.core.v01.Id;
32 import org.matsim.vehicles.Vehicle;
33 import org.xml.sax.Attributes;
34 
39 abstract class AbstractHouseholdsReaderV10 extends MatsimXmlParser{
40 
41  private List<Id<Person>> currentmembers = null;
42 
43  private Income currentincome = null;
44 
45  private HouseholdsFactory builder = null;
46 
47  private Id<Household> currentHhId = null;
48 
49  private List<Id<Vehicle>> currentVehicleIds = null;
50 
51  private IncomePeriod currentIncomePeriod;
52 
53  private String currentincomeCurrency;
54 
55  private final Households households;
56 
57  private Counter counter = new Counter(" households # ");
58 
59  private Household currentHousehold = null;
60 
61  private final AttributesXmlReaderDelegate attributesReader = new AttributesXmlReaderDelegate();
62  private org.matsim.utils.objectattributes.attributable.Attributes currAttributes =
64 
65  public AbstractHouseholdsReaderV10(Households households) {
66  super(ValidationType.XSD_ONLY);
67  if (households == null) {
68  throw new IllegalArgumentException("Container for households must not be null!");
69  }
70  this.households = households;
71  this.builder = households.getFactory();
72  }
73 
74  public void putAttributeConverter( final Class<?> clazz , AttributeConverter<?> converter ) {
75  attributesReader.putAttributeConverter( clazz , converter );
76  }
77 
78  @Inject
79  public void putAttributeConverters( final Map<Class<?>, AttributeConverter<?>> converters ) {
80  attributesReader.putAttributeConverters( converters );
81  }
82 
83  @Override
84  public void endTag(String name, String content, Stack<String> context) {
85  if (HouseholdsSchemaV10Names.HOUSEHOLD.equalsIgnoreCase(name)) {
86  fillHousehold();
87  ((HouseholdsImpl)this.households).addHousehold(currentHousehold);
88  this.currentHousehold = null;
89  counter.incCounter();
90  }
91  else if (HouseholdsSchemaV10Names.INCOME.equalsIgnoreCase(name)) {
92  this.currentincome = this.builder.createIncome(Double.parseDouble(content.trim()), this.currentIncomePeriod);
93  this.currentincome.setCurrency(this.currentincomeCurrency);
94  }
95  else if (HouseholdsSchemaV10Names.HOUSEHOLDS.equalsIgnoreCase(name)) {
96  counter.printCounter();
97  }
98  else if (name.equalsIgnoreCase(HouseholdsSchemaV10Names.ATTRIBUTES)) {
99  this.currAttributes = null;
100  }
101  else if (name.equalsIgnoreCase(HouseholdsSchemaV10Names.ATTRIBUTE)) {
102  this.attributesReader.endTag( name , content , context );
103  }
104  }
105 
106  private void fillHousehold() {
107  ((HouseholdImpl) this.currentHousehold).setMemberIds(this.currentmembers);
108  ((HouseholdImpl) this.currentHousehold).setVehicleIds(this.currentVehicleIds);
109  this.currentHousehold.setIncome(this.currentincome);
110  this.currentHhId = null;
111  this.currentVehicleIds = null;
112  this.currentincome = null;
113  this.currentmembers = null;
114  this.currentIncomePeriod = null;
115  this.currentincomeCurrency = null;
116  }
117 
118  /*package*/ Household createHousehold() {
119  Household hh = this.builder.createHousehold(this.currentHhId);
120  ((HouseholdImpl) hh).setMemberIds(this.currentmembers);
121  ((HouseholdImpl) hh).setVehicleIds(this.currentVehicleIds);
122  hh.setIncome(this.currentincome);
123  this.currentHhId = null;
124  this.currentVehicleIds = null;
125  this.currentincome = null;
126  this.currentmembers = null;
127  this.currentIncomePeriod = null;
128  this.currentincomeCurrency = null;
129  return hh;
130  }
131 
135  @Override
136  public void startTag(String name, Attributes atts, Stack<String> context) {
137  if (HouseholdsSchemaV10Names.HOUSEHOLD.equalsIgnoreCase(name)) {
138  this.currentHhId = Id.create(atts.getValue(HouseholdsSchemaV10Names.ID), Household.class);
139  this.currentHousehold = this.builder.createHousehold(this.currentHhId);
140  this.currentmembers = new ArrayList<>();
141  this.currentVehicleIds = new ArrayList<>();
142  }
143  else if (HouseholdsSchemaV10Names.MEMBERS.equalsIgnoreCase(name)) {
144 // this.currentmembers = new ArrayList<Id>();
145  }
146  else if (HouseholdsSchemaV10Names.PERSONID.equalsIgnoreCase(name)) {
147  Id<Person> personId = Id.create(atts.getValue(HouseholdsSchemaV10Names.REFID), Person.class);
148  this.currentmembers.add(personId);
149  }
150  else if (HouseholdsSchemaV10Names.INCOME.equalsIgnoreCase(name)) {
151  this.currentIncomePeriod = getIncomePeriod(atts.getValue(HouseholdsSchemaV10Names.PERIOD));
152  this.currentincomeCurrency = atts.getValue(HouseholdsSchemaV10Names.CURRENCY);
153  }
154  else if (HouseholdsSchemaV10Names.VEHICLES.equalsIgnoreCase(name)){
155 // this.currentVehicleIds = new ArrayList<Id>();
156  }
157  else if (HouseholdsSchemaV10Names.VEHICLEDEFINITIONID.equalsIgnoreCase(name)) {
158  Id<Vehicle> vehicleId = Id.create(atts.getValue(HouseholdsSchemaV10Names.REFID), Vehicle.class);
159  this.currentVehicleIds.add(vehicleId);
160  }
161  else if (name.equalsIgnoreCase(HouseholdsSchemaV10Names.ATTRIBUTES)) {
162  if (context.peek().equalsIgnoreCase(HouseholdsSchemaV10Names.HOUSEHOLD)) {
163  currAttributes = this.currentHousehold.getAttributes();
164  attributesReader.startTag( name , atts , context, currAttributes );
165  }
166  }
167  else if (name.equalsIgnoreCase(HouseholdsSchemaV10Names.ATTRIBUTE)) {
168  attributesReader.startTag( name , atts , context, currAttributes );
169  }
170  }
171 
172  private IncomePeriod getIncomePeriod(String s) {
173  if (IncomePeriod.day.toString().equalsIgnoreCase(s)) {
174  return IncomePeriod.day;
175  }
176  else if (IncomePeriod.month.toString().equalsIgnoreCase(s)) {
177  return IncomePeriod.month;
178  }
179  else if (IncomePeriod.week.toString().equalsIgnoreCase(s)) {
180  return IncomePeriod.week;
181  }
182  else if (IncomePeriod.hour.toString().equalsIgnoreCase(s)) {
183  return IncomePeriod.hour;
184  }
185  else if (IncomePeriod.second.toString().equalsIgnoreCase(s)) {
186  return IncomePeriod.second;
187  }
188  else if (IncomePeriod.year.toString().equalsIgnoreCase(s)) {
189  return IncomePeriod.year;
190  }
191  throw new IllegalArgumentException("Not known income period!");
192  }
193 
194  /*package*/ Households getHouseholds(){
195  return this.households;
196  }
197 }
void setIncome(Income income)
Household createHousehold(Id< Household > householdId)
HouseholdsFactory getFactory()
static< T > Id< T > create(final long key, final Class< T > type)
Definition: Id.java:68
void setCurrency(String currency)
Income createIncome(double income, IncomePeriod period)
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)