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.HashMap;
14  import java.util.Iterator;
15  import java.util.Map;
16  
17  import javax.xml.stream.XMLEventReader;
18  import javax.xml.stream.events.Attribute;
19  import javax.xml.stream.events.EndElement;
20  import javax.xml.stream.events.StartElement;
21  import javax.xml.stream.events.XMLEvent;
22  
23  import edu.uci.ics.jung.graph.Hypergraph;
24  import edu.uci.ics.jung.io.GraphIOException;
25  import edu.uci.ics.jung.io.graphml.EndpointMetadata;
26  import edu.uci.ics.jung.io.graphml.GraphMLConstants;
27  import edu.uci.ics.jung.io.graphml.ExceptionConverter;
28  import edu.uci.ics.jung.io.graphml.EndpointMetadata.EndpointType;
29  
30  /**
31   * Parses endpoint elements.
32   * 
33   * @author Nathan Mittler - nathan.mittler@gmail.com
34   */
35  public class EndpointElementParser<G extends Hypergraph<V,E>,V,E> extends AbstractElementParser<G,V,E> {
36      
37      final static private Map<String, EndpointType> endpointTypeMap = new HashMap<String, EndpointType>();
38      static {
39          endpointTypeMap.put(GraphMLConstants.IN_NAME, EndpointType.IN);
40          endpointTypeMap.put(GraphMLConstants.OUT_NAME, EndpointType.OUT);
41          endpointTypeMap.put(GraphMLConstants.UNDIR_NAME, EndpointType.UNDIR);
42      }
43      
44      public EndpointElementParser(ParserContext<G,V,E> parserContext) {
45          super(parserContext);
46      }
47      
48      public EndpointMetadata parse(XMLEventReader xmlEventReader, StartElement start)
49              throws GraphIOException {
50  
51          try {
52              // Create the new endpoint.
53              EndpointMetadata endpoint = new EndpointMetadata();
54  
55              // Parse the attributes.
56              @SuppressWarnings("unchecked")
57              Iterator<Attribute> iterator = start.getAttributes();
58              while (iterator.hasNext()) {
59                  Attribute attribute = iterator.next();
60                  String name = attribute.getName().getLocalPart();
61                  String value = attribute.getValue();
62                  if (endpoint.getId() == null && GraphMLConstants.ID_NAME.equals(name)) {
63                      endpoint.setId(value);
64                  } if (endpoint.getPort() == null && GraphMLConstants.PORT_NAME.equals(name)) {
65                      endpoint.setPort(value);
66                  } if (endpoint.getNode() == null && GraphMLConstants.NODE_NAME.equals(name)) {
67                      endpoint.setNode(value);
68                  } if (GraphMLConstants.TYPE_NAME.equals(name)) {
69                      EndpointType t = endpointTypeMap.get(value);
70                      if( t == null ) {
71                          t = EndpointType.UNDIR;
72                      }
73                      endpoint.setEndpointType(t);
74                  } else {
75                      endpoint.setProperty(name, value);
76                  }
77              }
78  
79              // Make sure the node has been set.
80              if (endpoint.getNode() == null) {
81                  throw new GraphIOException(
82                          "Element 'endpoint' is missing attribute 'node'");
83              }
84  
85              while (xmlEventReader.hasNext()) {
86  
87                  XMLEvent event = xmlEventReader.nextEvent();
88                  if (event.isStartElement()) {
89                      StartElement element = (StartElement) event;
90  
91                      String name = element.getName().getLocalPart();
92                      if(GraphMLConstants.DESC_NAME.equals(name)) {
93                          String desc = (String)getParser(name).parse(xmlEventReader, element);
94                          endpoint.setDescription(desc);
95                      } else {
96                          
97                          // Treat anything else as unknown
98                          getUnknownParser().parse(xmlEventReader, element);
99                      }
100 
101                 }
102                 if (event.isEndElement()) {
103                     EndElement end = (EndElement) event;
104                     verifyMatch(start, end);
105                     break;
106                 }
107             }
108             
109             // Apply the keys to this object.
110             applyKeys(endpoint);
111 
112             return endpoint;
113             
114         } catch (Exception e) {
115             ExceptionConverter.convert(e);
116         }
117 
118         return null;
119     }
120 }