Mercurial > hg > openmind
comparison src/main/java/org/mpi/openmind/repository/utils/OM4XmlEventReader.java @ 32:9c54842f5e86
better names for XML importer sub-classes.
author | casties |
---|---|
date | Thu, 25 Aug 2016 11:29:47 +0200 |
parents | 7d8ebe8ac8a2 |
children | 90f9a1c45b15 |
comparison
equal
deleted
inserted
replaced
31:7d8ebe8ac8a2 | 32:9c54842f5e86 |
---|---|
28 * The constructor takes an InputStream. | 28 * The constructor takes an InputStream. |
29 * | 29 * |
30 * The read() method reads the contents of the file into the members | 30 * The read() method reads the contents of the file into the members |
31 * .entities and .relations. | 31 * .entities and .relations. |
32 * | 32 * |
33 * The contents are Lists of OmEntities and OmRelations also holding | 33 * The contents are Lists of OmXmlEntities and OmXmlRelations holding |
34 * Lists of omAttributes. | 34 * Lists of omXmlAttributes. |
35 * | |
36 * This implementation uses XMLEventReader. | |
35 * | 37 * |
36 * @author casties | 38 * @author casties |
37 * | 39 * |
38 */ | 40 */ |
39 public class OM4XmlEventReader { | 41 public class OM4XmlEventReader { |
46 } | 48 } |
47 | 49 |
48 InputStream xmlStream; | 50 InputStream xmlStream; |
49 | 51 |
50 public int numEntities; | 52 public int numEntities; |
51 public List<OmEntity> entities; | 53 public List<OmXmlEntity> entities; |
52 private int entCnt = 0; | 54 private int entCnt = 0; |
53 | 55 |
54 public int numRelations; | 56 public int numRelations; |
55 public List<OmRelation> relations; | 57 public List<OmXmlRelation> relations; |
56 private int relCnt = 0; | 58 private int relCnt = 0; |
57 | 59 |
58 /** | 60 /** |
59 * Simple class holding the representation of an OpenMind Attribute from XML. | 61 * Simple class holding the representation of an OpenMind Attribute from XML. |
60 * | 62 * |
61 * @author casties | 63 * @author casties |
62 */ | 64 */ |
63 public class OmAttribute { | 65 public class OmXmlAttribute { |
64 public Map<String, String> xmlAtts; | 66 public Map<String, String> xmlAtts; |
65 public String value; | 67 public String value; |
66 | 68 |
67 public String getId() { | 69 public String getId() { |
68 return xmlAtts.get("id"); | 70 return xmlAtts.get("id"); |
72 /** | 74 /** |
73 * Simple class holding the representation of an OpenMind Entity from XML. | 75 * Simple class holding the representation of an OpenMind Entity from XML. |
74 * | 76 * |
75 * @author casties | 77 * @author casties |
76 */ | 78 */ |
77 public class OmEntity { | 79 public class OmXmlEntity { |
78 public Map<String, String> xmlAtts; | 80 public Map<String, String> xmlAtts; |
79 public String value; | 81 public String value; |
80 public List<OmAttribute> attributes; | 82 public List<OmXmlAttribute> attributes; |
81 | 83 |
82 public String getId() { | 84 public String getId() { |
83 return xmlAtts.get("id"); | 85 return xmlAtts.get("id"); |
84 } | 86 } |
85 } | 87 } |
87 /** | 89 /** |
88 * Simple class holding the representation of an OpenMind Relation from XML. | 90 * Simple class holding the representation of an OpenMind Relation from XML. |
89 * | 91 * |
90 * @author casties | 92 * @author casties |
91 */ | 93 */ |
92 public class OmRelation { | 94 public class OmXmlRelation { |
93 public Map<String, String> xmlAtts; | 95 public Map<String, String> xmlAtts; |
94 public String value; | 96 public String value; |
95 public List<OmAttribute> attributes; | 97 public List<OmXmlAttribute> attributes; |
96 | 98 |
97 public String getId() { | 99 public String getId() { |
98 return xmlAtts.get("id"); | 100 return xmlAtts.get("id"); |
99 } | 101 } |
100 } | 102 } |
133 * @param elem | 135 * @param elem |
134 * @param reader | 136 * @param reader |
135 * @return | 137 * @return |
136 * @throws XMLStreamException | 138 * @throws XMLStreamException |
137 */ | 139 */ |
138 private List<OmEntity> processEntities(StartElement elem, XMLEventReader reader) throws XMLStreamException { | 140 private List<OmXmlEntity> processEntities(StartElement elem, XMLEventReader reader) throws XMLStreamException { |
139 logger.debug("loading entities..."); | 141 logger.debug("loading entities..."); |
140 // get number attribute | 142 // get number attribute |
141 Attribute numa = elem.getAttributeByName(new QName("number")); | 143 Attribute numa = elem.getAttributeByName(new QName("number")); |
142 if (numa != null) { | 144 if (numa != null) { |
143 numEntities = Integer.parseInt(numa.getValue()); | 145 numEntities = Integer.parseInt(numa.getValue()); |
144 } | 146 } |
145 // start reading sub-elements | 147 // start reading sub-elements |
146 List<OmEntity> entities = new ArrayList<OmEntity>(); | 148 List<OmXmlEntity> entities = new ArrayList<OmXmlEntity>(); |
147 while (reader.hasNext()) { | 149 while (reader.hasNext()) { |
148 XMLEvent e = reader.nextEvent(); | 150 XMLEvent e = reader.nextEvent(); |
149 if (e.isStartElement()) { | 151 if (e.isStartElement()) { |
150 // start of next element | 152 // start of next element |
151 StartElement es = e.asStartElement(); | 153 StartElement es = e.asStartElement(); |
173 * @param elem | 175 * @param elem |
174 * @param reader | 176 * @param reader |
175 * @return | 177 * @return |
176 * @throws XMLStreamException | 178 * @throws XMLStreamException |
177 */ | 179 */ |
178 private OmEntity processEntity(StartElement elem, XMLEventReader reader) throws XMLStreamException { | 180 private OmXmlEntity processEntity(StartElement elem, XMLEventReader reader) throws XMLStreamException { |
179 //logger.debug("entity"); | 181 //logger.debug("entity"); |
180 OmEntity ent = new OmEntity(); | 182 OmXmlEntity ent = new OmXmlEntity(); |
181 Map<String, String> xmlAtts = new HashMap<String, String>(); | 183 Map<String, String> xmlAtts = new HashMap<String, String>(); |
182 @SuppressWarnings("unchecked") | 184 @SuppressWarnings("unchecked") |
183 Iterator<Attribute> atts = elem.getAttributes(); | 185 Iterator<Attribute> atts = elem.getAttributes(); |
184 while (atts.hasNext()) { | 186 while (atts.hasNext()) { |
185 Attribute att = atts.next(); | 187 Attribute att = atts.next(); |
186 xmlAtts.put(att.getName().getLocalPart(), att.getValue()); | 188 xmlAtts.put(att.getName().getLocalPart(), att.getValue()); |
187 } | 189 } |
188 ent.xmlAtts = xmlAtts; | 190 ent.xmlAtts = xmlAtts; |
189 // start reading sub-elements | 191 // start reading sub-elements |
190 ent.attributes = new ArrayList<OmAttribute>(); | 192 ent.attributes = new ArrayList<OmXmlAttribute>(); |
191 while (reader.hasNext()) { | 193 while (reader.hasNext()) { |
192 XMLEvent e = reader.nextEvent(); | 194 XMLEvent e = reader.nextEvent(); |
193 if (e.isStartElement()) { | 195 if (e.isStartElement()) { |
194 // start of next element | 196 // start of next element |
195 StartElement es = e.asStartElement(); | 197 StartElement es = e.asStartElement(); |
230 * @param elem | 232 * @param elem |
231 * @param reader | 233 * @param reader |
232 * @return | 234 * @return |
233 * @throws XMLStreamException | 235 * @throws XMLStreamException |
234 */ | 236 */ |
235 private List<OmRelation> processRelations(StartElement elem, XMLEventReader reader) throws XMLStreamException { | 237 private List<OmXmlRelation> processRelations(StartElement elem, XMLEventReader reader) throws XMLStreamException { |
236 logger.debug("loading relations..."); | 238 logger.debug("loading relations..."); |
237 // get number attribute | 239 // get number attribute |
238 Attribute numa = elem.getAttributeByName(new QName("number")); | 240 Attribute numa = elem.getAttributeByName(new QName("number")); |
239 if (numa != null) { | 241 if (numa != null) { |
240 numRelations = Integer.parseInt(numa.getValue()); | 242 numRelations = Integer.parseInt(numa.getValue()); |
241 } | 243 } |
242 // start reading sub-elements | 244 // start reading sub-elements |
243 List<OmRelation> rels = new ArrayList<OmRelation>(); | 245 List<OmXmlRelation> rels = new ArrayList<OmXmlRelation>(); |
244 while (reader.hasNext()) { | 246 while (reader.hasNext()) { |
245 XMLEvent e = reader.nextEvent(); | 247 XMLEvent e = reader.nextEvent(); |
246 if (e.isStartElement()) { | 248 if (e.isStartElement()) { |
247 // start of next element | 249 // start of next element |
248 StartElement es = e.asStartElement(); | 250 StartElement es = e.asStartElement(); |
271 * @param elem | 273 * @param elem |
272 * @param reader | 274 * @param reader |
273 * @return | 275 * @return |
274 * @throws XMLStreamException | 276 * @throws XMLStreamException |
275 */ | 277 */ |
276 private OmRelation processRelation(StartElement elem, XMLEventReader reader) throws XMLStreamException { | 278 private OmXmlRelation processRelation(StartElement elem, XMLEventReader reader) throws XMLStreamException { |
277 //logger.debug("relation"); | 279 //logger.debug("relation"); |
278 OmRelation rel = new OmRelation(); | 280 OmXmlRelation rel = new OmXmlRelation(); |
279 Map<String, String> xmlAtts = new HashMap<String, String>(); | 281 Map<String, String> xmlAtts = new HashMap<String, String>(); |
280 @SuppressWarnings("unchecked") | 282 @SuppressWarnings("unchecked") |
281 Iterator<Attribute> atts = elem.getAttributes(); | 283 Iterator<Attribute> atts = elem.getAttributes(); |
282 while (atts.hasNext()) { | 284 while (atts.hasNext()) { |
283 Attribute att = atts.next(); | 285 Attribute att = atts.next(); |
284 xmlAtts.put(att.getName().getLocalPart(), att.getValue()); | 286 xmlAtts.put(att.getName().getLocalPart(), att.getValue()); |
285 } | 287 } |
286 rel.xmlAtts = xmlAtts; | 288 rel.xmlAtts = xmlAtts; |
287 // start reading sub-elements | 289 // start reading sub-elements |
288 rel.attributes = new ArrayList<OmAttribute>(); | 290 rel.attributes = new ArrayList<OmXmlAttribute>(); |
289 while (reader.hasNext()) { | 291 while (reader.hasNext()) { |
290 XMLEvent e = reader.nextEvent(); | 292 XMLEvent e = reader.nextEvent(); |
291 if (e.isStartElement()) { | 293 if (e.isStartElement()) { |
292 // start of next element | 294 // start of next element |
293 StartElement es = e.asStartElement(); | 295 StartElement es = e.asStartElement(); |
328 * @param elem | 330 * @param elem |
329 * @param reader | 331 * @param reader |
330 * @return | 332 * @return |
331 * @throws XMLStreamException | 333 * @throws XMLStreamException |
332 */ | 334 */ |
333 private OmAttribute processAttribute(StartElement elem, XMLEventReader reader) throws XMLStreamException { | 335 private OmXmlAttribute processAttribute(StartElement elem, XMLEventReader reader) throws XMLStreamException { |
334 //logger.debug("attribute"); | 336 //logger.debug("attribute"); |
335 OmAttribute oma = new OmAttribute(); | 337 OmXmlAttribute oma = new OmXmlAttribute(); |
336 Map<String, String> xmlAtts = new HashMap<String, String>(); | 338 Map<String, String> xmlAtts = new HashMap<String, String>(); |
337 @SuppressWarnings("unchecked") | 339 @SuppressWarnings("unchecked") |
338 Iterator<Attribute> atts = elem.getAttributes(); | 340 Iterator<Attribute> atts = elem.getAttributes(); |
339 while (atts.hasNext()) { | 341 while (atts.hasNext()) { |
340 Attribute att = atts.next(); | 342 Attribute att = atts.next(); |