[graphl-cvs] graphl/src/org/mediavirus/graphl/jxpath GraphAttributePointer.java GraphNodeIterator.ja
Status: Pre-Alpha
Brought to you by:
flo1
Update of /cvsroot/graphl/graphl/src/org/mediavirus/graphl/jxpath In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2618/src/org/mediavirus/graphl/jxpath Added Files: GraphAttributePointer.java GraphNodeIterator.java GraphNodePointer.java GraphEdgePointer.java GraphEdgeIterator.java GraphPointerFactory.java GraphAttributeIterator.java Log Message: - FEATURE: RXPath landed! you can assign properties of facets through XPath-like expressions based on the currently rendered node - CODE: Remove LabelGenerator classes - this can now done with RXPath - CODE: migrated to JDK 1.5, added class specifiers for all collections (generics) - CODE: added a singleton GraphlRegistry, currently only holding the vocabularyRegistry instance --- NEW FILE: GraphAttributePointer.java --- /* * Created on 16.12.2005 */ package org.mediavirus.graphl.jxpath; import org.apache.commons.jxpath.ri.QName; import org.apache.commons.jxpath.ri.model.NodePointer; public class GraphAttributePointer extends NodePointer { String value; QName name; public GraphAttributePointer(NodePointer parent, QName name, String value) { super(parent); this.name = name; this.value = value; } public QName getName() { return name; } public Object getValue() { return value; } public Object getBaseValue() { return value; } public boolean isCollection() { return false; } public int getLength() { return 1; } public Object getImmediateNode() { return value; } public boolean isActual() { return true; } public boolean isLeaf() { return true; } /** * Sets the value of this attribute. */ public void setValue(Object value) { // TODO ? } public void remove() { // TODO ? } /** */ public String asPath() { StringBuffer buffer = new StringBuffer(); if (parent != null) { buffer.append(parent.asPath()); if (buffer.length() == 0 || buffer.charAt(buffer.length() - 1) != '/') { buffer.append('/'); } } buffer.append('@'); buffer.append(getName()); return buffer.toString(); } public int hashCode() { return System.identityHashCode(value); } public boolean equals(Object object) { if (object == this) { return true; } if (!(object instanceof String)) { return false; } return object.equals(value); } public int compareChildNodePointers( NodePointer pointer1, NodePointer pointer2) { // Won't happen - attributes don't have children return 0; } } --- NEW FILE: GraphEdgeIterator.java --- /* * Created on 15.12.2005 */ package org.mediavirus.graphl.jxpath; import org.apache.commons.jxpath.ri.QName; import org.apache.commons.jxpath.ri.compiler.NodeNameTest; import org.apache.commons.jxpath.ri.compiler.NodeTest; import org.apache.commons.jxpath.ri.model.NodeIterator; import org.apache.commons.jxpath.ri.model.NodePointer; import org.mediavirus.graphl.graph.Edge; import org.mediavirus.graphl.graph.Node; public class GraphEdgeIterator implements NodeIterator { private NodePointer self; private NodeTest nodeTest; //private boolean reverse; private Edge edge = null; private Node child = null; private int position = 0; public GraphEdgeIterator(GraphEdgePointer self, NodeTest test, boolean reverse, NodePointer startWith) { this.self = self; this.edge = (Edge) self.getNode(); this.nodeTest = test; //this.reverse = reverse; if (startWith != null) { this.child = (Node) startWith.getNode(); } } public NodePointer getNodePointer() { if (child == null) { return null; } return new GraphNodePointer(self, child); } public int getPosition() { return position; } public boolean setPosition(int position) { //if (reverse) position = 3-position; if (position == 1) { if (nodeTest instanceof NodeNameTest) { NodeNameTest nodeNameTest = (NodeNameTest) nodeTest; QName testName = nodeNameTest.getNodeName(); //String namespaceURI = nodeNameTest.getNamespaceURI(); boolean wildcard = nodeNameTest.isWildcard(); //String testPrefix = testName.getPrefix(); if (wildcard) { child = edge.getFrom(); return true; } else if (testName.getName().equals("from")) { child = edge.getFrom(); return true; } else if (testName.getName().equals("to")) { child = edge.getTo(); return true; } else { child = null; return false; } } else { child = edge.getFrom(); return true; } } else if (position == 2) { if (nodeTest instanceof NodeNameTest) { NodeNameTest nodeNameTest = (NodeNameTest) nodeTest; //QName testName = nodeNameTest.getNodeName(); //String namespaceURI = nodeNameTest.getNamespaceURI(); boolean wildcard = nodeNameTest.isWildcard(); //String testPrefix = testName.getPrefix(); if (wildcard) { child = edge.getTo(); return true; } else { child = null; return false; } } else { child = edge.getTo(); return true; } } else { position = 1; child = null; } return false; } } --- NEW FILE: GraphPointerFactory.java --- /* * Created on 15.12.2005 */ package org.mediavirus.graphl.jxpath; import java.util.Locale; import org.apache.commons.jxpath.ri.QName; import org.apache.commons.jxpath.ri.model.NodePointer; import org.apache.commons.jxpath.ri.model.NodePointerFactory; import org.mediavirus.graphl.graph.Edge; import org.mediavirus.graphl.graph.Node; public class GraphPointerFactory implements NodePointerFactory { public int getOrder() { return 1; } public NodePointer createNodePointer(NodePointer parent, QName name, Object object) { if (object instanceof Node) { return new GraphNodePointer(parent, (Node)object); } else if (object instanceof Edge) { return new GraphEdgePointer(parent, (Edge)object); } else { return null; } } public NodePointer createNodePointer(QName name, Object object, Locale locale) { if (object instanceof Node) { return new GraphNodePointer((Node)object); } else if (object instanceof Edge) { return new GraphEdgePointer((Edge)object); } else { return null; } } } --- NEW FILE: GraphEdgePointer.java --- /* * Created on 15.12.2005 */ package org.mediavirus.graphl.jxpath; import org.apache.commons.jxpath.ri.QName; import org.apache.commons.jxpath.ri.compiler.NodeTest; import org.apache.commons.jxpath.ri.model.NodeIterator; import org.apache.commons.jxpath.ri.model.NodePointer; import org.mediavirus.graphl.GraphlRegistry; import org.mediavirus.graphl.graph.Edge; import org.mediavirus.util.ParseUtils; public class GraphEdgePointer extends NodePointer { private Edge edge; public GraphEdgePointer(Edge edge) { super(null); this.edge = edge; } public GraphEdgePointer(NodePointer parent, Edge edge) { super(parent); this.edge = edge; } public NodeIterator childIterator(NodeTest test, boolean reverse, NodePointer startWith) { return new GraphEdgeIterator(this, test, reverse, startWith); } public NodeIterator attributeIterator(QName name) { return new GraphAttributeIterator(this, name); } @Override public int compareChildNodePointers(NodePointer pointer1, NodePointer pointer2) { return 0; } @Override public Object getBaseValue() { return edge; } @Override public Object getImmediateNode() { return edge; } @Override public int getLength() { return 2; } @Override public QName getName() { String type = edge.getType(); String ns = ParseUtils.guessNamespace(type); String prefix = GraphlRegistry.instance().getVocabularyRegistry().resolvePrefix(ns); String name = ParseUtils.guessName(type); return new QName(prefix, name); } @Override public boolean isCollection() { return false; } @Override public boolean isLeaf() { return false; } @Override public void setValue(Object value) { } public Edge getNode() { return edge; } } --- NEW FILE: GraphAttributeIterator.java --- /* * Created on 15.12.2005 */ package org.mediavirus.graphl.jxpath; import org.apache.commons.jxpath.ri.QName; import org.apache.commons.jxpath.ri.model.NodeIterator; import org.apache.commons.jxpath.ri.model.NodePointer; import org.mediavirus.graphl.GraphlRegistry; import org.mediavirus.graphl.graph.GraphElement; import org.mediavirus.graphl.vocabulary.VocabularyRegistry; public class GraphAttributeIterator implements NodeIterator { GraphElement element; QName name; String value = null; NodePointer self; protected GraphAttributeIterator(GraphElement el, QName name) { this.element = el; this.name = name; } public GraphAttributeIterator(GraphNodePointer pointer, QName name) { this((GraphElement)pointer.getNode(), name); self = pointer; } public GraphAttributeIterator(GraphEdgePointer pointer, QName name) { this((GraphElement)pointer.getNode(), name); self = pointer; } public NodePointer getNodePointer() { if (value == null) { return null; } return new GraphAttributePointer(self, name, value); } public int getPosition() { return (value == null) ? 0 : 1; } public boolean setPosition(int position) { value = null; if (position == 1) { VocabularyRegistry vr = GraphlRegistry.instance().getVocabularyRegistry(); value = element.getProperty(vr.expandPrefix(name.toString())); } return (value != null); } } --- NEW FILE: GraphNodePointer.java --- /* * Created on 15.12.2005 */ package org.mediavirus.graphl.jxpath; import org.apache.commons.jxpath.ri.QName; import org.apache.commons.jxpath.ri.compiler.NodeTest; import org.apache.commons.jxpath.ri.model.NodeIterator; import org.apache.commons.jxpath.ri.model.NodePointer; import org.mediavirus.graphl.GraphlRegistry; import org.mediavirus.graphl.graph.Node; import org.mediavirus.util.ParseUtils; public class GraphNodePointer extends NodePointer { private Node node; public GraphNodePointer(Node node) { super(null); this.node = node; } public GraphNodePointer(NodePointer parent, Node node) { super(parent); this.node = node; } public NodeIterator childIterator(NodeTest test, boolean reverse, NodePointer startWith) { return new GraphNodeIterator(this, test, reverse, startWith); } public NodeIterator attributeIterator(QName name) { return new GraphAttributeIterator(this, name); } @Override public int compareChildNodePointers(NodePointer pointer1, NodePointer pointer2) { return 0; } @Override public Object getBaseValue() { return node; } @Override public Object getImmediateNode() { return node; } @Override public int getLength() { return node.getEdgesFrom().size(); } @Override public QName getName() { String type = node.getType(); String ns = ParseUtils.guessNamespace(type); String prefix = GraphlRegistry.instance().getVocabularyRegistry().resolvePrefix(ns); String name = ParseUtils.guessName(type); return new QName(prefix, name); } @Override public boolean isCollection() { return false; } @Override public boolean isLeaf() { return false; } @Override public void setValue(Object value) { } public Node getNode() { return node; } } --- NEW FILE: GraphNodeIterator.java --- /* * Created on 15.12.2005 */ package org.mediavirus.graphl.jxpath; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.commons.jxpath.ri.QName; import org.apache.commons.jxpath.ri.compiler.NodeNameTest; import org.apache.commons.jxpath.ri.compiler.NodeTest; import org.apache.commons.jxpath.ri.model.NodeIterator; import org.apache.commons.jxpath.ri.model.NodePointer; import org.mediavirus.graphl.GraphlRegistry; import org.mediavirus.graphl.graph.Edge; import org.mediavirus.graphl.graph.Node; import org.mediavirus.util.ParseUtils; public class GraphNodeIterator implements NodeIterator { private NodePointer parent; private NodeTest nodeTest; private boolean reverse; private Node node = null; private Edge child = null; private int position = 0; List<Edge> passedEdges; public GraphNodeIterator(GraphNodePointer parent, NodeTest test, boolean reverse, NodePointer startWith) { this.parent = parent; this.node = (Node)parent.getNode(); this.nodeTest = test; this.reverse = reverse; if (startWith != null) { this.child = (Edge) startWith.getNode(); } prepare(); } protected void prepare() { passedEdges = new ArrayList<Edge>(); int i = 0; for (Iterator edges = node.getEdgesFrom().iterator(); edges.hasNext();) { Edge edge = (Edge) edges.next(); if (edge.equals(child)) position = i; if (testChild(edge)) { passedEdges.add(edge); i++; } } } public NodePointer getNodePointer() { if (child == null) { return null; } return new GraphEdgePointer(parent, child); } public int getPosition() { return position; } public boolean setPosition(int position) { if (reverse) position = passedEdges.size() - position; if (position > 0 && position <= passedEdges.size()) { child = passedEdges.get(position - 1); this.position = position; return true; } else { child = null; if (position > passedEdges.size()) position = passedEdges.size(); else position = 1; return false; } } private boolean testChild(Edge edge) { if (nodeTest == null) { return true; } else if (nodeTest instanceof NodeNameTest) { NodeNameTest nodeNameTest = (NodeNameTest) nodeTest; QName testName = nodeNameTest.getNodeName(); String namespaceURI = GraphlRegistry.instance().getVocabularyRegistry().resolveNamespace(testName.getPrefix()); boolean wildcard = nodeNameTest.isWildcard(); String testPrefix = testName.getPrefix(); if (wildcard && testPrefix == null) { return true; } if (wildcard || testName.getName().equals(ParseUtils.guessName(edge.getType()))) { String nodeNS = ParseUtils.guessNamespace(edge.getType()); return equalStrings(namespaceURI, nodeNS); } } return false; } private static boolean equalStrings(String s1, String s2) { if (s1 == null && s2 != null) { return false; } if (s1 != null && s2 == null) { return false; } if (s1 != null && !s1.trim().equals(s2.trim())) { return false; } return true; } } |