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 an edge element.
27   *
28   * @author Nathan Mittler - nathan.mittler@gmail.com
29   */
30  public class EdgeElementParser<G extends Hypergraph<V,E>,V,E> extends AbstractElementParser<G,V,E> {
31      
32      public EdgeElementParser(ParserContext<G,V,E> parserContext) {
33          super(parserContext);
34      }
35      
36      @SuppressWarnings("unchecked")
37      public EdgeMetadata parse(XMLEventReader xmlEventReader, StartElement start)
38              throws GraphIOException {
39  
40          try {
41              // Create the new edge.
42              EdgeMetadata edge = new EdgeMetadata();
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 if (edge.isDirected() == null && GraphMLConstants.DIRECTED_NAME.equals(name)) {
53                      edge.setDirected(("true".equals(value)));
54                  } else if (edge.getSource() == null && GraphMLConstants.SOURCE_NAME.equals(name)) {
55                      edge.setSource(value);
56                  } else if (edge.getTarget() == null && GraphMLConstants.TARGET_NAME.equals(name)) {
57                      edge.setTarget(value);
58                  } else if (edge.getSourcePort() == null && GraphMLConstants.SOURCEPORT_NAME.equals(name)) {
59                      edge.setSourcePort(value);
60                  } else if (edge.getTargetPort() == null && GraphMLConstants.TARGETPORT_NAME.equals(name)) {
61                      edge.setTargetPort(value);
62                  } else {
63                      edge.setProperty(name, value);
64                  }
65              }
66  
67              // Make sure the source and target have been been set.
68              if (edge.getSource() == null || edge.getTarget() == null) {
69                  throw new GraphIOException(
70                          "Element 'edge' is missing attribute 'source' or 'target'");
71              }
72  
73              while (xmlEventReader.hasNext()) {
74  
75                  XMLEvent event = xmlEventReader.nextEvent();
76                  if (event.isStartElement()) {
77                      StartElement element = (StartElement) event;
78  
79                      String name = element.getName().getLocalPart();
80                      if(GraphMLConstants.DESC_NAME.equals(name)) {
81                          String desc = (String)getParser(name).parse(xmlEventReader, element);
82                          edge.setDescription(desc);
83                      } else if(GraphMLConstants.DATA_NAME.equals(name)) {
84                          DataMetadata data = (DataMetadata)getParser(name).parse(xmlEventReader, element);
85                          edge.addData(data);
86                      } else {
87                          
88                          // Treat anything else as unknown
89                          getUnknownParser().parse(xmlEventReader, element);
90                      }
91  
92                  }
93                  if (event.isEndElement()) {
94                      EndElement end = (EndElement) event;
95                      verifyMatch(start, end);
96                      break;
97                  }
98              }
99              
100             // Apply the keys to this object.
101             applyKeys(edge);
102 
103             return edge;
104             
105         } catch (Exception e) {
106             ExceptionConverter.convert(e);
107         }
108 
109         return null;
110     }
111 }