View Javadoc

1   /*
2    * Copyright (c) 2008, the JUNG Project and the Regents of the University
3    * of California
4    * All rights reserved.
5    *
6    * This software is open-source under the BSD license; see either
7    * "license.txt" or
8    * http://jung.sourceforge.net/license.txt 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.EndElement;
18  import javax.xml.stream.events.StartElement;
19  import javax.xml.stream.events.XMLEvent;
20  
21  import edu.uci.ics.jung.graph.Hypergraph;
22  import edu.uci.ics.jung.io.GraphIOException;
23  import edu.uci.ics.jung.io.graphml.*;
24  
25  /**
26   * Parses hyper edge elements.
27   *
28   * @author Nathan Mittler - nathan.mittler@gmail.com
29   */
30  public class HyperEdgeElementParser<G extends Hypergraph<V,E>,V,E> extends AbstractElementParser<G,V,E> {
31  
32      public HyperEdgeElementParser(ParserContext<G,V,E> parserContext) {
33          super(parserContext);
34      }
35      
36      @SuppressWarnings("unchecked")
37      public HyperEdgeMetadata parse(XMLEventReader xmlEventReader, StartElement start)
38              throws GraphIOException {
39  
40          try {
41              // Create the new edge.
42              HyperEdgeMetadata edge = new HyperEdgeMetadata();
43  
44              // Parse the attributes.
45              Iterator iterator = start.getAttributes();
46              while (iterator.hasNext()) {
47                  Attribute attribute = (Attribute) iterator.next();
48                  String name = attribute.getName().getLocalPart();
49                  String value = attribute.getValue();
50                  if (edge.getId() == null && GraphMLConstants.ID_NAME.equals(name)) {
51                      edge.setId(value);
52                  } else {
53                      edge.setProperty(name, value);
54                  }
55              }
56  
57              while (xmlEventReader.hasNext()) {
58  
59                  XMLEvent event = xmlEventReader.nextEvent();
60                  if (event.isStartElement()) {
61                      StartElement element = (StartElement) event;
62  
63                      String name = element.getName().getLocalPart();
64                      if(GraphMLConstants.DESC_NAME.equals(name)) {
65                          String desc = (String)getParser(name).parse(xmlEventReader, element);
66                          edge.setDescription(desc);
67                      } else if(GraphMLConstants.DATA_NAME.equals(name)) {
68                          DataMetadata data = (DataMetadata)getParser(name).parse(xmlEventReader, element);
69                          edge.addData(data);
70                      } else if(GraphMLConstants.ENDPOINT_NAME.equals(name)) {
71                          EndpointMetadata ep = (EndpointMetadata)getParser(name).parse(xmlEventReader, element);
72                          edge.addEndpoint(ep);
73                      } else {
74                          
75                          // Treat anything else as unknown
76                          getUnknownParser().parse(xmlEventReader, element);
77                      }
78  
79                  }
80                  if (event.isEndElement()) {
81                      EndElement end = (EndElement) event;
82                      verifyMatch(start, end);
83                      break;
84                  }
85              }
86  
87              // Apply the keys to this object.
88              applyKeys(edge);
89              
90              return edge;
91              
92          } catch (Exception e) {
93              ExceptionConverter.convert(e);
94          }
95  
96          return null;
97      }
98  }