From: <del...@us...> - 2008-12-30 20:51:08
|
Revision: 8447 http://exist.svn.sourceforge.net/exist/?rev=8447&view=rev Author: deliriumsky Date: 2008-12-30 20:51:04 +0000 (Tue, 30 Dec 2008) Log Message: ----------- Support for namespaces in the POST Query message Modified Paths: -------------- branches/allad/jsr-225/src/org/exist/http/RESTServer.java Modified: branches/allad/jsr-225/src/org/exist/http/RESTServer.java =================================================================== --- branches/allad/jsr-225/src/org/exist/http/RESTServer.java 2008-12-30 20:44:51 UTC (rev 8446) +++ branches/allad/jsr-225/src/org/exist/http/RESTServer.java 2008-12-30 20:51:04 UTC (rev 8447) @@ -34,8 +34,10 @@ import java.io.Writer; import java.net.URI; import java.net.URLDecoder; +import java.util.ArrayList; import java.util.Date; import java.util.Iterator; +import java.util.List; import java.util.Properties; import javax.servlet.http.HttpServletRequest; @@ -111,6 +113,7 @@ import org.xml.sax.SAXParseException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.AttributesImpl; +import org.xml.sax.helpers.XMLFilterImpl; /** * @@ -248,10 +251,15 @@ query = request.getParameter("_query"); String _var = request.getParameter("_variables"); + List /*<Namespace>*/ namespaces = null; NodeImpl variables = null; try { if (_var != null) - variables = parseXML(_var); + { + NamespaceExtractor nsExtractor = new NamespaceExtractor(); + variables = parseXML(_var, nsExtractor); + namespaces = nsExtractor.getNamespaces(); + } } catch (SAXException e) { XPathException x = new XPathException(e.toString()); EXistServlet.SendXMLResponse(request, response, @@ -330,7 +338,7 @@ // query parameter specified, search method does all the rest of the // work try { - search(broker, query, path, variables, howmany, start, typed, + search(broker, query, path, namespaces, variables, howmany, start, typed, outputProperties, wrap, cache, request, response); } catch (XPathException e) { @@ -659,7 +667,8 @@ Txn transaction = null; try { String content = getRequestContent(request); - Element root = parseXML(content); + NamespaceExtractor nsExtractor = new NamespaceExtractor(); + Element root = parseXML(content, nsExtractor); String rootNS = root.getNamespaceURI(); if (rootNS != null && rootNS.equals(Namespaces.EXIST_NS)) { if (root.getLocalName().equals("query")) { @@ -752,7 +761,7 @@ if (query != null) { String result; try { - search(broker, query, path, variables, howmany, start, + search(broker, query, path, nsExtractor.getNamespaces(), variables, howmany, start, typed, outputProperties, enclose, cache, request, response); } catch (XPathException xpe) { @@ -847,7 +856,7 @@ } } - private ElementImpl parseXML(String content) + private ElementImpl parseXML(String content, NamespaceExtractor nsExtractor) throws ParserConfigurationException, SAXException, IOException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); @@ -855,14 +864,54 @@ SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); SAXAdapter adapter = new SAXAdapter(); - reader.setContentHandler(adapter); - reader.parse(src); - + //reader.setContentHandler(adapter); + //reader.parse(src); + nsExtractor.setContentHandler(adapter); + nsExtractor.setParent(reader); + nsExtractor.parse(src); + Document doc = adapter.getDocument(); return (ElementImpl) doc.getDocumentElement(); } + private class NamespaceExtractor extends XMLFilterImpl + { + List namespaces = new ArrayList(); //<Namespace> + + public void startPrefixMapping(String prefix, String uri) throws SAXException + { + Namespace ns = new Namespace(prefix, uri); + namespaces.add(ns); + super.startPrefixMapping(prefix, uri); + } + + public List getNamespaces() + { + return namespaces; + } + } + + private class Namespace + { + private String prefix = null; + private String uri = null; + + public Namespace(String prefix, String uri) + { + this.prefix = prefix; + this.uri = uri; + } + + public String getPrefix() { + return prefix; + } + + public String getUri() { + return uri; + } + } + /** * Creates an input source from a URL location with an optional known * charset. @@ -1073,7 +1122,7 @@ * @throws XPathException */ protected void search(DBBroker broker, String query, String path, - Node variables, int howmany, int start, boolean typed, + List /*<Namespace>*/namespaces, Node variables, int howmany, int start, boolean typed, Properties outputProperties, boolean wrap, boolean cache, HttpServletRequest request, HttpServletResponse response) throws BadRequestException, PermissionDeniedException, @@ -1113,6 +1162,7 @@ context = compiled.getContext(); context.setStaticallyKnownDocuments(new XmldbURI[] { pathUri }); context.setBaseURI(new AnyURIValue(pathUri.toString())); + declareNamespace(context, namespaces); declareVariables(context, variables, request, response); if (compiled == null) @@ -1147,6 +1197,18 @@ } } + private void declareNamespace(XQueryContext context, List/*<Namespace>*/ namespaces) throws XPathException + { + if(namespaces == null) + return; + + for(int i = 0; i < namespaces.size(); i++) + { + Namespace ns = (Namespace)namespaces.get(i); + context.declareNamespace(ns.getPrefix(), ns.getUri()); + } + } + /** * Pass the request, response and session objects to the XQuery context. * This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |