Exemplo n.º 1
0
  @Test
  public void testChangeAltWithOthers() {
    // note, they all have same prefix.
    Namespace nsa = Namespace.getNamespace("pfxa", "nsurla");
    Namespace nsb = Namespace.getNamespace("pfxa", "nsurlb");
    Namespace nsc = Namespace.getNamespace("pfxa", "nsurlc");
    Namespace alta = Namespace.getNamespace("alta", "nsalturla");
    Namespace altb = Namespace.getNamespace("altb", "nsalturlb");

    Element root = new Element("root");
    Element child = new Element("child");
    Element leaf = new Element("leaf");

    root.addNamespaceDeclaration(nsa);
    root.addNamespaceDeclaration(alta);
    root.addNamespaceDeclaration(altb);

    child.addNamespaceDeclaration(nsb);
    child.addNamespaceDeclaration(alta);
    child.addNamespaceDeclaration(altb);

    // On the leaf we try the varaint of adding the namespace
    // via the attributes.
    leaf.setAttribute("att", "val", nsc);
    leaf.addNamespaceDeclaration(alta);
    leaf.addNamespaceDeclaration(altb);

    root.addContent(child);
    child.addContent(leaf);
    exercise(root, new NamespaceStack());
  }
Exemplo n.º 2
0
  @Test
  public void testChangeMainWithOthers() {
    Namespace nsa = Namespace.getNamespace("pfxa", "nsurla");
    Namespace nsb = Namespace.getNamespace("pfxb", "nsurlb");
    Namespace nsc = Namespace.getNamespace("pfxc", "nsurlc");
    Namespace nsd = Namespace.getNamespace("pfxd", "nsurld");

    Element root = new Element("root", Namespace.getNamespace("rooturl"));
    // child is in a different namespace but with same "" prefix.
    Element child = new Element("child", Namespace.getNamespace("childurl"));
    // leaf is in the NO_NAMESPACE namespace (also prefix "");
    Element leaf = new Element("leaf");

    root.addNamespaceDeclaration(nsa);
    root.addNamespaceDeclaration(nsb);
    root.addNamespaceDeclaration(nsc);
    root.addNamespaceDeclaration(nsd);

    child.addNamespaceDeclaration(nsa);
    child.addNamespaceDeclaration(nsb);
    child.addNamespaceDeclaration(nsc);
    child.addNamespaceDeclaration(nsd);

    // On the leaf we try the varaint of adding the namespace
    // via the attributes.
    leaf.setAttribute("att", "val", nsa);
    leaf.setAttribute("att", "val", nsb);
    leaf.setAttribute("att", "val", nsc);
    leaf.setAttribute("att", "val", nsd);

    root.addContent(child);
    child.addContent(leaf);
    exercise(root, new NamespaceStack());
  }
Exemplo n.º 3
0
  public String formatAsXHTML(String xhtml) throws IOException, JDOMException {
    DocType doctype =
        new DocType(
            "html", "-//W3C//DTD XHTML 1.1//EN", "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd");
    Namespace namespace = Namespace.getNamespace("http://www.w3.org/1999/xhtml");

    org.jdom2.Document document = XHTMLUtils.parseXHTMLDocument(xhtml);

    org.jdom2.Element root = document.getRootElement();
    root.setNamespace(namespace);
    root.addNamespaceDeclaration(namespace);
    IteratorIterable<org.jdom2.Element> elements = root.getDescendants(Filters.element());
    for (org.jdom2.Element element : elements) {
      if (element.getNamespace() == null) {
        element.setNamespace(Constants.NAMESPACE_XHTML);
      }
    }
    document.setDocType(doctype);

    XMLOutputter outputter = new XMLOutputter();
    Format xmlFormat = Format.getPrettyFormat();
    outputter.setFormat(xmlFormat);
    outputter.setXMLOutputProcessor(new XHTMLOutputProcessor());
    String result = outputter.outputString(document);
    return result;
  }
Exemplo n.º 4
0
 @Test
 public void testOutputElementNamespaces() {
   String txt =
       "<ns:root xmlns:ns=\"myns\" xmlns:ans=\"attributens\" xmlns:two=\"two\" ans:att=\"val\"/>";
   Element emt = new Element("root", Namespace.getNamespace("ns", "myns"));
   Namespace ans = Namespace.getNamespace("ans", "attributens");
   emt.setAttribute(new Attribute("att", "val", ans));
   emt.addNamespaceDeclaration(Namespace.getNamespace("two", "two"));
   checkOutput(emt, txt, txt, txt, txt, txt);
 }
 protected Element createRootElement(Feed feed) {
   Element root = new Element("feed", getFeedNamespace());
   root.addNamespaceDeclaration(getFeedNamespace());
   // Attribute version = new Attribute("version", getVersion());
   // root.setAttribute(version);
   if (feed.getXmlBase() != null) {
     root.setAttribute("base", feed.getXmlBase(), Namespace.XML_NAMESPACE);
   }
   generateModuleNamespaceDefs(root);
   return root;
 }
Exemplo n.º 6
0
  @Test
  public void testAdditionalSpecialCases() {
    // note, they all have same prefix.
    Namespace nsa = Namespace.getNamespace("pfxa", "nsurla");

    // Note how they have the same namespaces, just swap between
    // Element and Attribute.
    Element root = new Element("root", nsa);
    // additional has same Namespace as element
    root.addNamespaceDeclaration(nsa);

    exercise(root, new NamespaceStack());
  }
Exemplo n.º 7
0
 @Test
 public void testSeededConstructor() {
   Namespace x = Namespace.getNamespace("X");
   Namespace y = Namespace.getNamespace("y", "Y");
   Namespace[] nsa = new Namespace[] {x, Namespace.XML_NAMESPACE};
   NamespaceStack nstack = new NamespaceStack(nsa);
   checkIterable(nstack, nsa);
   checkIterable(nstack.addedForward(), nsa);
   Element emt = new Element("root", y);
   emt.addNamespaceDeclaration(Namespace.NO_NAMESPACE);
   nstack.push(emt);
   checkIterable(nstack, y, Namespace.NO_NAMESPACE, Namespace.XML_NAMESPACE);
   checkIterable(nstack.addedForward(), y, Namespace.NO_NAMESPACE);
 }
Exemplo n.º 8
0
  /*
   * (non-Javadoc)
   * @see com.rometools.rome.io.ModuleGenerator#generate(com.rometools.rome.feed.module.Module,
   * org.jdom2.Element)
   */
  @Override
  public void generate(final Module module, final Element element) {
    // this is not necessary, it is done to avoid the namespace definition
    // in every item.
    Element root = element;
    while (root.getParent() != null && root.getParent() instanceof Element) {
      root = (Element) element.getParent();
    }
    root.addNamespaceDeclaration(GeoRSSModule.SIMPLE_NS);
    root.addNamespaceDeclaration(GeoRSSModule.GML_NS);

    final Element whereElement = new Element("where", GeoRSSModule.SIMPLE_NS);
    element.addContent(whereElement);

    final GeoRSSModule geoRSSModule = (GeoRSSModule) module;
    final AbstractGeometry geometry = geoRSSModule.getGeometry();

    if (geometry instanceof Point) {
      final Position pos = ((Point) geometry).getPosition();

      final Element pointElement = new Element("Point", GeoRSSModule.GML_NS);
      whereElement.addContent(pointElement);

      final Element posElement = new Element("pos", GeoRSSModule.GML_NS);
      posElement.addContent(
          String.valueOf(pos.getLatitude()) + " " + String.valueOf(pos.getLongitude()));
      pointElement.addContent(posElement);
    } else if (geometry instanceof LineString) {
      final PositionList posList = ((LineString) geometry).getPositionList();

      final Element lineElement = new Element("LineString", GeoRSSModule.GML_NS);
      lineElement.addContent(createPosListElement(posList));
      whereElement.addContent(lineElement);
    } else if (geometry instanceof Polygon) {
      final Element polygonElement = new Element("Polygon", GeoRSSModule.GML_NS);
      {
        final AbstractRing ring = ((Polygon) geometry).getExterior();
        if (ring instanceof LinearRing) {
          final Element exteriorElement = new Element("exterior", GeoRSSModule.GML_NS);
          polygonElement.addContent(exteriorElement);
          final Element ringElement = new Element("LinearRing", GeoRSSModule.GML_NS);
          exteriorElement.addContent(ringElement);
          ringElement.addContent(createPosListElement(((LinearRing) ring).getPositionList()));

        } else {
          System.err.println(
              "GeoRSS GML format can't handle rings of type: " + ring.getClass().getName());
        }
      }
      final List<AbstractRing> interiorList = ((Polygon) geometry).getInterior();
      final Iterator<AbstractRing> it = interiorList.iterator();
      while (it.hasNext()) {
        final AbstractRing ring = it.next();
        if (ring instanceof LinearRing) {
          final Element interiorElement = new Element("interior", GeoRSSModule.GML_NS);
          polygonElement.addContent(interiorElement);
          final Element ringElement = new Element("LinearRing", GeoRSSModule.GML_NS);
          interiorElement.addContent(ringElement);
          ringElement.addContent(createPosListElement(((LinearRing) ring).getPositionList()));

        } else {
          System.err.println(
              "GeoRSS GML format can't handle rings of type: " + ring.getClass().getName());
        }
      }
      whereElement.addContent(polygonElement);
    } else if (geometry instanceof Envelope) {
      final Envelope envelope = (Envelope) geometry;
      final Element envelopeElement = new Element("Envelope", GeoRSSModule.GML_NS);
      whereElement.addContent(envelopeElement);

      final Element lowerElement = new Element("lowerCorner", GeoRSSModule.GML_NS);
      lowerElement.addContent(
          String.valueOf(envelope.getMinLatitude())
              + " "
              + String.valueOf(envelope.getMinLongitude()));
      envelopeElement.addContent(lowerElement);

      final Element upperElement = new Element("upperCorner", GeoRSSModule.GML_NS);
      upperElement.addContent(
          String.valueOf(envelope.getMaxLatitude())
              + " "
              + String.valueOf(envelope.getMaxLongitude()));
      envelopeElement.addContent(upperElement);

    } else {
      System.err.println(
          "GeoRSS GML format can't handle geometries of type: " + geometry.getClass().getName());
    }
  }