View Javadoc
1   /*
2    * Copyright (c) 2008, The JUNG Authors
3    *
4    * All rights reserved.
5    *
6    * This software is open-source under the BSD license; see either
7    * "license.txt" or
8    * https://github.com/jrtom/jung/blob/master/LICENSE for a description.
9    */
10  
11  package edu.uci.ics.jung.io.graphml.parser;
12  
13  import java.util.Iterator;
14  
15  import javax.xml.stream.XMLEventReader;
16  import javax.xml.stream.events.Attribute;
17  import javax.xml.stream.events.Characters;
18  import javax.xml.stream.events.EndElement;
19  import javax.xml.stream.events.StartElement;
20  import javax.xml.stream.events.XMLEvent;
21  
22  import edu.uci.ics.jung.graph.Hypergraph;
23  import edu.uci.ics.jung.io.GraphIOException;
24  import edu.uci.ics.jung.io.graphml.DataMetadata;
25  import edu.uci.ics.jung.io.graphml.GraphMLConstants;
26  import edu.uci.ics.jung.io.graphml.ExceptionConverter;
27  
28  /**
29   * Parses the data element.
30   *
31   * @author Nathan Mittler - nathan.mittler@gmail.com
32   */
33  public class DataElementParser<G extends Hypergraph<V,E>,V,E> extends AbstractElementParser<G,V,E> {
34  
35      public DataElementParser(ParserContext<G,V,E> parserContext) {
36          super(parserContext);
37      }
38      
39      public DataMetadata parse(XMLEventReader xmlEventReader, StartElement start)
40              throws GraphIOException {
41  
42          try {
43              // Create the new port.
44              DataMetadata data = new DataMetadata();
45  
46              // Parse the attributes.
47              @SuppressWarnings("unchecked")
48  			Iterator<Attribute> iterator = start.getAttributes();
49              while (iterator.hasNext()) {
50                  Attribute attribute = (Attribute) iterator.next();
51                  String name = attribute.getName().getLocalPart();
52                  String value = attribute.getValue();
53                  if (data.getKey() == null && GraphMLConstants.KEY_NAME.equals(name)) {
54                      data.setKey(value);
55                  }
56              }
57  
58              // Make sure the key has been set.
59              if (data.getKey() == null) {
60                  throw new GraphIOException(
61                          "Element 'data' is missing attribute 'key'");
62              }
63  
64              while (xmlEventReader.hasNext()) {
65  
66                  XMLEvent event = xmlEventReader.nextEvent();
67                  if (event.isStartElement()) {
68                      StartElement element = (StartElement) event;
69                          
70                      // Treat any child elements as unknown
71                      getUnknownParser().parse(xmlEventReader, element);
72                  }
73                  if (event.isCharacters()) {
74                      Characters characters = (Characters) event;
75                      data.setValue(characters.getData());
76                  }
77                  if (event.isEndElement()) {
78                      EndElement end = (EndElement) event;
79                      verifyMatch(start, end);
80                      break;
81                  }
82              }
83  
84              return data;
85              
86          } catch (Exception e) {
87              ExceptionConverter.convert(e);
88          }
89  
90          return null;
91      }
92  }
93