001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.plugins.main.saveload.xmlparser;
005    
006    import graphlab.graph.atributeset.EdgeAttrSet;
007    import graphlab.graph.atributeset.GraphAttrSet;
008    import graphlab.graph.atributeset.VertexAttrSet;
009    import graphlab.graph.graph.EdgeModel;
010    import graphlab.graph.graph.GraphModel;
011    import graphlab.graph.graph.VertexModel;
012    import graphlab.platform.core.BlackBoard;
013    import graphlab.platform.StaticUtils;
014    import org.xml.sax.Attributes;
015    import org.xml.sax.SAXException;
016    
017    import java.util.HashMap;
018    
019    public class GraphmlHandlerImpl implements GraphmlHandler {
020    
021        public static final boolean DEBUG = false;
022        public HashMap<String, VertexModel> vByID = new HashMap<String, VertexModel>();
023    
024    
025        public GraphModel g;
026        private VertexModel curv = null;
027        private EdgeModel cure = null;
028        public BlackBoard bb;
029        private VertexAttrSet curvAS;
030        private EdgeAttrSet cureAS;
031    
032        /**
033         * @param graph if graph is null a new graph will be generated which can be get by getGraph() , otherwise the loaded graph
034         *              will be added to the given graph
035         */
036    
037        public GraphmlHandlerImpl(GraphModel graph) {
038            g = graph;
039        }
040    
041        /**
042         * @see GraphmlHandlerImpl(GraphModel graph)
043         */
044        public GraphmlHandlerImpl() {
045            g = null;
046        }
047    
048    
049        public GraphmlHandlerImpl(BlackBoard blackBoard) {
050            bb = blackBoard;
051    //        g = new Graph(bb);
052        }
053    
054        public GraphModel getGraph() {
055            return g;
056        }
057    
058        public static HashMap<String, String> graphMLGraphKeys = new HashMap<String, String>();
059        public static HashMap<String, String> graphMLVertexKeys = new HashMap<String, String>();
060        public static HashMap<String, String> graphMLEdgeKeys = new HashMap<String, String>();
061    
062        public void handle_key(final java.lang.String data, final Attributes meta) throws SAXException {
063            String s = meta.getValue("for");
064            String id = meta.getValue("id");
065            String attrname = meta.getValue("attr.name");
066            String attrtype = meta.getValue("attr.type");
067            if (s.equalsIgnoreCase("graph")) {
068                graphMLGraphKeys.put(attrname, attrtype);
069            } else if (s.equalsIgnoreCase("node")) {
070                graphMLVertexKeys.put(attrname, attrtype);
071            } else {
072                graphMLEdgeKeys.put(attrname, attrtype);
073            }
074            if (DEBUG) System.err.println("handle_key: " + data + "," + id + "," + s);
075        }
076    
077        public void start_edge(final Attributes meta) throws SAXException {
078            VertexModel v1 = vByID.get(meta.getValue("source"));
079            VertexModel v2 = vByID.get(meta.getValue("target"));
080    
081            if (DEBUG)
082                System.out.println("Edge between : (" + meta.getValue(EdgeAttrSet.SOURCE) + ")" + v1 + ",(" + meta.getValue(EdgeAttrSet.TARGET) + ")" + v2);
083            EdgeModel e = new EdgeModel(v1, v2);
084            //todo: the id can not be setted (it's a fix value)
085    //        e.setID(meta.getValue(EdgeModel.ID));
086            g.insertEdge(e);
087            cure = e;
088            cureAS = new EdgeAttrSet(e);
089            if (DEBUG) System.err.println("start_edge: " + meta);
090        }
091    
092        public void end_edge() throws SAXException {
093    
094            if (DEBUG) System.err.println("end_edge()");
095        }
096    
097        public void handle_locator(final Attributes meta) throws SAXException {
098    
099            if (DEBUG) System.err.println("handle_locator: " + meta);
100        }
101    
102        public void handle_data(final java.lang.String data, final Attributes meta) throws SAXException {
103            String s1 = meta.getValue("key");
104            if (s1.charAt(0) == 'g') {
105                String ss = graphMLGraphKeys.get(s1.substring(2));
106                //it is handled on start graph
107    //            if (ss != Graph.EDGEDEFAULT)
108                new GraphAttrSet(g).put(s1.substring(2), StaticUtils.fromString(ss, data));
109            } else if (s1.charAt(0) == 'n') {
110                String ss = graphMLVertexKeys.get(s1.substring(2));
111                curvAS.put(s1.substring(2), StaticUtils.fromString(ss, data));
112            } else {
113                String ss = graphMLEdgeKeys.get(s1.substring(2));
114                cureAS.put(s1.substring(2), StaticUtils.fromString(ss, data));
115            }
116            if (DEBUG) System.err.println("handle_data: " + data + "," + s1);
117        }
118    
119        public void start_node(final Attributes meta) throws SAXException {
120            String id = meta.getValue("id");
121            Integer iid = Integer.parseInt(id);
122            VertexModel v = new VertexModel();
123    //        v.putAtr(VertexModel.ID, iid);
124    
125    
126            vByID.put("" + id, v);
127            curv = v;
128            curvAS = new VertexAttrSet(curv);
129            if (DEBUG)
130                System.out.println("Vertex added : " + v);
131    
132            if (DEBUG) System.err.println("start_node: " + meta);
133        }
134    
135        public void end_node() throws SAXException {
136            g.insertVertex(curv);
137    
138            if (DEBUG) System.err.println("end_node()");
139        }
140    
141        public void start_graph(final Attributes meta) throws SAXException {
142            if (g == null) {
143    //todo
144                g = new GraphModel(meta.getValue("edgedefault").equals("directed"));
145            }
146            g.setLabel(meta.getValue("id"));
147    
148            if (DEBUG) System.err.println("start_graph: " + meta);
149        }
150    
151        public void end_graph() throws SAXException {
152    
153            if (DEBUG) System.err.println("end_graph()");
154        }
155    
156        public void start_endpoint(final Attributes meta) throws SAXException {
157    
158            if (DEBUG) System.err.println("start_endpoint: " + meta);
159        }
160    
161        public void end_endpoint() throws SAXException {
162    
163            if (DEBUG) System.err.println("end_endpoint()");
164        }
165    
166        public void start_graphml(final Attributes meta) throws SAXException {
167    
168            if (DEBUG) System.err.println("start_graphml: " + meta);
169        }
170    
171        public void end_graphml() throws SAXException {
172    
173            if (DEBUG) System.err.println("end_graphml()");
174        }
175    
176        public void start_hyperedge(final Attributes meta) throws SAXException {
177    
178            if (DEBUG) System.err.println("start_hyperedge: " + meta);
179        }
180    
181        public void end_hyperedge() throws SAXException {
182    
183            if (DEBUG) System.err.println("end_hyperedge()");
184        }
185    
186        public void start_port(final Attributes meta) throws SAXException {
187    
188            if (DEBUG) System.err.println("start_port: " + meta);
189        }
190    
191        public void end_port() throws SAXException {
192    
193            if (DEBUG) System.err.println("end_port()");
194        }
195    
196        public void handle_desc(final java.lang.String data, final Attributes meta) throws SAXException {
197    
198            if (DEBUG) System.err.println("handle_desc: " + data);
199        }
200    }