/** * Evaluate a compiled XPath expression against the retrieved document. * An XPath expression can return a Node or subinterface such as * Element or Text, a NodeList, or a Boolean, Number, or String value. * @param xpathExpression an XPath expression compiled previously * @param as the type of the value * @return the value produced by the XPath expression */ public <T> T evaluateXPath(XPathExpression xpathExpression, Class<T> as) throws XPathExpressionException { return evaluateXPath(xpathExpression, get(), as); } /**
/** * Evaluate a string XPath expression against the retrieved document. * An XPath expression can return a Node or subinterface such as * Element or Text, a NodeList, or a Boolean, Number, or String value. * @param xpathExpression the XPath expression as a string * @param as the type of the value * @return the value produced by the XPath expression */ public <T> T evaluateXPath(String xpathExpression, Class<T> as) throws XPathExpressionException { return evaluateXPath(xpathExpression, get(), as); } /**
/** * Evaluate a string XPath expression against the retrieved document. * An XPath expression can return a Node or subinterface such as * Element or Text, a NodeList, or a Boolean, Number, or String value. * @param xpathExpression the XPath expression as a string * @param as the type expected to be matched by the xpath * @param <T> the type to return * @return the value produced by the XPath expression * @throws XPathExpressionException if xpathExpression cannot be evaluated */ public <T> T evaluateXPath(String xpathExpression, Class<T> as) throws XPathExpressionException { return evaluateXPath(xpathExpression, get(), as); } /**
/** * Evaluate a compiled XPath expression against the retrieved document. * An XPath expression can return a Node or subinterface such as * Element or Text, a NodeList, or a Boolean, Number, or String value. * @param xpathExpression an XPath expression compiled previously * @param as the type expected to be matched by the xpath * @param <T> the type to return * @return the value produced by the XPath expression * @throws XPathExpressionException if xpathExpression cannot be evaluated */ public <T> T evaluateXPath(XPathExpression xpathExpression, Class<T> as) throws XPathExpressionException { return evaluateXPath(xpathExpression, get(), as); } /**
@Override public Document getRelevanceInfo() { DOMHandle handle = getRelevanceInfo(new DOMHandle()); return (handle == null) ? null : handle.get(); } @Override
@SuppressWarnings("unchecked") @Override public String[] suggest(SuggestDefinition suggestDef) { DOMHandle handle = new DOMHandle(); @SuppressWarnings("rawtypes") HandleImplementation suggestBase = HandleAccessor.checkHandle(handle, "suggest"); suggestBase.receiveContent(services.suggest(suggestBase.receiveAs(), suggestDef)); Document doc = handle.get(); NodeList nodeList = doc.getDocumentElement().getChildNodes(); List<String> suggestions = new ArrayList<String>(); for (int i=0; i <nodeList.getLength(); i++) { suggestions.add(nodeList.item(i).getTextContent()); } return suggestions.toArray(new String[suggestions.size()]); }
/** * Returns the query plan associated with this search. * * <p>Query plans are highly variable.</p> * @return the plan as a DOM Document */ @Override public Document getPlan() { DOMHandle handle = getPlan(new DOMHandle()); return (handle == null) ? null : handle.get(); } @Override
@Override public Document getMetadata() { DOMHandle handle = getMetadata(new DOMHandle()); return (handle == null) ? null : handle.get(); } @Override
@Override public Document getRelevanceInfo() { DOMHandle handle = getRelevanceInfo(new DOMHandle()); return (handle == null) ? null : handle.get(); } @Override
/** * Returns the query plan associated with this search. * * <p>Query plans are highly variable.</p> * @return the plan as a DOM Document */ @Override public Document getPlan() { DOMHandle handle = getPlan(new DOMHandle()); return (handle == null) ? null : handle.get(); } @Override
protected void receiveContent(InputStream content) { rules = new ArrayList<RuleDefinition>(); DOMHandle domHandle = new DOMHandle(); HandleAccessor.receiveContent(domHandle, content); Document document = domHandle.get(); NodeList ruleNodes = document.getElementsByTagNameNS(RequestConstants.RESTAPI_NS, "rule"); for (int i=0; i < ruleNodes.getLength(); i++) { Element ruleElement = (Element) ruleNodes.item(i); RuleDefinition ruleDefinition = new RuleDefinition(); ruleDefinition.receiveElement(ruleElement); rules.add(ruleDefinition); } } }
@SuppressWarnings("unchecked") @Override public String[] suggest(SuggestDefinition suggestDef) { DOMHandle handle = new DOMHandle(); @SuppressWarnings("rawtypes") HandleImplementation suggestBase = HandleAccessor.checkHandle(handle, "suggest"); suggestBase.receiveContent(services.suggest(suggestBase.receiveAs(), suggestDef)); Document doc = handle.get(); NodeList nodeList = doc.getDocumentElement().getChildNodes(); List<String> suggestions = new ArrayList<>(); for (int i=0; i <nodeList.getLength(); i++) { suggestions.add(nodeList.item(i).getTextContent()); } return suggestions.toArray(new String[suggestions.size()]); }
@Override public Document getMetadata() { DOMHandle handle = getMetadata(new DOMHandle()); return (handle == null) ? null : handle.get(); } @Override
protected void receiveContent(InputStream content) { DOMHandle handle = new DOMHandle(); HandleAccessor.receiveContent(handle, content); Element ruleElement = handle.get().getDocumentElement(); receiveElement(ruleElement); }
@Override protected void receiveContent(InputStream content) { DOMHandle handle = new DOMHandle(); HandleAccessor.receiveContent(handle, content); Element ruleElement = handle.get().getDocumentElement(); receiveElement(ruleElement); }
@Test public void testPostDocumentWithoutFormat() throws SAXException, IOException { StringHandle stringHandle = new StringHandle("<search xmlns=\"http://marklogic.com/appservices/search\"><qtext>true</qtext></search>"); DOMHandle answer = ruleManager.match(stringHandle, new DOMHandle()); Document doc = answer.get(); NodeList nl = doc.getElementsByTagNameNS( "http://marklogic.com/rest-api", "name"); assertEquals(2, nl.getLength()); }
@Test public void testMatchPostQuery() throws SAXException, IOException { StructuredQueryBuilder qb = new StructuredQueryBuilder(); StructuredQueryDefinition structuredQuery; structuredQuery = qb.rangeConstraint("favorited", Operator.EQ, "true"); DOMHandle answer = ruleManager.match(structuredQuery, new DOMHandle()); Document doc = answer.get(); NodeList nl = doc.getElementsByTagNameNS( "http://marklogic.com/rest-api", "name"); assertEquals(2, nl.getLength()); answer = ruleManager.match(structuredQuery, 1, QueryManager.DEFAULT_PAGE_LENGTH, new String[] { "favorites" }, new DOMHandle()); doc = answer.get(); nl = doc.getElementsByTagNameNS("http://marklogic.com/rest-api", "name"); assertEquals(1, nl.getLength()); }
@Test public void testRuleMatchTransform() { StructuredQueryBuilder qb = new StructuredQueryBuilder(); StructuredQueryDefinition structuredQuery; structuredQuery = qb.rangeConstraint("favorited", Operator.EQ, "true"); ServerTransform transform = new ServerTransform("ruleTransform"); DOMHandle answer = ruleManager.match(structuredQuery, 0L, QueryManager.DEFAULT_PAGE_LENGTH, new String[] {}, new DOMHandle(), transform); Document doc = answer.get(); NodeList nl = doc.getElementsByTagNameNS( "", "transformed-name"); assertEquals(2, nl.getLength()); }
protected Document getModulesDocument(String uri) { return modMgr.read(uri).next().getContent(new DOMHandle()).get(); }
@Test public void testExplain() throws IOException { RowManager rowMgr = Common.client.newRowManager(); PlanBuilder p = rowMgr.newPlanBuilder(); PlanBuilder.ExportablePlan builtPlan = p.fromLiterals(litRows); JsonNode jsonRoot = rowMgr.explain(builtPlan, new JacksonHandle()).get(); assertNotNull(jsonRoot); jsonRoot = rowMgr.explainAs(builtPlan, JsonNode.class); assertNotNull(jsonRoot); Document xmlRoot = rowMgr.explain(builtPlan, new DOMHandle()).get(); assertNotNull(xmlRoot); xmlRoot = rowMgr.explainAs(builtPlan, Document.class); assertNotNull(xmlRoot); String stringRoot = rowMgr.explain(builtPlan, new StringHandle()).get(); assertNotNull(new ObjectMapper().readTree(stringRoot)); } private DOMHandle initNamespaces(DOMHandle handle) {