Search in sources :

Example 6 with XPath

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

the class SchemeGeneratorTest method launchActionShouldNotContainRemoteRunnableWhenNotProvided.

@Test
public void launchActionShouldNotContainRemoteRunnableWhenNotProvided() 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 remoteRunnableLaunchActionXPath = xpathFactory.newXPath();
    XPathExpression remoteRunnableLaunchActionExpr = remoteRunnableLaunchActionXPath.compile("//LaunchAction/RemoteRunnable");
    NodeList remoteRunnables = (NodeList) remoteRunnableLaunchActionExpr.evaluate(scheme, XPathConstants.NODESET);
    assertThat(remoteRunnables.getLength(), equalTo(0));
}
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) 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 7 with XPath

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

the class SchemeGeneratorTest method whenProvidedAPrimaryTargetThatIsBuiltWithBuckDoesntBuildDependencies.

@Test
public void whenProvidedAPrimaryTargetThatIsBuiltWithBuckDoesntBuildDependencies() throws Exception {
    ImmutableMap.Builder<PBXTarget, Path> targetToProjectPathMapBuilder = ImmutableMap.builder();
    PBXTarget libraryTarget = new PBXNativeTarget("library");
    libraryTarget.setGlobalID("libraryGID");
    libraryTarget.setProductReference(new PBXFileReference("lib.a", "lib.a", PBXReference.SourceTree.BUILT_PRODUCTS_DIR, Optional.empty()));
    libraryTarget.setProductType(ProductType.STATIC_LIBRARY);
    PBXTarget rootTarget = new PBXNativeTarget("rootRule");
    rootTarget.setGlobalID("rootGID");
    rootTarget.setProductName("Foo");
    String runnablePath = "buck-out/gen/Foo/Foo.app";
    Path pbxprojectPath = Paths.get("foo/Foo.xcodeproj/project.pbxproj");
    targetToProjectPathMapBuilder.put(libraryTarget, pbxprojectPath);
    targetToProjectPathMapBuilder.put(rootTarget, pbxprojectPath);
    SchemeGenerator schemeGenerator = new SchemeGenerator(projectFilesystem, Optional.of(rootTarget), ImmutableSet.of(libraryTarget, rootTarget), ImmutableSet.of(), ImmutableSet.of(), "TestScheme", Paths.get("_gen/Foo.xcworkspace/scshareddata/xcshemes"), true, /* primaryTargetIsBuildWithBuck */
    false, /* parallelizeBuild */
    Optional.of(runnablePath), /* 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 buildActionXpath = xpathFactory.newXPath();
    XPathExpression buildActionExpr = buildActionXpath.compile("//BuildAction//BuildActionEntry");
    NodeList buildActionNodes = (NodeList) buildActionExpr.evaluate(scheme, XPathConstants.NODESET);
    Node libraryNode = null;
    for (int i = 0; i < buildActionNodes.getLength(); i++) {
        Node node = buildActionNodes.item(i);
        if (node.getChildNodes().getLength() != 1) {
            continue;
        }
        Node buildableReference = node.getChildNodes().item(0);
        if (buildableReference.getAttributes().getNamedItem("BlueprintIdentifier").getNodeValue().equals("libraryGID")) {
            libraryNode = node;
            break;
        }
    }
    assertThat(libraryNode, is(notNullValue()));
    assertThat(libraryNode.getAttributes().getNamedItem("buildForRunning").getNodeValue(), equalTo("NO"));
}
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 8 with XPath

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

the class SchemeGeneratorTest method schemeWithNoPrimaryRuleCanIncludeTests.

@Test
public void schemeWithNoPrimaryRuleCanIncludeTests() throws Exception {
    ImmutableMap.Builder<PBXTarget, Path> targetToProjectPathMapBuilder = ImmutableMap.builder();
    PBXTarget testLibraryTarget = new PBXNativeTarget("testLibrary");
    testLibraryTarget.setGlobalID("testLibraryGID");
    testLibraryTarget.setProductReference(new PBXFileReference("lib.a", "lib.a", PBXReference.SourceTree.BUILT_PRODUCTS_DIR, Optional.empty()));
    testLibraryTarget.setProductType(ProductType.STATIC_LIBRARY);
    PBXTarget testTarget = new PBXNativeTarget("testRule");
    testTarget.setGlobalID("testGID");
    testTarget.setProductReference(new PBXFileReference("test.a", "test.a", PBXReference.SourceTree.BUILT_PRODUCTS_DIR, Optional.empty()));
    testTarget.setProductType(ProductType.STATIC_LIBRARY);
    PBXTarget testBundleTarget = new PBXNativeTarget("testBundleRule");
    testBundleTarget.setGlobalID("testBundleGID");
    testBundleTarget.setProductReference(new PBXFileReference("test.xctest", "test.xctest", PBXReference.SourceTree.BUILT_PRODUCTS_DIR, Optional.empty()));
    testBundleTarget.setProductType(ProductType.UNIT_TEST);
    Path pbxprojectPath = Paths.get("foo/Foo.xcodeproj/project.pbxproj");
    targetToProjectPathMapBuilder.put(testLibraryTarget, pbxprojectPath);
    targetToProjectPathMapBuilder.put(testTarget, pbxprojectPath);
    targetToProjectPathMapBuilder.put(testBundleTarget, pbxprojectPath);
    SchemeGenerator schemeGenerator = new SchemeGenerator(projectFilesystem, Optional.empty(), ImmutableSet.of(), ImmutableSet.of(testBundleTarget), ImmutableSet.of(testBundleTarget), "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();
    String schemeXml = projectFilesystem.readFileIfItExists(schemePath).get();
    System.out.println(schemeXml);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document scheme = dBuilder.parse(projectFilesystem.newFileInputStream(schemePath));
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath buildActionXpath = xpathFactory.newXPath();
    XPathExpression buildActionExpr = buildActionXpath.compile("//BuildAction//BuildableReference/@BlueprintIdentifier");
    NodeList buildActionNodes = (NodeList) buildActionExpr.evaluate(scheme, XPathConstants.NODESET);
    List<String> expectedOrdering = ImmutableList.of("testBundleGID");
    List<String> actualOrdering = Lists.newArrayList();
    for (int i = 0; i < buildActionNodes.getLength(); i++) {
        actualOrdering.add(buildActionNodes.item(i).getNodeValue());
    }
    assertThat(actualOrdering, equalTo(expectedOrdering));
    XPath testActionXpath = xpathFactory.newXPath();
    XPathExpression testActionExpr = testActionXpath.compile("//TestAction//BuildableReference/@BlueprintIdentifier");
    String testActionBlueprintIdentifier = (String) testActionExpr.evaluate(scheme, XPathConstants.STRING);
    assertThat(testActionBlueprintIdentifier, equalTo("testBundleGID"));
    XPath launchActionXpath = xpathFactory.newXPath();
    XPathExpression launchActionExpr = launchActionXpath.compile("//LaunchAction//BuildableReference/@BlueprintIdentifier");
    String launchActionBlueprintIdentifier = (String) launchActionExpr.evaluate(scheme, XPathConstants.STRING);
    assertThat(launchActionBlueprintIdentifier, equalTo(""));
    XPath launchActionBuildConfigurationXpath = xpathFactory.newXPath();
    XPathExpression launchActionBuildConfigurationExpr = launchActionBuildConfigurationXpath.compile("//LaunchAction//@buildConfiguration");
    String launchActionBuildConfigurationBlueprintIdentifier = (String) launchActionBuildConfigurationExpr.evaluate(scheme, XPathConstants.STRING);
    assertThat(launchActionBuildConfigurationBlueprintIdentifier, equalTo("Debug"));
    XPath profileActionXpath = xpathFactory.newXPath();
    XPathExpression profileActionExpr = profileActionXpath.compile("//ProfileAction//BuildableReference/@BlueprintIdentifier");
    String profileActionBlueprintIdentifier = (String) profileActionExpr.evaluate(scheme, XPathConstants.STRING);
    assertThat(profileActionBlueprintIdentifier, equalTo(""));
    XPath profileActionBuildConfigurationXpath = xpathFactory.newXPath();
    XPathExpression profileActionBuildConfigurationExpr = profileActionBuildConfigurationXpath.compile("//ProfileAction//@buildConfiguration");
    String profileActionBuildConfigurationBlueprintIdentifier = (String) profileActionBuildConfigurationExpr.evaluate(scheme, XPathConstants.STRING);
    assertThat(profileActionBuildConfigurationBlueprintIdentifier, equalTo("Release"));
    XPath analyzeActionBuildConfigurationXpath = xpathFactory.newXPath();
    XPathExpression analyzeActionBuildConfigurationExpr = analyzeActionBuildConfigurationXpath.compile("//AnalyzeAction//@buildConfiguration");
    String analyzeActionBuildConfigurationBlueprintIdentifier = (String) analyzeActionBuildConfigurationExpr.evaluate(scheme, XPathConstants.STRING);
    assertThat(analyzeActionBuildConfigurationBlueprintIdentifier, equalTo("Debug"));
    XPath archiveActionBuildConfigurationXpath = xpathFactory.newXPath();
    XPathExpression archiveActionBuildConfigurationExpr = archiveActionBuildConfigurationXpath.compile("//ArchiveAction//@buildConfiguration");
    String archiveActionBuildConfigurationBlueprintIdentifier = (String) archiveActionBuildConfigurationExpr.evaluate(scheme, XPathConstants.STRING);
    assertThat(archiveActionBuildConfigurationBlueprintIdentifier, 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) 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 9 with XPath

use of javax.xml.xpath.XPath in project qi4j-sdk by Qi4j.

the class RssReaderTest method testReadRssFeed.

@Test
public void testReadRssFeed() {
    Client client = new Client(Protocol.HTTPS);
    Reference ref = new Reference("https://github.com/Qi4j/qi4j-sdk/commits/develop.atom");
    ContextResourceClientFactory contextResourceClientFactory = module.newObject(ContextResourceClientFactory.class, client);
    contextResourceClientFactory.registerResponseReader(new ResponseReader() {

        @Override
        public Object readResponse(Response response, Class<?> resultType) throws ResourceException {
            if (resultType.equals(Document.class)) {
                try {
                    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                    documentBuilderFactory.setNamespaceAware(false);
                    return documentBuilderFactory.newDocumentBuilder().parse(response.getEntity().getStream());
                } catch (Exception e) {
                    throw new ResourceException(e);
                }
            }
            return null;
        }
    });
    contextResourceClientFactory.setErrorHandler(new ErrorHandler().onError(ErrorHandler.RECOVERABLE_ERROR, new ResponseHandler() {

        @Override
        public HandlerCommand handleResponse(Response response, ContextResourceClient client) {
            System.out.println(">> REFRESH on recoverable error: " + response.getStatus());
            return refresh();
        }
    }));
    crc = contextResourceClientFactory.newClient(ref);
    crc.onResource(new ResultHandler<Document>() {

        Iterator<Node> itemNodes;

        @Override
        public HandlerCommand handleResult(Document result, ContextResourceClient client) {
            try {
                final XPath xPath = XPathFactory.newInstance().newXPath();
                System.out.println("== " + xPath.evaluate("feed/title", result) + " ==");
                final NodeList nodes = (NodeList) xPath.evaluate("feed/entry", result, XPathConstants.NODESET);
                List<Node> items = new ArrayList<>();
                for (int i = 0; i < nodes.getLength(); i++) {
                    items.add(nodes.item(i));
                }
                itemNodes = items.iterator();
                return processEntry(xPath);
            } catch (XPathExpressionException e) {
                throw new ResourceException(e);
            }
        }

        private HandlerCommand processEntry(final XPath xPath) throws XPathExpressionException {
            if (!itemNodes.hasNext()) {
                return null;
            }
            Node item = itemNodes.next();
            String title = xPath.evaluate("title", item);
            String detailUrl = xPath.evaluate("link/@href", item);
            System.out.println("-- " + title + " --");
            System.out.println("-- " + detailUrl + " --");
            return processEntry(xPath);
        }
    });
    crc.start();
}
Also used : ContextResourceClientFactory(org.qi4j.library.rest.client.api.ContextResourceClientFactory) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ResponseHandler(org.qi4j.library.rest.client.spi.ResponseHandler) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) HandlerCommand(org.qi4j.library.rest.client.api.HandlerCommand) ResourceException(org.restlet.resource.ResourceException) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) ContextResourceClient(org.qi4j.library.rest.client.api.ContextResourceClient) Client(org.restlet.Client) ContextResourceClient(org.qi4j.library.rest.client.api.ContextResourceClient) XPath(javax.xml.xpath.XPath) ErrorHandler(org.qi4j.library.rest.client.api.ErrorHandler) Reference(org.restlet.data.Reference) NodeList(org.w3c.dom.NodeList) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ResourceException(org.restlet.resource.ResourceException) AssemblyException(org.qi4j.bootstrap.AssemblyException) Response(org.restlet.Response) ResponseReader(org.qi4j.library.rest.client.spi.ResponseReader) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 10 with XPath

use of javax.xml.xpath.XPath in project jersey by jersey.

the class ExtendedWadlWebappOsgiTest method checkWadl.

private void checkWadl(String wadl, URI baseUri) throws Exception {
    DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
    bf.setNamespaceAware(true);
    bf.setValidating(false);
    DocumentBuilder b = bf.newDocumentBuilder();
    Document document = b.parse(new ByteArrayInputStream(wadl.getBytes(Charset.forName("UTF-8"))));
    XPath xp = XPathFactory.newInstance().newXPath();
    xp.setNamespaceContext(new SimpleNamespaceResolver("wadl", "http://wadl.dev.java.net/2009/02"));
    String val = (String) xp.evaluate("/wadl:application/wadl:resources/@base", document, XPathConstants.STRING);
    assertEquals(baseUri.toString(), val.endsWith("/") ? val.substring(0, val.length() - 1) : val);
    val = (String) xp.evaluate("count(//wadl:resource)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements.", val, "4");
    val = (String) xp.evaluate("count(//wadl:resource[@path='items'])", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements with 'items' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='{id}'])", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements with '{id}' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='try-hard'])", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements with 'try-hard' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='value/{value}'])", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements with 'value/{value}' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='{id}']/wadl:method)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of methods in resource element with '{id}' path.", "2", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='{id}']/wadl:method[@id='getItem']" + "/wadl:doc[contains(., 'Typically returns the item if it exists.')])", document, XPathConstants.STRING);
    assertEquals("Unexpected documentation of getItem resource method at '{id}' path", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='try-hard']/wadl:method)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of methods in resource element with 'try-hard' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='try-hard']/wadl:method[@id='getItem']" + "/wadl:doc[contains(., 'Tries hard to return the item if it exists.')])", document, XPathConstants.STRING);
    assertEquals("Unexpected documentation of getItem resource method at 'try-hard' path", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='items']/wadl:method)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of methods in resource element with 'items' path.", "4", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='value/{value}']/wadl:method)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of methods in resource element with 'value/{value}' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:application/wadl:doc)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of doc elements in application element.", "3", val);
}
Also used : XPath(javax.xml.xpath.XPath) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleNamespaceResolver(org.glassfish.jersey.internal.util.SimpleNamespaceResolver) Document(org.w3c.dom.Document)

Aggregations

XPath (javax.xml.xpath.XPath)197 Document (org.w3c.dom.Document)107 NodeList (org.w3c.dom.NodeList)99 XPathExpressionException (javax.xml.xpath.XPathExpressionException)70 Node (org.w3c.dom.Node)70 XPathExpression (javax.xml.xpath.XPathExpression)69 XPathFactory (javax.xml.xpath.XPathFactory)59 DocumentBuilder (javax.xml.parsers.DocumentBuilder)45 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)37 IOException (java.io.IOException)32 Element (org.w3c.dom.Element)32 Test (org.junit.Test)25 InputSource (org.xml.sax.InputSource)23 SAXException (org.xml.sax.SAXException)21 File (java.io.File)19 ArrayList (java.util.ArrayList)19 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)18 HashMap (java.util.HashMap)16 InputStream (java.io.InputStream)12 PBXNativeTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXNativeTarget)11