Search in sources :

Example 51 with XPathFactory

use of javax.xml.xpath.XPathFactory in project buck by facebook.

the class SchemeGeneratorTest method allActionsShouldBePresentInSchemeWithDefaultBuildConfigurations.

@Test
public void allActionsShouldBePresentInSchemeWithDefaultBuildConfigurations() throws Exception {
    ImmutableMap.Builder<PBXTarget, Path> targetToProjectPathMapBuilder = ImmutableMap.builder();
    PBXTarget rootTarget = new PBXNativeTarget("rootRule");
    rootTarget.setGlobalID("rootGID");
    rootTarget.setProductReference(new PBXFileReference("root.a", "root.a", PBXReference.SourceTree.BUILT_PRODUCTS_DIR, Optional.empty()));
    rootTarget.setProductType(ProductType.STATIC_LIBRARY);
    Path pbxprojectPath = Paths.get("foo/Foo.xcodeproj/project.pbxproj");
    targetToProjectPathMapBuilder.put(rootTarget, pbxprojectPath);
    SchemeGenerator schemeGenerator = new SchemeGenerator(projectFilesystem, Optional.of(rootTarget), ImmutableSet.of(rootTarget), ImmutableSet.of(), ImmutableSet.of(), "TestScheme", Paths.get("_gen/Foo.xcworkspace/scshareddata/xcshemes"), false, /* primaryTargetIsBuildWithBuck */
    false, /* parallelizeBuild */
    Optional.empty(), /* runnablePath */
    Optional.empty(), /* remoteRunnablePath */
    SchemeActionType.DEFAULT_CONFIG_NAMES, targetToProjectPathMapBuilder.build(), XCScheme.LaunchAction.LaunchStyle.AUTO);
    Path schemePath = schemeGenerator.writeScheme();
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document scheme = dBuilder.parse(projectFilesystem.newFileInputStream(schemePath));
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath schemeChildrenXPath = xpathFactory.newXPath();
    XPathExpression schemeChildrenExpr = schemeChildrenXPath.compile("/Scheme/node()");
    NodeList actions = (NodeList) schemeChildrenExpr.evaluate(scheme, XPathConstants.NODESET);
    assertThat(actions.getLength(), equalTo(6));
    Node buildAction = actions.item(0);
    assertThat(buildAction.getNodeName(), equalTo("BuildAction"));
    assertThat(buildAction.getAttributes().getNamedItem("buildConfiguration"), nullValue());
    Node testAction = actions.item(1);
    assertThat(testAction.getNodeName(), equalTo("TestAction"));
    assertThat(testAction.getAttributes().getNamedItem("buildConfiguration").getNodeValue(), equalTo("Debug"));
    Node launchAction = actions.item(2);
    assertThat(launchAction.getNodeName(), equalTo("LaunchAction"));
    assertThat(launchAction.getAttributes().getNamedItem("buildConfiguration").getNodeValue(), equalTo("Debug"));
    Node profileAction = actions.item(3);
    assertThat(profileAction.getNodeName(), equalTo("ProfileAction"));
    assertThat(profileAction.getAttributes().getNamedItem("buildConfiguration").getNodeValue(), equalTo("Release"));
    Node analyzeAction = actions.item(4);
    assertThat(analyzeAction.getNodeName(), equalTo("AnalyzeAction"));
    assertThat(analyzeAction.getAttributes().getNamedItem("buildConfiguration").getNodeValue(), equalTo("Debug"));
    Node archiveAction = actions.item(5);
    assertThat(archiveAction.getNodeName(), equalTo("ArchiveAction"));
    assertThat(archiveAction.getAttributes().getNamedItem("buildConfiguration").getNodeValue(), equalTo("Release"));
}
Also used : PBXTarget(com.facebook.buck.apple.xcode.xcodeproj.PBXTarget) XPath(javax.xml.xpath.XPath) Path(java.nio.file.Path) XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) PBXNativeTarget(com.facebook.buck.apple.xcode.xcodeproj.PBXNativeTarget) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) ImmutableMap(com.google.common.collect.ImmutableMap) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) PBXFileReference(com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference) Test(org.junit.Test)

Example 52 with XPathFactory

use of javax.xml.xpath.XPathFactory in project openhab1-addons by openhab.

the class AutelisBinding method execute.

/**
     * @{inheritDoc}
     */
@Override
protected void execute() {
    logger.trace("Connecting to {}" + baseURL);
    clearState();
    String xmlDoc = fetchStateFromController();
    if (xmlDoc == null) {
        return;
    }
    for (AutelisBindingProvider provider : providers) {
        for (String itemName : provider.getItemNames()) {
            Item item = provider.getItem(itemName);
            String config = provider.getAutelisBindingConfigString(itemName);
            XPathFactory xpathFactory = XPathFactory.newInstance();
            XPath xpath = xpathFactory.newXPath();
            try {
                InputSource is = new InputSource(new StringReader(xmlDoc));
                String value = xpath.evaluate("response/" + config.replace('.', '/'), is);
                State state = toState(item.getClass(), value);
                State oldState = stateMap.put(itemName, state);
                if (!state.equals(oldState)) {
                    logger.debug("updating item {} with state {}", itemName, state);
                    eventPublisher.postUpdate(itemName, state);
                }
            } catch (XPathExpressionException e) {
                logger.warn("could not parse xml", e);
            }
        }
    }
}
Also used : AutelisBindingProvider(org.openhab.binding.autelis.AutelisBindingProvider) XPath(javax.xml.xpath.XPath) SwitchItem(org.openhab.core.library.items.SwitchItem) NumberItem(org.openhab.core.library.items.NumberItem) Item(org.openhab.core.items.Item) XPathFactory(javax.xml.xpath.XPathFactory) InputSource(org.xml.sax.InputSource) State(org.openhab.core.types.State) XPathExpressionException(javax.xml.xpath.XPathExpressionException) StringReader(java.io.StringReader)

Example 53 with XPathFactory

use of javax.xml.xpath.XPathFactory in project camel by apache.

the class XPathTest method testXPathFunctionSubstringUsingSaxon.

@Test
public void testXPathFunctionSubstringUsingSaxon() throws Exception {
    String xml = "<foo><bar>Hello World</bar></foo>";
    XPathFactory fac = new XPathFactoryImpl();
    XPathBuilder builder = XPathBuilder.xpath("substring(/foo/bar, 7)").factory(fac);
    String result = builder.resultType(String.class).evaluate(context, xml, String.class);
    assertEquals("World", result);
    result = builder.evaluate(context, xml);
    assertEquals("World", result);
}
Also used : XPathFactoryImpl(net.sf.saxon.xpath.XPathFactoryImpl) XPathFactory(javax.xml.xpath.XPathFactory) XPathBuilder(org.apache.camel.builder.xml.XPathBuilder) Test(org.junit.Test)

Example 54 with XPathFactory

use of javax.xml.xpath.XPathFactory in project camel by apache.

the class XPathBuilder method createDefaultXPathFactory.

protected static XPathFactory createDefaultXPathFactory() throws XPathFactoryConfigurationException {
    XPathFactory factory = null;
    // read system property and see if there is a factory set
    Properties properties = System.getProperties();
    for (Map.Entry<Object, Object> prop : properties.entrySet()) {
        String key = (String) prop.getKey();
        if (key.startsWith(XPathFactory.DEFAULT_PROPERTY_NAME)) {
            String uri = ObjectHelper.after(key, ":");
            if (uri != null) {
                factory = XPathFactory.newInstance(uri);
                LOG.info("Using system property {} with value {} when created default XPathFactory {}", new Object[] { key, uri, factory });
            }
        }
    }
    if (factory == null) {
        factory = XPathFactory.newInstance();
        LOG.info("Created default XPathFactory {}", factory);
    }
    return factory;
}
Also used : XPathFactory(javax.xml.xpath.XPathFactory) Properties(java.util.Properties) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 55 with XPathFactory

use of javax.xml.xpath.XPathFactory in project sonarqube by SonarSource.

the class XpathParser method parse.

public void parse(String xml) {
    try {
        String fixedXml = fixUnicodeChar(xml);
        doc = builder.parse(new ByteArrayInputStream(fixedXml.getBytes(StandardCharsets.UTF_8)));
        XPathFactory factory = XPathFactory.newInstance();
        xpath = factory.newXPath();
    } catch (IOException | SAXException e) {
        throw new XmlParserException(CAN_NOT_PARSE_XML + xml, e);
    }
}
Also used : XPathFactory(javax.xml.xpath.XPathFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Aggregations

XPathFactory (javax.xml.xpath.XPathFactory)75 XPath (javax.xml.xpath.XPath)59 XPathExpression (javax.xml.xpath.XPathExpression)40 Document (org.w3c.dom.Document)34 NodeList (org.w3c.dom.NodeList)34 XPathExpressionException (javax.xml.xpath.XPathExpressionException)26 DocumentBuilder (javax.xml.parsers.DocumentBuilder)24 Node (org.w3c.dom.Node)24 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)22 InputSource (org.xml.sax.InputSource)16 Test (org.junit.Test)15 IOException (java.io.IOException)13 PBXNativeTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXNativeTarget)11 PBXTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXTarget)11 ImmutableMap (com.google.common.collect.ImmutableMap)11 Path (java.nio.file.Path)11 SAXException (org.xml.sax.SAXException)11 PBXFileReference (com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)9 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)7