You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-dev@xml.apache.org by ks...@apache.org on 2002/01/16 10:48:39 UTC
cvs commit: xml-xindice/java/src/org/apache/xindice/xml NamespaceMap.java
kstaken 02/01/16 01:48:39
Modified: java/src/org/apache/xindice/client/corba
CollectionServant.java
java/src/org/apache/xindice/client/xmldb/services
XUpdateQueryServiceImpl.java
java/src/org/apache/xindice/core/xupdate XUpdateImpl.java
XUpdateQueryResolver.java
java/src/org/apache/xindice/xml NamespaceMap.java
Added: java/lib xmldb-xupdate.jar
Removed: java/lib infozone-lexus.jar
Log:
Adding better naespace support for XUpdate operations.
Submitted by: Timothy Dean
Reviewed by: Kimbro Staken
Revision Changes Path
1.1 xml-xindice/java/lib/xmldb-xupdate.jar
<<Binary file>>
1.2 +2 -2 xml-xindice/java/src/org/apache/xindice/client/corba/CollectionServant.java
Index: CollectionServant.java
===================================================================
RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/client/corba/CollectionServant.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CollectionServant.java 6 Dec 2001 19:33:53 -0000 1.1
+++ CollectionServant.java 16 Jan 2002 09:48:39 -0000 1.2
@@ -56,7 +56,7 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- * $Id: CollectionServant.java,v 1.1 2001/12/06 19:33:53 bradford Exp $
+ * $Id: CollectionServant.java,v 1.2 2002/01/16 09:48:39 kstaken Exp $
*/
import org.apache.xindice.client.corba.db.*;
@@ -414,7 +414,7 @@
*/
public org.apache.xindice.client.corba.db.EncodedBuffer queryCollection(String style,
String query, NamedVal[] namespaces, long stamp)
- throws APIException {
+ throws APIException {
try {
NamespaceMap nsMap = null;
if ( namespaces.length > 0 ) {
1.2 +67 -5 xml-xindice/java/src/org/apache/xindice/client/xmldb/services/XUpdateQueryServiceImpl.java
Index: XUpdateQueryServiceImpl.java
===================================================================
RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/client/xmldb/services/XUpdateQueryServiceImpl.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- XUpdateQueryServiceImpl.java 6 Dec 2001 19:33:54 -0000 1.1
+++ XUpdateQueryServiceImpl.java 16 Jan 2002 09:48:39 -0000 1.2
@@ -56,7 +56,7 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- * $Id: XUpdateQueryServiceImpl.java,v 1.1 2001/12/06 19:33:54 bradford Exp $
+ * $Id: XUpdateQueryServiceImpl.java,v 1.2 2002/01/16 09:48:39 kstaken Exp $
*/
import org.apache.xindice.core.FaultCodes;
@@ -74,11 +74,13 @@
import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
+import java.util.*;
+
public class XUpdateQueryServiceImpl extends CommonConfigurable
implements org.xmldb.api.modules.XUpdateQueryService
{
- private static final NamedVal[] EmptyNamedVals = new NamedVal[0];
protected org.xmldb.api.base.Collection collection = null;
+ private NamespaceMap nsMap = new NamespaceMap();
public XUpdateQueryServiceImpl() {
}
@@ -96,15 +98,75 @@
this.collection = col;
}
+
+ public void setDefaultNamespace(String uri) throws XMLDBException {
+ if ( uri != null )
+ nsMap.setDefaultNamespace(uri);
+ else
+ nsMap.removeDefaultNamespace();
+ }
+
+ public void removeDefaultNamespace() {
+ nsMap.removeDefaultNamespace();
+ }
+
+ public void setNamespace(String prefix, String uri) throws XMLDBException {
+ if ( ( prefix == null ) || prefix.equals("") ) {
+ setDefaultNamespace(uri);
+ }
+ else {
+ if ( uri != null ) {
+ nsMap.setNamespace(prefix, uri);
+ }
+ }
+ }
+
+ public void removeNamespace(String prefix) {
+ if ( ( prefix == null ) || prefix.equals("") ) {
+ removeDefaultNamespace();
+ }
+
+ nsMap.removeNamespace(prefix);
+ }
+
+ public String getDefaultNamespace() {
+ return nsMap.getDefaultNamespaceURI();
+ }
+
+ public String getNamespace(String prefix) {
+ if ( ( prefix == null ) || prefix.equals("") ) {
+ return nsMap.getDefaultNamespaceURI();
+ }
+
+ return nsMap.getNamespaceURI(prefix);
+ }
+
+ public void clearNamespaces() {
+ nsMap.clear();
+ }
+
+ private NamedVal[] getNamespaces() {
+ NamedVal[] nv = new NamedVal[nsMap.size()];
+ Iterator iter = nsMap.keySet().iterator();
+ int i = 0;
+ while ( iter.hasNext() ) {
+ String prefix = (String)iter.next();
+ String uri = nsMap.getNamespaceURI(prefix);
+ nv[i++] = new NamedVal(prefix, uri);
+ }
+ return nv;
+ }
+
public XMLResource updateResult(String commands) throws XMLDBException {
// Get the Xindice collection object from the XML:DB collection wrapper.
Collection col = ((CollectionImpl) collection).getServerObject();
try {
- EncodedBuffer buffer = col.queryCollection("XUpdate", commands, EmptyNamedVals, -1);
+ EncodedBuffer buffer = col.queryCollection("XUpdate", commands, getNamespaces(), -1);
return new XMLResourceImpl("", collection, new String(buffer.buf));
}
catch (Exception e) {
+ e.printStackTrace();
throw FaultCodes.createXMLDBException(e);
}
}
@@ -129,10 +191,10 @@
Collection col = ((CollectionImpl) collection).getServerObject();
try {
- EncodedBuffer buffer = col.queryDocument("XUpdate", commands, EmptyNamedVals, id, -1);
+ EncodedBuffer buffer = col.queryDocument("XUpdate", commands, getNamespaces(), id, -1);
return new XMLResourceImpl("", collection, new String(buffer.buf));
}
- catch (Exception e) {
+ catch (Exception e) {
throw FaultCodes.createXMLDBException(e);
}
}
1.2 +45 -6 xml-xindice/java/src/org/apache/xindice/core/xupdate/XUpdateImpl.java
Index: XUpdateImpl.java
===================================================================
RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/core/xupdate/XUpdateImpl.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- XUpdateImpl.java 6 Dec 2001 21:00:15 -0000 1.1
+++ XUpdateImpl.java 16 Jan 2002 09:48:39 -0000 1.2
@@ -56,7 +56,7 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- * $Id: XUpdateImpl.java,v 1.1 2001/12/06 21:00:15 bradford Exp $
+ * $Id: XUpdateImpl.java,v 1.2 2002/01/16 09:48:39 kstaken Exp $
*/
import org.apache.xindice.core.Collection;
@@ -64,12 +64,15 @@
import org.apache.xindice.xml.dom.*;
import org.apache.xindice.xml.*;
-import org.infozone.lexus.commands.*;
-import org.infozone.lexus.*;
+import org.xmldb.xupdate.lexus.commands.*;
+import org.xmldb.xupdate.lexus.*;
+import org.infozone.tools.xml.queries.*;
import java.util.*;
import org.w3c.dom.*;
+import org.xmldb.xupdate.lexus.*;
+import org.xml.sax.*;
/**
* Provides Collection and document based XUpdate capabilities.
@@ -79,8 +82,43 @@
*/
public class XUpdateImpl extends XUpdateQueryImpl {
protected int nodesModified = 0;
-
- /**
+ protected NamespaceMap nsMap;
+
+ /**
+ * If set to true, then namespaces set explicitly via an API call will take precendence.
+ * If set to false, then namespaces set implicitly within query string will take precedence.
+ */
+ private static final boolean API_NS_PRECEDENCE = true;
+
+ /**
+ * Set the namespace map to be used when resolving queries
+ */
+ public void setNamespaceMap(NamespaceMap nsMap) {
+ if (nsMap == null)
+ return;
+
+ if (this.nsMap == null) {
+ this.nsMap = nsMap;
+ }
+ else {
+ this.nsMap.includeNamespaces(nsMap, API_NS_PRECEDENCE);
+ }
+ }
+
+
+ /**
+ * Sets the query string to be used when executing update
+ */
+ public void setQString(String query) throws SAXException {
+ super.setQString(query);
+ if (nsMap == null) {
+ nsMap = new NamespaceMap();
+ }
+ nsMap.includeNamespaces(_namespaces, !API_NS_PRECEDENCE);
+ }
+
+
+ /**
* Execute the XUpdate commands against a document.
*/
public void execute(Node contextNode) throws Exception {
@@ -89,6 +127,7 @@
Enumeration attributes = _query[1].elements();
Enumeration characters = _query[2].elements();
Node origNode = contextNode;
+ CommandObject.getXPath().setNamespace(nsMap.getContextNode());
while (commands.hasMoreElements()) {
int id = ((Integer) commands.nextElement()).intValue();
@@ -143,7 +182,7 @@
// If we found an XPath selector we need to execute the commands.
if (selector != null) {
- NodeSet ns = col.queryCollection("XPath", selector, null);
+ NodeSet ns = col.queryCollection("XPath", selector, nsMap);
Document lastDoc = null;
while (ns != null && ns.hasMoreNodes()) {
DBNode node = (DBNode)ns.getNextNode();
1.2 +2 -1 xml-xindice/java/src/org/apache/xindice/core/xupdate/XUpdateQueryResolver.java
Index: XUpdateQueryResolver.java
===================================================================
RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/core/xupdate/XUpdateQueryResolver.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- XUpdateQueryResolver.java 6 Dec 2001 21:00:15 -0000 1.1
+++ XUpdateQueryResolver.java 16 Jan 2002 09:48:39 -0000 1.2
@@ -56,7 +56,7 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- * $Id: XUpdateQueryResolver.java,v 1.1 2001/12/06 21:00:15 bradford Exp $
+ * $Id: XUpdateQueryResolver.java,v 1.2 2002/01/16 09:48:39 kstaken Exp $
*/
import org.apache.xindice.core.*;
@@ -127,6 +127,7 @@
try {
xu = new XUpdateImpl();
xu.setQString(query);
+ xu.setNamespaceMap(nsMap);
}
catch ( Exception e ) {
throw new CompilationException("Error Compiling XUpdate Query");
1.2 +19 -3 xml-xindice/java/src/org/apache/xindice/xml/NamespaceMap.java
Index: NamespaceMap.java
===================================================================
RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/xml/NamespaceMap.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- NamespaceMap.java 6 Dec 2001 19:33:58 -0000 1.1
+++ NamespaceMap.java 16 Jan 2002 09:48:39 -0000 1.2
@@ -56,7 +56,7 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- * $Id: NamespaceMap.java,v 1.1 2001/12/06 19:33:58 bradford Exp $
+ * $Id: NamespaceMap.java,v 1.2 2002/01/16 09:48:39 kstaken Exp $
*/
import java.util.*;
@@ -72,7 +72,7 @@
public final class NamespaceMap extends HashMap {
private Element elem = null;
-
+
public Node getContextNode() {
if ( elem == null ) {
Document d = new DocumentImpl();
@@ -92,8 +92,9 @@
public void clearNamespaces() {
clear();
+ elem = null;
}
-
+
public String getDefaultNamespaceURI() {
return (String)get("");
}
@@ -104,17 +105,32 @@
public void setDefaultNamespace(String uri) {
put("", uri);
+ elem = null;
}
public void setNamespace(String prefix, String uri) {
put(prefix, uri);
+ elem = null;
}
public void removeDefaultNamespace() {
remove("");
+ elem = null;
}
public void removeNamespace(String prefix) {
remove(prefix);
+ elem = null;
+ }
+
+ public void includeNamespaces(Map nsMap, boolean override) {
+ Iterator newEntries = nsMap.entrySet().iterator();
+ while (newEntries.hasNext()) {
+ Map.Entry entry = (Map.Entry) newEntries.next();
+ if (!override && super.containsKey(entry.getKey()))
+ continue;
+ super.put(entry.getKey(), entry.getValue());
+ }
+ elem = null;
}
}