Example usage for org.jdom2 Element getName

List of usage examples for org.jdom2 Element getName

Introduction

In this page you can find the example usage for org.jdom2 Element getName.

Prototype

public String getName() 

Source Link

Document

Returns the (local) name of the element (without any namespace prefix).

Usage

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

License:Open Source License

public ArtifactAccess read(Reader reader) throws IOException {
    if (reader == null) {
        throw new IllegalArgumentException("reader must not be null");
    }//from  w ww .java 2  s  . com

    Document document;
    try {
        document = XmlUtil.buildDocument(reader);
    } catch (JDOMException ex) {
        throw new IllegalArgumentException("invalid input document", ex);
    }

    // Root element and namespace of the Document
    Element root = document.getRootElement();
    if (!ENAMES.artifactAccess.name().equals(root.getName())) {
        throw new IllegalArgumentException(
                "invalid root element: " + root.getName() + " expected: " + ENAMES.artifactAccess);
    }

    Artifact a = getArtifact(root.getChild(ENAMES.artifact.name()));
    ArtifactAccess ret = new ArtifactAccess(a);

    ret.isPublic = getBoolean(root.getChildTextTrim(ENAMES.isPublic.name()));

    getGroupList(ret.getReadGroups(), ENAMES.readGroups.name(), root.getChildren(ENAMES.readGroups.name()));

    return ret;
}

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

License:Open Source License

private void getGroupList(List<URI> fill, String ename, List<Element> elements) {
    if (elements == null || elements.isEmpty()) {
        return;/* w w w .jav  a 2s.c  o m*/
    }

    if (elements.size() > 1) {
        throw new IllegalArgumentException(
                "invalid input document: found multiple " + ename + " expected: 0 or 1");
    }
    Element ge = elements.get(0);
    for (Element e : ge.getChildren()) {
        if (!ENAMES.uri.name().equals(e.getName())) {
            throw new IllegalArgumentException(
                    "invalid child element in " + ename + ": " + e.getName() + " expected: uri");
        }
        URI guri = getURI(e.getTextTrim(), true);
        fill.add(guri);
    }
}

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

License:Open Source License

/**
 * Creates ObservationURI from the observationURI elements found in the
 * members element, and adds them to the given Set of ObservationURI's.
 * //ww w . j  a va  2s.co  m
 * @param members
 *            Set of Member's from the Observation.
 * @param parent
 *            the parent Element.
 * @param namespace
 *            of the document.
 * @param rc
 * @throws ObservationParsingException
 */
protected void addMembers(Set<ObservationURI> members, Element parent, Namespace namespace, ReadContext rc)
        throws ObservationParsingException {
    Element element = getChildElement("members", parent, namespace, false);
    if (element != null) {
        List children = getChildrenElements("observationURI", element, namespace, false);
        Iterator it = children.iterator();
        while (it.hasNext()) {
            Element child = (Element) it.next();
            try {
                members.add(new ObservationURI(new URI(child.getText())));
            } catch (URISyntaxException e) {
                String error = "Unable to parse observationURI " + child.getText()
                        + " in to an ObservationURI in element " + element.getName() + " because "
                        + e.getMessage();
                throw new ObservationParsingException(error);
            }
        }
    }
}

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

License:Open Source License

/**
 * Creates Plane's from the plane elements found in the planes element, and
 * adds them to the given Set of Plane's.
 * //from www.j a v a 2s .com
 * @param planes
 *            the Set of Plane's from the Observation.
 * @param parent
 *            the parent Element.
 * @param namespace
 *            of the document.
 * @param rc
 * @throws ObservationParsingException
 */
protected void addPlanes(Set<Plane> planes, Element parent, Namespace namespace, ReadContext rc)
        throws ObservationParsingException {
    Element element = getChildElement("planes", parent, namespace, false);
    if (element == null || element.getContentSize() == 0) {
        return;
    }

    List planeElements = getChildrenElements("plane", element, namespace, false);
    Iterator it = planeElements.iterator();
    while (it.hasNext()) {
        Element planeElement = (Element) it.next();
        String productID = getChildText("productID", planeElement, namespace, true);
        Plane plane = new Plane(productID);

        plane.metaRelease = getChildTextAsDate("metaRelease", planeElement, namespace, false, rc.dateFormat);
        plane.dataRelease = getChildTextAsDate("dataRelease", planeElement, namespace, false, rc.dateFormat);

        String creatorIDStr = getChildText("creatorID", planeElement, namespace, false);
        if (creatorIDStr != null) {
            try {
                plane.creatorID = new URI(creatorIDStr);
            } catch (URISyntaxException e) {
                String error = "Unable to parse creatorID " + creatorIDStr + " to URI in element "
                        + element.getName() + " because " + e.getMessage();
                throw new ObservationParsingException(error, e);
            }
        }

        String dataProductType = getChildText("dataProductType", planeElement, namespace, false);
        if (dataProductType != null) {
            plane.dataProductType = DataProductType.toValue(dataProductType);
        }

        String calibrationLevel = getChildText("calibrationLevel", planeElement, namespace, false);
        if (calibrationLevel != null) {
            plane.calibrationLevel = CalibrationLevel.toValue(Integer.parseInt(calibrationLevel));
        }

        plane.provenance = getProvenance(planeElement, namespace, rc);
        plane.metrics = getMetrics(planeElement, namespace, rc);
        plane.quality = getQuality(planeElement, namespace, rc);

        plane.position = getPosition(planeElement, namespace, rc);
        plane.energy = getEnergy(planeElement, namespace, rc);
        plane.time = getTime(planeElement, namespace, rc);
        plane.polarization = getPolarization(planeElement, namespace, rc);

        addArtifacts(plane.getArtifacts(), planeElement, namespace, rc);

        assignEntityAttributes(planeElement, plane, rc);

        boolean added = planes.add(plane);
        if (!added) {
            throw new IllegalArgumentException("Plane.productID = " + productID + " is not unique");
        }
    }
}

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

License:Open Source License

/**
 * Build a Provenance from a JDOM representation of an Provenance.
 * /* ww  w  .  j  a v  a 2  s.c o  m*/
 * @param parent
 *            the parent Element.
 * @param namespace
 *            of the document.
 * @param rc
 * @return an Provenance, or null if the document doesn't contain a
 *         provenance element.
 * @throws ObservationParsingException
 */
protected Provenance getProvenance(Element parent, Namespace namespace, ReadContext rc)
        throws ObservationParsingException {
    Element element = getChildElement("provenance", parent, namespace, false);
    if (element == null || element.getContentSize() == 0) {
        return null;
    }

    String name = getChildText("name", element, namespace, true);
    Provenance provenance = new Provenance(name);

    provenance.version = getChildText("version", element, namespace, false);
    provenance.project = getChildText("project", element, namespace, false);
    provenance.producer = getChildText("producer", element, namespace, false);
    provenance.runID = getChildText("runID", element, namespace, false);
    String reference = getChildText("reference", element, namespace, false);
    if (reference != null) {
        try {
            provenance.reference = new URI(reference);
        } catch (URISyntaxException e) {
            String error = "Unable to parse reference " + reference + " to URI in element " + element.getName()
                    + " because " + e.getMessage();
            throw new ObservationParsingException(error);
        }
    }
    provenance.lastExecuted = getChildTextAsDate("lastExecuted", element, namespace, false, rc.dateFormat);
    if (rc.docVersion < 23) {
        addChildTextToStringList("keywords", provenance.getKeywords(), element, namespace, false);
    } else {
        addKeywordsToList(provenance.getKeywords(), element, namespace);
    }
    addInputs(provenance.getInputs(), element, namespace, rc);

    return provenance;
}

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

License:Open Source License

/**
 * Creates PlaneURI's from the planeURI elements found in the inputs
 * element, and adds them to the given Set of PlaneURI's.
 * /*from   ww  w.  j  ava2 s . c om*/
 * @param inputs
 *            the Set of PlaneURI from the Provenance.
 * @param parent
 *            the parent Element.
 * @param namespace
 *            of the document.
 * @param rc
 * @throws ObservationParsingException
 */
protected void addInputs(Set<PlaneURI> inputs, Element parent, Namespace namespace, ReadContext rc)
        throws ObservationParsingException {
    Element element = getChildElement("inputs", parent, namespace, false);
    if (element != null) {
        List children = getChildrenElements("planeURI", element, namespace, false);
        Iterator it = children.iterator();
        while (it.hasNext()) {
            Element child = (Element) it.next();
            try {
                inputs.add(new PlaneURI(new URI(child.getText())));
            } catch (URISyntaxException e) {
                String error = "Unable to parse observationURI " + child.getText()
                        + " in to an ObservationURI in element " + element.getName() + " because "
                        + e.getMessage();
                throw new ObservationParsingException(error);
            }
        }
    }
}

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

License:Open Source License

/**
 * Creates Artifact's from the artifact elements found in the artifacts
 * element, and adds them to the given Set of Artifact's.
 * /*from   w w  w  . jav  a 2s.  c  om*/
 * @param artifacts
 *            the Set of Artifact's from the Plane.
 * @param parent
 *            the parent Element.
 * @param namespace
 *            of the document.
 * @param rc
 * @throws ObservationParsingException
 */
protected void addArtifacts(Set<Artifact> artifacts, Element parent, Namespace namespace, ReadContext rc)
        throws ObservationParsingException {
    Element element = getChildElement("artifacts", parent, namespace, false);
    if (element == null || element.getContentSize() == 0) {
        return;
    }

    List artifactElements = getChildrenElements("artifact", element, namespace, false);
    Iterator it = artifactElements.iterator();
    while (it.hasNext()) {
        Element artifactElement = (Element) it.next();
        String uri = getChildText("uri", artifactElement, namespace, true);

        String pts = getChildText("productType", artifactElement, namespace, false);
        ProductType productType = null;
        if (pts != null) {
            productType = ProductType.toValue(pts);
        } else {
            productType = ProductType.SCIENCE;
            log.warn("assigning default Artifact.productType = " + productType + " for " + uri);
        }

        String rts = getChildText("releaseType", artifactElement, namespace, false);
        ReleaseType releaseType = null;
        if (rts != null) {
            releaseType = ReleaseType.toValue(rts);
        } else {
            releaseType = ReleaseType.DATA;
            log.warn("assigning default Artifact.releaseType = " + releaseType + " for " + uri);
        }

        Artifact artifact;
        try {
            artifact = new Artifact(new URI(uri), productType, releaseType);
        } catch (URISyntaxException e) {
            String error = "Unable to parse uri " + uri + " in to a URI in element " + artifactElement.getName()
                    + " because " + e.getMessage();
            throw new ObservationParsingException(error);
        }

        artifact.contentType = getChildText("contentType", artifactElement, namespace, false);
        artifact.contentLength = getChildTextAsLong("contentLength", artifactElement, namespace, false);

        String contentChecksumStr = getChildText("contentChecksum", artifactElement, namespace, false);
        if (contentChecksumStr != null) {
            try {
                artifact.contentChecksum = new URI(contentChecksumStr);
            } catch (URISyntaxException e) {
                String error = "Unable to parse contentChecksum " + uri + " into a URI in element "
                        + artifactElement.getName() + " because " + e.getMessage();
                throw new ObservationParsingException(error, e);
            }
        }

        addParts(artifact.getParts(), artifactElement, namespace, rc);

        assignEntityAttributes(artifactElement, artifact, rc);

        boolean added = artifacts.add(artifact);
        if (!added) {
            throw new IllegalArgumentException("Artifact.uri = " + uri + " is not unique");
        }
    }
}

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

License:Open Source License

protected Element getChildElement(String name, Element element, Namespace ns, boolean required)
        throws ObservationParsingException {
    Element child = element.getChild(name, ns);
    if (required && child == null) {
        String error = name + " element not found in " + element.getName();
        throw new ObservationParsingException(error);
    }//  w  w w .j  a v  a  2  s  .c om
    return child;
}

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

License:Open Source License

protected Date getChildTextAsDate(String name, Element element, Namespace ns, boolean required,
        DateFormat dateFormat) throws ObservationParsingException {
    String child = getChildText(name, element, ns, required);
    if (child != null) {
        try {/*from  ww  w  . j a  v  a 2 s .c  o m*/
            return DateUtil.flexToDate(child, dateFormat);
        } catch (ParseException ex) {
            String error = "Unable to parse " + name + " in " + element.getName() + " to a date because "
                    + ex.getMessage();
            throw new ObservationParsingException(error, ex);
        }
    }
    return null;
}

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

License:Open Source License

protected List getChildrenElements(String name, Element element, Namespace ns, boolean required)
        throws ObservationParsingException {
    List children = element.getChildren(name, ns);
    if (required && children.isEmpty()) {
        String error = name + " element not found in " + element.getName();
        throw new ObservationParsingException(error);
    }/* www.  j  a  v a 2  s.c  o  m*/
    return children;
}