Пример #1
0
  @Override
  public void execute_node(
      SesameDataSet dataset,
      String expression,
      TriplesMap parentTriplesMap,
      RMLPerformer performer,
      Object node,
      Resource subject) {
    // still need to make it work with more nore-results
    // currently it handles only one

    if (expression.startsWith("/")) expression = expression.substring(1);

    Node node2 = (Node) node;
    Nodes nodes = node2.query(expression, nsContext);

    for (int i = 0; i < nodes.size(); i++) {
      Node n = nodes.get(i);
      if (subject == null) performer.perform(n, dataset, parentTriplesMap);
      else {
        RMLProcessorFactory factory = new ConcreteRMLProcessorFactory();
        RMLProcessor subprocessor =
            factory.create(map.getLogicalSource().getReferenceFormulation());
        RMLPerformer subperformer = new NodeRMLPerformer(subprocessor);
        subperformer.perform(n, dataset, parentTriplesMap, subject);
      }
    }
  }
Пример #2
0
 private static Element createResults0(Element element) {
   Element newElement = null;
   String tag = element.getLocalName();
   if (ResultsElement.TAG.equals(tag)) {
     newElement = new ResultsElement();
   } else if (ResultElement.TAG.equals(tag)) {
     newElement = new ResultElement();
   } else {
     LOG.error("Unknown element: " + tag);
   }
   XMLUtil.copyAttributes(element, newElement);
   for (int i = 0; i < element.getChildCount(); i++) {
     Node child = element.getChild(i);
     if (child instanceof Text) {
       child = child.copy();
     } else {
       child = ResultsElement.createResults0((Element) child);
     }
     if (newElement != null && child != null) {
       newElement.appendChild(child);
     }
   }
   LOG.trace("XML :" + newElement.toXML());
   return newElement;
 }
Пример #3
0
 public static String getAttributeValue(Nodes attributes) {
   if (attributes.hasAny()) {
     Node attribute = attributes.get(0);
     if (attribute instanceof Attribute) {
       return attribute.getValue();
     }
   }
   return "";
 }
Пример #4
0
  /**
   * Converts a xom node into something readable by Saxon
   *
   * @param node
   * @param config
   * @return
   */
  static NodeInfo wrap(Node node, Configuration config) {
    if (node == null) throw new IllegalArgumentException("node must not be null"); // $NON-NLS-1$
    if (node instanceof DocType)
      throw new IllegalArgumentException("DocType can't be queried by XQuery/XPath"); // $NON-NLS-1$

    Node root = node;
    while (root.getParent() != null) {
      root = root.getParent();
    }

    DocumentWrapper docWrapper = new DocumentWrapper(root, root.getBaseURI(), config);

    return docWrapper.wrap(node);
  }
  @Test
  public void update()
      throws ValidityException, ParsingException, IOException, MojoExecutionException {
    Document pom = new Builder().build(new File(new File("src/it/reflector"), "pom.xml"));
    Artifact artifact =
        new DefaultArtifact("net.stickycode", "sticky-coercion", "jar", "", "[3.1,4)");

    new StickyBoundsMojo().updateDependency(pom, artifact, "[3.6,4)");
    XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0");

    Nodes versions = pom.query("//mvn:version", context);
    assertThat(versions.size()).isEqualTo(3);
    Nodes nodes = pom.query("//mvn:version[text()='[3.6,4)']", context);
    assertThat(nodes.size()).isEqualTo(1);
    Node node = nodes.get(0);
    assertThat(node.getValue()).isEqualTo("[3.6,4)");
  }
Пример #6
0
  public void loadLocationsAsync(String kml) {
    List<LotLocation> locations = new ArrayList<LotLocation>();

    try {
      XMLReader parser = XMLReaderFactory.createXMLReader("org.ccil.cowan.tagsoup.Parser");
      InputStream is = new ByteArrayInputStream(kml.getBytes());

      // build out an XML document using TagSoup
      Document doc = new Builder(parser).build(is);

      // set the ns of the document as the XPathContext or we will not find the elements when we
      // attempt to parse
      XPathContext context = new XPathContext("ns", "http://www.w3.org/1999/xhtml");

      // get the Placemark nodes within the data
      Nodes nodes = doc.query(".//ns:Placemark", context);

      for (int index = 0; index < nodes.size(); index++) {
        LotLocation placemark = new LotLocation();
        Node placemarkNode = nodes.get(index);

        Node nameNode = placemarkNode.query("ns:name", context).get(0);
        if (nameNode != null) placemark.setLocation(nameNode.getValue());

        Node descriptionNode = placemarkNode.query("ns:description", context).get(0);
        if (descriptionNode != null) placemark.setDescription(descriptionNode.getValue());

        Node lnglatNode = placemarkNode.query("ns:Point/ns:coordinates", context).get(0);
        if (lnglatNode != null) {
          // get longitude,latitude,altitude, per KML spec
          String[] points = lnglatNode.getValue().split(",");
          placemark.setPoint(
              new LatLng(
                  Double.parseDouble(points[1].trim()), Double.parseDouble(points[0].trim())));
        }

        locations.add(placemark);
      }

      // spin off a new thread and load locations
      new LoadLocationsTask().execute(locations);

    } catch (Exception e) {
      Log.e("LoadLocationsTask", "Failure attempting to load locations", e);
    }
  }
  @Test
  public void updateTheClassifier()
      throws ValidityException, ParsingException, IOException, MojoExecutionException {
    Document pom =
        new Builder()
            .build(
                new BufferedReader(
                    new InputStreamReader(getClass().getResourceAsStream("classifiers.xml"))));
    Artifact artifact =
        new DefaultArtifact("net.stickycode", "sticky-coercion", "jar", "test-jar", "[2.1,4)");

    new StickyBoundsMojo().updateDependency(pom, artifact, "[2.6,3)");
    XPathContext context = new XPathContext("mvn", "http://maven.apache.org/POM/4.0.0");

    Nodes versions = pom.query("//mvn:version", context);
    assertThat(versions.size()).isEqualTo(4);
    Nodes nodes = pom.query("//mvn:version[text()='[2.6,3)']", context);
    assertThat(nodes.size()).isEqualTo(1);
    Node node = nodes.get(0);
    assertThat(node.getValue()).isEqualTo("[2.6,3)");
  }
 protected static void createSubclassedChildren(
     Element oldElement, AbstractEditorElement newElement) {
   if (oldElement != null) {
     for (int i = 0; i < oldElement.getChildCount(); i++) {
       Node node = oldElement.getChild(i);
       Node newNode = null;
       if (node instanceof Text) {
         String value = node.getValue();
         newNode = new Text(value);
       } else if (node instanceof Comment) {
         newNode = new Comment(node.getValue());
       } else if (node instanceof ProcessingInstruction) {
         newNode = new ProcessingInstruction((ProcessingInstruction) node);
       } else if (node instanceof Element) {
         newNode = createEditorElement((Element) node);
       } else {
         throw new RuntimeException("Cannot create new node: " + node.getClass());
       }
       newElement.appendChild(newNode);
     }
   }
 }
Пример #9
0
  /**
   * Process a XPath expression against an XML node
   *
   * @param node
   * @param expression
   * @return value that matches expression
   */
  private List<String> extractValueFromNode(Node node, String expression) {
    DefaultNamespaceContext dnc = get_namespaces();
    List<String> list = new ArrayList<>();

    if (expression.startsWith("count(")) {
      // Nodes result = node.query(expression, nsContext);
      String result;
      try {
        result = execute(node, expression);
        list.add(result.toString());
      } catch (SaxonApiException ex) {
        Logger.getLogger(XPathProcessor.class.getName()).log(Level.SEVERE, null, ex);
      }
    } else {
      // if there's nothing to uniquelly identify, use # - temporary solution - challenge
      if (expression.equals("#")) {
        list.add(Integer.toString(enumerator++));
        return list;
      }
      Nodes nodes = node.query(expression, nsContext);

      for (int i = 0; i < nodes.size(); i++) {
        Node n = nodes.get(i);

        // checks if the node has a value or children
        if (!n.getValue().isEmpty() || (n.getChildCount() != 0))
          // MVS's for extracting elements and not the string
          /*if (!(n instanceof Attribute) && n.getChild(0) instanceof Element) {
              list.add(n.toXML());
          }
          else {
              list.add(n.getValue());
          }*/

          // checks if the node has children, then cleans up new lines and extra spaces
          if (!(n instanceof Attribute) && n.getChildCount() > 1)
            list.add(
                n.getValue()
                    .trim()
                    .replaceAll("[\\t\\n\\r]", " ")
                    .replaceAll(" +", " ")
                    .replaceAll("\\( ", "\\(")
                    .replaceAll(" \\)", "\\)")
                    .replaceAll(" :", ":")
                    .replaceAll(" ,", ","));
          else list.add(n.getValue().toString());
      }
    }
    return list;
  }