List of usage examples for javax.xml.xpath XPathConstants STRING
QName STRING
To view the source code for javax.xml.xpath XPathConstants STRING.
Click Source Link
The XPath 1.0 string data type.
Maps to Java String .
From source file:edu.sabanciuniv.sentilab.sare.controllers.opinion.OpinionCorpusFactory.java
@Override protected OpinionCorpusFactory addXmlPacket(OpinionCorpus corpus, InputStream input) throws ParserConfigurationException, SAXException, IOException, XPathException { Validate.notNull(corpus, CannedMessages.NULL_ARGUMENT, "corpus"); Validate.notNull(input, CannedMessages.NULL_ARGUMENT, "input"); DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true);//from www .j a va 2s.co m Document doc = domFactory.newDocumentBuilder().parse(input); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); OpinionDocumentFactory opinionFactory; if ("document".equals(doc.getDocumentElement().getLocalName())) { opinionFactory = new OpinionDocumentFactory().setCorpus(corpus).setXmlNode(doc.getDocumentElement()); corpus.addDocument(opinionFactory.create()); return this; } Node corpusNode = (Node) xpath.compile("/corpus").evaluate(doc, XPathConstants.NODE); if (corpusNode == null) { corpusNode = Validate.notNull(doc.getDocumentElement(), CannedMessages.NULL_ARGUMENT, "/corpus"); } String title = (String) xpath.compile("./@title").evaluate(corpusNode, XPathConstants.STRING); String description = (String) xpath.compile("./@description").evaluate(corpusNode, XPathConstants.STRING); String language = (String) xpath.compile("./@language").evaluate(corpusNode, XPathConstants.STRING); if (StringUtils.isNotEmpty(title)) { corpus.setTitle(title); } if (StringUtils.isNotEmpty(description)) { corpus.setDescription(description); } if (StringUtils.isNotEmpty(language)) { corpus.setLanguage(language); } NodeList documentNodes = (NodeList) xpath.compile("./document").evaluate(corpusNode, XPathConstants.NODESET); if (documentNodes == null || documentNodes.getLength() == 0) { documentNodes = corpusNode.getChildNodes(); Validate.isTrue(documentNodes != null && documentNodes.getLength() > 0, CannedMessages.NULL_ARGUMENT, "/corpus/document"); } for (int index = 0; index < documentNodes.getLength(); index++) { opinionFactory = new OpinionDocumentFactory().setCorpus(corpus).setXmlNode(documentNodes.item(index)); corpus.addDocument(opinionFactory.create()); } return this; }
From source file:edu.sabanciuniv.sentilab.sare.controllers.aspect.AspectLexiconFactory.java
@Override protected AspectLexiconFactory addXmlPacket(AspectLexicon lexicon, InputStream input) throws ParserConfigurationException, SAXException, IOException, XPathException { Validate.notNull(lexicon, CannedMessages.NULL_ARGUMENT, "lexicon"); Validate.notNull(input, CannedMessages.NULL_ARGUMENT, "input"); DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true);/*w w w.jav a 2 s. c om*/ Document doc = domFactory.newDocumentBuilder().parse(input); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); if ("aspect".equalsIgnoreCase(doc.getDocumentElement().getLocalName())) { return this.addXmlAspect(lexicon, doc.getDocumentElement()); } Node lexiconNode = (Node) xpath.compile("/aspect-lexicon").evaluate(doc, XPathConstants.NODE); if (lexiconNode == null) { lexiconNode = Validate.notNull(doc.getDocumentElement(), CannedMessages.NULL_ARGUMENT, "/aspect-lexicon"); } String title = (String) xpath.compile("./@title").evaluate(lexiconNode, XPathConstants.STRING); String description = (String) xpath.compile("./@description").evaluate(lexiconNode, XPathConstants.STRING); if (StringUtils.isNotEmpty(title)) { lexicon.setTitle(title); } if (StringUtils.isNotEmpty(description)) { lexicon.setDescription(description); } return this.addXmlAspect(lexicon, lexiconNode); }
From source file:de.egore911.versioning.deployer.performer.PerformCopy.java
public void perform(Node serverDeploymentsDeploymentNode) throws XPathExpressionException { NodeList copyOperations = (NodeList) copyXpath.evaluate(serverDeploymentsDeploymentNode, XPathConstants.NODESET); for (int j = 0; j < copyOperations.getLength(); j++) { Node copyOperation = copyOperations.item(j); String uri = (String) urlXpath.evaluate(copyOperation, XPathConstants.STRING); String target = (String) targetXpath.evaluate(copyOperation, XPathConstants.STRING); String targetFilename = (String) filenameXpath.evaluate(copyOperation, XPathConstants.STRING); if (!copy(uri, target, targetFilename)) { LOG.error("Failed copy operation"); }//from ww w. j av a2s.c o m } }
From source file:org.opencastproject.remotetest.util.WorkflowUtils.java
/** * Checks whether the given workflow is in the requested operation. * //from w w w.j a va 2 s .c om * @param workflowId * identifier of the workflow * @param operation * the operation that the workflow is expected to be in * @return <code>true</code> if the workflow is in the expected state * @throws IllegalStateException * if the specified workflow can't be found */ public static boolean isWorkflowInOperation(String workflowId, String operation) throws IllegalStateException, Exception { HttpGet getWorkflowMethod = new HttpGet(BASE_URL + "/workflow/instance/" + workflowId + ".xml"); TrustedHttpClient client = Main.getClient(); String workflow = EntityUtils.toString(client.execute(getWorkflowMethod).getEntity()); String currentOperation = (String) Utils.xpath(workflow, "//*[local-name() = 'operation'][@state='PAUSED']/@id", XPathConstants.STRING); Main.returnClient(client); return operation.equalsIgnoreCase(currentOperation); }
From source file:net.adamcin.commons.testing.sling.SlingPostResponse.java
public static SlingPostResponse createFromInputStream(InputStream stream, String encoding) throws IOException { InputSource source = new InputSource(new BufferedInputStream(stream)); source.setEncoding(encoding == null ? "UTF-8" : encoding); SlingPostResponse postResponse = new SlingPostResponse(); XPathFactory xpf = XPathFactory.newInstance(); XPath xpath = xpf.newXPath(); try {/* w ww. j a va 2 s. c o m*/ DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(source); postResponse .setStatus((String) xpath.compile(XPATH_ID_STATUS).evaluate(document, XPathConstants.STRING)); postResponse .setMessage((String) xpath.compile(XPATH_ID_MESSAGE).evaluate(document, XPathConstants.STRING)); postResponse.setLocation( (String) xpath.compile(XPATH_ID_LOCATION).evaluate(document, XPathConstants.STRING)); postResponse.setParentLocation( (String) xpath.compile(XPATH_ID_PARENT_LOCATION).evaluate(document, XPathConstants.STRING)); postResponse.setPath((String) xpath.compile(XPATH_ID_PATH).evaluate(document, XPathConstants.STRING)); postResponse .setReferer((String) xpath.compile(XPATH_ID_REFERER).evaluate(document, XPathConstants.STRING)); List<Change> changes = new ArrayList<Change>(); NodeList changeLogNodes = (NodeList) xpath.compile(XPATH_ID_CHANGE_LOG).evaluate(document, XPathConstants.NODESET); if (changeLogNodes != null) { for (int i = 0; i < changeLogNodes.getLength(); i++) { String rawChange = changeLogNodes.item(i).getTextContent(); rawChange = rawChange.substring(0, rawChange.length() - 2); String[] rawChangeParts = rawChange.split("\\(", 2); if (rawChangeParts.length != 2) { continue; } String changeType = rawChangeParts[0]; String[] rawArguments = rawChangeParts[1].split(", "); List<String> arguments = new ArrayList<String>(); for (String rawArgument : rawArguments) { arguments.add(rawArgument.substring(1, rawArgument.length() - 1)); } Change change = new Change(changeType, arguments.toArray(new String[arguments.size()])); changes.add(change); } } postResponse.setChangeLog(changes); } catch (XPathExpressionException e) { LOGGER.error("Failed to evaluate xpath statement.", e); throw new IOException("Failed to evaluate xpath statement.", e); } catch (ParserConfigurationException e) { LOGGER.error("Failed to create DocumentBuilder.", e); throw new IOException("Failed to create DocumentBuilder.", e); } catch (SAXException e) { LOGGER.error("Failed to create Document.", e); throw new IOException("Failed to create Document.", e); } LOGGER.info("Returning post response"); return postResponse; }
From source file:com.esri.geoportal.commons.csw.client.impl.ProfilesLoader.java
/** * Loads profiles.//from w w w . jav a2 s . c o m * @return profiles * @throws IOException if loading profiles from configuration fails * @throws ParserConfigurationException if unable to obtain XML parser * @throws SAXException if unable to parse XML document * @throws XPathExpressionException if invalid XPath expression */ public Profiles load() throws IOException, ParserConfigurationException, SAXException, XPathExpressionException { LOG.info(String.format("Loading CSW profiles")); Profiles profiles = new Profiles(); try (InputStream profilesXml = Thread.currentThread().getContextClassLoader() .getResourceAsStream(CONFIG_FILE_PATH);) { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); Document profilesDom = builder.parse(new InputSource(profilesXml)); XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); NodeList profilesNodeList = (NodeList) xpath.evaluate("/CSWProfiles/Profile", profilesDom, XPathConstants.NODESET); for (int pidx = 0; pidx < profilesNodeList.getLength(); pidx++) { Node profileNode = profilesNodeList.item(pidx); String id = StringUtils .trimToEmpty((String) xpath.evaluate("ID", profileNode, XPathConstants.STRING)); String name = StringUtils .trimToEmpty((String) xpath.evaluate("Name", profileNode, XPathConstants.STRING)); String namespace = StringUtils .trimToEmpty((String) xpath.evaluate("CswNamespace", profileNode, XPathConstants.STRING)); String description = StringUtils .trimToEmpty((String) xpath.evaluate("Description", profileNode, XPathConstants.STRING)); String getRecordsReqXslt = StringUtils.trimToEmpty((String) xpath .evaluate("GetRecords/XSLTransformations/Request", profileNode, XPathConstants.STRING)); String getRecordsRspXslt = StringUtils.trimToEmpty((String) xpath .evaluate("GetRecords/XSLTransformations/Response", profileNode, XPathConstants.STRING)); String getRecordByIdReqKVP = StringUtils.trimToEmpty( (String) xpath.evaluate("GetRecordByID/RequestKVPs", profileNode, XPathConstants.STRING)); String getRecordByIdRspXslt = StringUtils.trimToEmpty((String) xpath .evaluate("GetRecordByID/XSLTransformations/Response", profileNode, XPathConstants.STRING)); Profile prof = new Profile(); prof.setId(id); prof.setName(name); prof.setDescription(description); prof.setGetRecordsReqXslt(getRecordsReqXslt); prof.setGetRecordsRspXslt(getRecordsRspXslt); prof.setKvp(getRecordByIdReqKVP); prof.setGetRecordByIdRspXslt(getRecordByIdRspXslt); profiles.add(prof); } } LOG.info(String.format("CSW profiles loaded.")); return profiles; }
From source file:com.espertech.esper.regression.event.TestNoSchemaXMLEvent.java
public void testVariableAndDotMethodResolution() throws Exception { // test for ESPER-341 Configuration configuration = SupportConfigFactory.getConfiguration(); configuration.addVariable("var", int.class, 0); ConfigurationEventTypeXMLDOM xmlDOMEventTypeDesc = new ConfigurationEventTypeXMLDOM(); xmlDOMEventTypeDesc.setRootElementName("myevent"); xmlDOMEventTypeDesc.addXPathProperty("xpathAttrNum", "/myevent/@attrnum", XPathConstants.STRING, "long"); xmlDOMEventTypeDesc.addXPathProperty("xpathAttrNumTwo", "/myevent/@attrnumtwo", XPathConstants.STRING, "long"); configuration.addEventType("TestXMLNoSchemaType", xmlDOMEventTypeDesc); epService = EPServiceProviderManager.getProvider("TestNoSchemaXML", configuration); epService.initialize();/*w w w . j a v a2 s . co m*/ updateListener = new SupportUpdateListener(); String stmtTextOne = "select var, xpathAttrNum.after(xpathAttrNumTwo) from TestXMLNoSchemaType.win:length(100)"; epService.getEPAdministrator().createEPL(stmtTextOne); }
From source file:org.opencastproject.remotetest.util.CaptureUtils.java
public static boolean isInState(String recordingId, String state) throws Exception { HttpGet request = new HttpGet(BASE_URL + "/capture-admin/recordings/" + recordingId); TrustedHttpClient client = Main.getClient(); HttpResponse response = client.execute(request); if (HttpStatus.SC_OK != response.getStatusLine().getStatusCode()) throw new IllegalStateException("Recording '" + recordingId + "' not found"); String responseBody = EntityUtils.toString(response.getEntity()); Main.returnClient(client);//from ww w . j a va 2 s . c om return state.equalsIgnoreCase( (String) Utils.xpath(responseBody, "//*[local-name() = 'state']", XPathConstants.STRING)); }
From source file:it.intecs.pisa.openCatalogue.solr.SolrHandler.java
public SaxonDocument search(HashMap<String, String> request) throws UnsupportedEncodingException, IOException, SaxonApiException, Exception { HttpClient client = new HttpClient(); HttpMethod method;//from w w w.j a va 2 s . c o m String urlStr = prepareUrl(request); Log.debug("The following search is goint to be executed:" + urlStr); // Create a method instance. method = new GetMethod(urlStr); // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); // Execute the method. int statusCode = client.executeMethod(method); SaxonDocument solrResponse = new SaxonDocument(method.getResponseBodyAsString()); //Log.debug(solrResponse.getXMLDocumentString()); if (statusCode != HttpStatus.SC_OK) { Log.error("Method failed: " + method.getStatusLine()); String errorMessage = (String) solrResponse.evaluatePath("//lst[@name='error']/str[@name='msg']/text()", XPathConstants.STRING); throw new Exception(errorMessage); } return solrResponse; }
From source file:de.egore911.versioning.deployer.performer.PerformReplacement.java
public void perform(Node serverDeploymentsDeploymentNode) throws XPathExpressionException { NodeList replacementOperations = (NodeList) replaceXpath.evaluate(serverDeploymentsDeploymentNode, XPathConstants.NODESET); for (int j = 0; j < replacementOperations.getLength(); j++) { Node replacementOperation = replacementOperations.item(j); String basepath = (String) basepathXpath.evaluate(replacementOperation, XPathConstants.STRING); NodeList wildcardNodes = (NodeList) wildcardsWildcardXpath.evaluate(replacementOperation, XPathConstants.NODESET); List<String> wildcards = new ArrayList<>(); for (int k = 0; k < wildcardNodes.getLength(); k++) { Node wildcard = wildcardNodes.item(k); wildcards.add(wildcard.getNodeValue()); }//from w ww . java2 s . c om NodeList replacementsNodes = (NodeList) replacementsReplacementXpath.evaluate(replacementOperation, XPathConstants.NODESET); List<ReplacementPair> replacements = new ArrayList<>(); for (int k = 0; k < replacementsNodes.getLength(); k++) { Node replacement = replacementsNodes.item(k); String variable = (String) variableXpath.evaluate(replacement, XPathConstants.STRING); String value = (String) valueXpath.evaluate(replacement, XPathConstants.STRING); replacements.add(new ReplacementPair(variable, value)); } NodeList replacementfilesNodes = (NodeList) replacementfileReplacementfileXpath .evaluate(replacementOperation, XPathConstants.NODESET); List<File> replacementfiles = new ArrayList<>(); for (int k = 0; k < replacementfilesNodes.getLength(); k++) { Node replacementfile = replacementfilesNodes.item(k); File file = new File(replacementfile.getNodeValue()); if (!file.exists()) { LOG.error("Could not find file {}, replacements will be likely incomplete", file); } replacementfiles.add(file); } perform(new File(basepath), wildcards, replacements, replacementfiles); } }