Example usage for org.jdom2 Namespace getPrefix

List of usage examples for org.jdom2 Namespace getPrefix

Introduction

In this page you can find the example usage for org.jdom2 Namespace getPrefix.

Prototype

public String getPrefix() 

Source Link

Document

This returns the prefix mapped to this Namespace.

Usage

From source file:AIR.Common.xml.XmlNamespaceManager.java

License:Open Source License

public void addNamespace(String prefix, String url) {
    Namespace newNs = Namespace.getNamespace(prefix, url);
    for (int counter1 = 0; counter1 < nsList.size(); ++counter1) {
        Namespace existing = nsList.get(counter1);
        if (StringUtils.equals(existing.getPrefix(), newNs.getPrefix())) {
            nsList.remove(counter1);/*from   w ww . j  a va 2s.  c  o  m*/
            nsList.add(counter1, newNs);
            return;
        }
    }

    nsList.add(newNs);
}

From source file:ca.nrc.cadc.caom2.xml.ObservationReader.java

License:Open Source License

/**
 * Construct an Observation from a Reader.
 *
 * @param reader// w  w  w  .j  a v  a 2 s.co  m
 *            A Reader.
 * @return An Observation.
 * @throws ObservationParsingException
 *             if there is an error parsing the XML.
 */
public Observation read(Reader reader) throws ObservationParsingException, IOException {
    if (reader == null) {
        throw new IllegalArgumentException("reader must not be null");
    }

    init();

    Document document;
    try {
        document = XmlUtil.buildDocument(reader, schemaMap);
    } catch (JDOMException jde) {
        String error = "XML failed schema validation: " + jde.getMessage();
        throw new ObservationParsingException(error, jde);
    }

    // Root element and namespace of the Document
    Element root = document.getRootElement();
    Namespace namespace = root.getNamespace();
    log.debug("obs namespace uri: " + namespace.getURI());
    log.debug("obs namespace prefix: " + namespace.getPrefix());

    ReadContext rc = new ReadContext();
    if (XmlConstants.CAOM2_0_NAMESPACE.equals(namespace.getURI())) {
        rc.docVersion = 20;
    } else if (XmlConstants.CAOM2_1_NAMESPACE.equals(namespace.getURI())) {
        rc.docVersion = 21;
    } else if (XmlConstants.CAOM2_2_NAMESPACE.equals(namespace.getURI())) {
        rc.docVersion = 22;
    }

    // Simple or Composite
    Attribute type = root.getAttribute("type", xsiNamespace);
    String tval = type.getValue();

    String collection = getChildText("collection", root, namespace, false);
    String observationID = getChildText("observationID", root, namespace, false);

    // Algorithm.
    Algorithm algorithm = getAlgorithm(root, namespace, rc);

    // Create the Observation.
    Observation obs;
    String simple = namespace.getPrefix() + ":" + SimpleObservation.class.getSimpleName();
    String comp = namespace.getPrefix() + ":" + CompositeObservation.class.getSimpleName();
    if (simple.equals(tval)) {
        obs = new SimpleObservation(collection, observationID);
        obs.setAlgorithm(algorithm);
    } else if (comp.equals(tval)) {
        obs = new CompositeObservation(collection, observationID, algorithm);
    } else {
        throw new ObservationParsingException("unexpected observation type: " + tval);
    }

    // Observation children.
    String intent = getChildText("intent", root, namespace, false);
    if (intent != null) {
        obs.intent = ObservationIntentType.toValue(intent);
    }
    obs.type = getChildText("type", root, namespace, false);

    obs.metaRelease = getChildTextAsDate("metaRelease", root, namespace, false, rc.dateFormat);
    obs.sequenceNumber = getChildTextAsInteger("sequenceNumber", root, namespace, false);
    obs.proposal = getProposal(root, namespace, rc);
    obs.target = getTarget(root, namespace, rc);
    obs.targetPosition = getTargetPosition(root, namespace, rc);
    obs.requirements = getRequirements(root, namespace, rc);
    obs.telescope = getTelescope(root, namespace, rc);
    obs.instrument = getInstrument(root, namespace, rc);
    obs.environment = getEnvironment(root, namespace, rc);

    addPlanes(obs.getPlanes(), root, namespace, rc);

    if (obs instanceof CompositeObservation) {
        addMembers(((CompositeObservation) obs).getMembers(), root, namespace, rc);
    }

    assignEntityAttributes(root, obs, rc);

    return obs;
}

From source file:ca.nrc.cadc.caom2.xml.ObservationReader.java

License:Open Source License

protected Position getPosition(Element parent, Namespace namespace, ReadContext rc)
        throws ObservationParsingException {
    Element element = getChildElement("position", parent, namespace, false);
    if (element == null) {
        return null;
    }//w  ww. j a va  2 s  .c  o  m

    Position pos = new Position();
    Element cur = getChildElement("bounds", element, namespace, false);
    if (cur != null) {
        if (rc.docVersion < 23) {
            throw new UnsupportedOperationException(
                    "cannot convert version " + rc.docVersion + " polygon to current version");
        }
        Attribute type = cur.getAttribute("type", xsiNamespace);
        String tval = type.getValue();
        String circleType = namespace.getPrefix() + ":" + Circle.class.getSimpleName();
        String polyType = namespace.getPrefix() + ":" + Polygon.class.getSimpleName();
        if (polyType.equals(tval)) {
            List<Point> points = new ArrayList<Point>();
            Element pes = cur.getChild("points", namespace);
            for (Element pe : pes.getChildren()) { // only vertex
                double cval1 = getChildTextAsDouble("cval1", pe, namespace, true);
                double cval2 = getChildTextAsDouble("cval2", pe, namespace, true);
                points.add(new Point(cval1, cval2));
            }
            Element se = cur.getChild("samples", namespace);
            MultiPolygon poly = new MultiPolygon();
            Element ves = se.getChild("vertices", namespace);
            for (Element ve : ves.getChildren()) { // only vertex
                double cval1 = getChildTextAsDouble("cval1", ve, namespace, true);
                double cval2 = getChildTextAsDouble("cval2", ve, namespace, true);
                int sv = getChildTextAsInteger("type", ve, namespace, true);
                poly.getVertices().add(new Vertex(cval1, cval2, SegmentType.toValue(sv)));
            }
            pos.bounds = new Polygon(points, poly);
        } else if (circleType.equals(tval)) {
            Element ce = cur.getChild("center", namespace);
            double cval1 = getChildTextAsDouble("cval1", ce, namespace, true);
            double cval2 = getChildTextAsDouble("cval2", ce, namespace, true);
            Point c = new Point(cval1, cval2);
            double r = getChildTextAsDouble("radius", cur, namespace, true);
            pos.bounds = new Circle(c, r);
        } else {
            throw new UnsupportedOperationException("unsupported shape: " + tval);
        }
    }

    cur = getChildElement("dimension", element, namespace, false);
    if (cur != null) {
        // Attribute type = cur.getAttribute("type", xsiNamespace);
        // String tval = type.getValue();
        // String extype = namespace.getPrefix() + ":" +
        // Dimension2D.class.getSimpleName();
        // if ( extype.equals(tval) )
        // {
        long naxis1 = getChildTextAsLong("naxis1", cur, namespace, true);
        long naxis2 = getChildTextAsLong("naxis2", cur, namespace, true);
        pos.dimension = new Dimension2D(naxis1, naxis2);
        // }
        // else
        // throw new ObservationParsingException("unsupported dimension
        // type: " + tval);
    }

    pos.resolution = getChildTextAsDouble("resolution", element, namespace, false);
    pos.sampleSize = getChildTextAsDouble("sampleSize", element, namespace, false);
    pos.timeDependent = getChildTextAsBoolean("timeDependent", element, namespace, false);

    return pos;
}

From source file:ca.nrc.cadc.vos.NodeReader.java

License:Open Source License

/**
 *  Construct a Node from a Reader.//from   w w  w  .j a v a 2  s. co  m
 *
 * @param reader Reader.
 * @return Node Node.
 * @throws NodeParsingException if there is an error parsing the XML.
 */
public Node read(Reader reader) throws NodeParsingException, IOException {
    if (reader == null)
        throw new IllegalArgumentException("reader must not be null");

    // Create a JDOM Document from the XML
    Document document;
    try {
        // TODO: investigate creating a SAXBuilder once and re-using it
        // as long as we can detect concurrent access (a la java collections)
        document = XmlUtil.validateXml(reader, schemaMap);
    } catch (JDOMException jde) {
        String error = "XML failed schema validation: " + jde.getMessage();
        throw new NodeParsingException(error, jde);
    }

    // Root element and namespace of the Document
    Element root = document.getRootElement();
    Namespace namespace = root.getNamespace();
    log.debug("node namespace uri: " + namespace.getURI());
    log.debug("node namespace prefix: " + namespace.getPrefix());

    /* Node base elements */
    // uri attribute of the node element
    String uri = root.getAttributeValue("uri");
    if (uri == null) {
        String error = "uri attribute not found in root element";
        throw new NodeParsingException(error);
    }
    log.debug("node uri: " + uri);

    // Get the xsi:type attribute which defines the Node class
    String xsiType = root.getAttributeValue("type", xsiNamespace);
    if (xsiType == null) {
        String error = "xsi:type attribute not found in node element " + uri;
        throw new NodeParsingException(error);
    }

    // Split the type attribute into namespace and Node type
    String[] types = xsiType.split(":");
    String type = types[1];
    log.debug("node type: " + type);

    try {
        if (type.equals(ContainerNode.class.getSimpleName()))
            return buildContainerNode(root, namespace, uri);
        else if (type.equals(DataNode.class.getSimpleName()))
            return buildDataNode(root, namespace, uri);
        else if (type.equals(UnstructuredDataNode.class.getSimpleName()))
            return buildUnstructuredDataNode(root, namespace, uri);
        else if (type.equals(LinkNode.class.getSimpleName()))
            return buildLinkNode(root, namespace, uri);
        else if (type.equals(StructuredDataNode.class.getSimpleName()))
            return buildStructuredDataNode(root, namespace, uri);
        else
            throw new NodeParsingException("unsupported node type " + type);
    } catch (URISyntaxException e) {
        throw new NodeParsingException("invalid uri in xml: " + e.getMessage());
    }
}

From source file:ca.nrc.cadc.vosi.Capability.java

License:Open Source License

public Element toXmlElement(Namespace xsi, Namespace cap, Namespace vor) {
    Element eleCapability = new Element("capability");
    eleCapability.setAttribute("standardID", _standardID);

    Element eleInterface = new Element("interface");
    eleCapability.addContent(eleInterface);

    Attribute attType = new Attribute("type", vor.getPrefix() + ":ParamHTTP", xsi);
    eleInterface.setAttribute(attType);//w  w w  . ja  v a 2s.  c  o  m
    if (_role != null)
        eleInterface.setAttribute("role", _role);

    Element eleAccessURL = new Element("accessURL");
    eleInterface.addContent(eleAccessURL);

    eleAccessURL.setAttribute("use", "full");
    eleAccessURL.setText(_hostContext + _resourceName);

    return eleCapability;
}

From source file:ca.nrc.cadc.xml.JsonOutputter.java

License:Open Source License

private boolean writeSchemaAttributes(Element e, PrintWriter w, int i) throws IOException {
    boolean ret = false;

    // getNamespacesIntroduced: only write for newly introduced namespaces;
    // this correctly handles the current context of namespaces from root to 
    // current element
    for (Namespace ans : e.getNamespacesIntroduced()) {
        String uri = ans.getURI();
        String pre = ans.getPrefix();
        if (ret) {
            w.print(",");
        }/*from   ww w. j  a  va  2  s  .  c om*/
        ret = true;
        indent(w, i);
        writeSchema(uri, pre, w);
    }
    return ret;
}

From source file:com.c4om.utils.xmlutils.JDOMUtils.java

License:Apache License

/**
 * Given a {@link Collection} of {@link Namespace} objects, it returns the first one (as returned by its iterator) 
 * whose prefix is equal to a given one 
 * @param prefix the prefix whose namespace we are looking for. It may be null, to look for the null namespace.
 * @param namespaces the collection of namespace
 * @return the corresponding {@link Namespace} object if found, the null namespace (i.e. {@link Namespace#NO_NAMESPACE}) if prefix is null or "" (regardless of it belongs to the collection or not), null otherwise.
 *///from w ww .j a  v  a  2 s  .  c o m
public static Namespace solveNamespaceForPrefix(String prefix, Collection<Namespace> namespaces) {
    Namespace namespace = null;
    if (prefix == null) {
        namespace = Namespace.NO_NAMESPACE;
        return namespace;
    } else {
        for (Namespace knownNS : namespaces) {
            if (knownNS.getPrefix().equals(prefix)) {
                namespace = knownNS;
                break;
            }
        }
    }
    if (prefix.equals("") && namespace == null) {
        namespace = Namespace.NO_NAMESPACE;
    }
    return namespace;
}

From source file:com.codeexcursion.PathCommon.java

public void setup() {
    testFile = new File(testFilePath);
    if (!testFile.isFile()) {
        fail("Unable to open file = " + testFilePath);
    }//  w  w  w. ja v  a2s . co m
    document = getDocument(testFile);

    rss = document.getRootElement();

    Namespace wordpressNamespace = rss.getNamespace("wp");
    Assert.assertNotNull("Wordpress namespace is null.", wordpressNamespace);
    Assert.assertEquals("Wordpress namespace is null.", "wp", wordpressNamespace.getPrefix());

    Namespace contentNamespace = rss.getNamespace("content");
    Assert.assertNotNull("Content namespace is null.", contentNamespace);
    Assert.assertEquals("Content namespace is null.", "content", contentNamespace.getPrefix());

    List<Element> channels = rss.getChildren(ConversionManager.CHANNEL);
    Assert.assertNotNull("List of channels was null", channels);

    Element channel = channels.get(0);
    Assert.assertNotNull("Channel was null", channel);

    List<Element> listOfItems = channel.getChildren(ConversionManager.ITEM);

    Optional<Element> firstItem = listOfItems.stream().findFirst();
    Assert.assertTrue("Item element is null!", firstItem.isPresent());
    attachmentItem = new Item(firstItem.get(), wordpressNamespace, contentNamespace);
    /*
        System.out.println("xxxxxxxxxxxxxxxxx");
    //    System.out.println(firstItem.get().getChildText(ItemElementChildTagNames.POST_TYPE, Namespace.getNamespace(ItemElementChildTagNames.WORDPRESS_NAMESPACE)));
        System.out.println(firstItem.get().getName());
        System.out.println(firstItem.get().getChildText("post_id", wordpressNamespace));
    //    firstItem.get().getChildren().stream().forEach(child -> System.out.println(child.getName()));
        //System.out.println(firstItem.get().getChild("post_id", wordpressNamespace));
        System.out.println("xxxxxxxxxxxxxxxxx");
      */
    Optional<Element> postElementItem = listOfItems.stream().filter(
            item -> "246".equals(item.getChildText(ItemElementChildTagNames.DOCUMENT_ID, wordpressNamespace)))
            .findFirst();

    Assert.assertTrue("Post element item element is null!", postElementItem.isPresent());
    postItem = new Item(postElementItem.get(), wordpressNamespace, contentNamespace);
}

From source file:com.cybernostics.jsp2thymeleaf.api.elements.CopyElementConverter.java

protected void addAttributes(Element parent, JspElementContext node, JSPElementNodeConverter context) {
    final List<Attribute> attributes = convertAttributes(node, context);
    List<Attribute> atts = attributes.stream().map(a -> {
        Namespace ns = a.getNamespace();
        if (ns.equals(XMLNS) || ns.getPrefix().equals("")) {
            if (a.getValue().startsWith("${") || a.getValue().startsWith("@{")) {
                a.setNamespace(TH);//from www  .j  ava2s.com
            }
        }
        return a;
    }).collect(toList());
    atts.forEach(it -> parent.setAttribute(it));
}

From source file:com.init.octo.schema.XSDCache.java

License:Open Source License

private void cacheNamespace(Namespace namespace) {

    String prefix = namespace.getPrefix();
    String uri = namespace.getURI();

    namespaceCache[namespaceIdx].put(prefix, uri);

}