Example usage for org.jdom2 Element getText

List of usage examples for org.jdom2 Element getText

Introduction

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

Prototype

public String getText() 

Source Link

Document

Returns the textual content directly held under this element as a string.

Usage

From source file:ca.nrc.cadc.ac.xml.AbstractReaderWriter.java

License:Open Source License

/**
 * Get a PersonalDetails object from a JDOM element.
 *
 * @param element The PersonalDetails JDOM element.
 * @return A PersonalDetails object.// w ww  . j  av  a2  s  . co m
 * @throws ReaderException
 */
protected final PersonalDetails getPersonalDetails(Element element) throws ReaderException {
    if (element == null) {
        String error = "null personalDetails element";
        throw new ReaderException(error);
    }

    // firstName
    Element firstNameElement = element.getChild(FIRST_NAME);
    if (firstNameElement == null) {
        String error = "personalDetails missing required element firstName";
        throw new ReaderException(error);
    }
    String firstName = firstNameElement.getText();

    // lastName
    Element lastNameElement = element.getChild(LAST_NAME);
    if (lastNameElement == null) {
        String error = "personalDetails missing required element lastName";
        throw new ReaderException(error);
    }
    String lastName = lastNameElement.getText();

    PersonalDetails details = new PersonalDetails(firstName, lastName);

    // email
    Element emailElement = element.getChild(EMAIL);
    if (emailElement != null) {
        details.email = emailElement.getText();
    }

    // address
    Element addressElement = element.getChild(ADDRESS);
    if (addressElement != null) {
        details.address = addressElement.getText();
    }

    // institute
    Element instituteElement = element.getChild(INSTITUTE);
    if (instituteElement != null) {
        details.institute = instituteElement.getText();
    }

    // city
    Element cityElement = element.getChild(CITY);
    if (cityElement != null) {
        details.city = cityElement.getText();
    }

    // country
    Element countryElement = element.getChild(COUNTRY);
    if (countryElement != null) {
        details.country = countryElement.getText();
    }

    return details;
}

From source file:ca.nrc.cadc.ac.xml.AbstractReaderWriter.java

License:Open Source License

/**
 * Get a UserRequest object from a JDOM element.
 *
 * @param element The UserRequest JDOM element.
 * @return A UserRequest object./* w w w  .  j  a  va 2 s .c  o m*/
 * @throws ReaderException
 */
protected final Group getGroup(Element element) throws ReaderException {
    String uri = element.getAttributeValue(URI);
    if (uri == null) {
        String error = "group missing required uri attribute";
        throw new ReaderException(error);
    }

    // Group owner
    User user = null;
    Element ownerElement = element.getChild(OWNER);
    if (ownerElement != null) {
        // Owner user
        Element userElement = ownerElement.getChild(USER);
        if (userElement == null) {
            String error = "owner missing required user element";
            throw new ReaderException(error);
        }
        user = getUser(userElement);
    }

    GroupURI groupURI = new GroupURI(uri);
    Group group = new Group(groupURI);

    // set owner field
    setField(group, user, OWNER);

    // description
    Element descriptionElement = element.getChild(DESCRIPTION);
    if (descriptionElement != null) {
        group.description = descriptionElement.getText();
    }

    // lastModified
    Element lastModifiedElement = element.getChild(LAST_MODIFIED);
    if (lastModifiedElement != null) {
        try {
            DateFormat df = DateUtil.getDateFormat(DateUtil.IVOA_DATE_FORMAT, DateUtil.UTC);
            group.lastModified = df.parse(lastModifiedElement.getText());
        } catch (ParseException e) {
            String error = "Unable to parse group lastModified because " + e.getMessage();

            throw new ReaderException(error);
        }
    }

    // properties
    Element propertiesElement = element.getChild(PROPERTIES);
    if (propertiesElement != null) {
        List<Element> propertyElements = propertiesElement.getChildren(PROPERTY);
        for (Element propertyElement : propertyElements) {
            group.getProperties().add(getGroupProperty(propertyElement));
        }
    }

    // groupMembers
    Element groupMembersElement = element.getChild(GROUP_MEMBERS);
    if (groupMembersElement != null) {
        List<Element> groupElements = groupMembersElement.getChildren(GROUP);
        for (Element groupMember : groupElements) {
            group.getGroupMembers().add(getGroup(groupMember));
        }
    }

    // userMembers
    Element userMembersElement = element.getChild(USER_MEMBERS);
    if (userMembersElement != null) {
        List<Element> userElements = userMembersElement.getChildren(USER);
        for (Element userMember : userElements) {
            group.getUserMembers().add(getUser(userMember));
        }
    }

    // groupAdmins
    Element groupAdminsElement = element.getChild(GROUP_ADMINS);
    if (groupAdminsElement != null) {
        List<Element> groupElements = groupAdminsElement.getChildren(GROUP);
        for (Element groupMember : groupElements) {
            group.getGroupAdmins().add(getGroup(groupMember));
        }
    }

    // userAdmins
    Element userAdminsElement = element.getChild(USER_ADMINS);
    if (userAdminsElement != null) {
        List<Element> userElements = userAdminsElement.getChildren(USER);
        for (Element userMember : userElements) {
            group.getUserAdmins().add(getUser(userMember));
        }
    }

    return group;
}

From source file:ca.nrc.cadc.ac.xml.AbstractReaderWriter.java

License:Open Source License

/**
 * Get a GroupProperty object from a JDOM element.
 *
 * @param element The GroupProperty JDOM element.
 * @return A GroupProperty object.//from   w w w. j av  a  2  s . c  o m
 * @throws ReaderException
 */
protected final GroupProperty getGroupProperty(Element element) throws ReaderException {
    if (element == null) {
        String error = "null property element";
        throw new ReaderException(error);
    }

    if (!element.getName().equals(PROPERTY)) {
        String error = "expected property element name, found " + element.getName();
        throw new ReaderException(error);
    }

    String key = element.getAttributeValue(KEY);
    if (key == null) {
        String error = "required key attribute not found";
        throw new ReaderException(error);
    }

    String type = element.getAttributeValue(TYPE);
    if (type == null) {
        String error = "required type attribute not found";
        throw new ReaderException(error);
    }
    Object value;
    if (type.equals(STRING)) {
        value = String.valueOf(element.getText());
    } else {
        if (type.equals(INTEGER)) {
            value = Integer.valueOf(element.getText());
        } else {
            String error = "Unsupported GroupProperty type: " + type;
            throw new ReaderException(error);
        }
    }
    Boolean readOnly = Boolean.valueOf(element.getAttributeValue(READ_ONLY));

    return new GroupProperty(key, value, readOnly);
}

From source file:ca.nrc.cadc.ac.xml.AbstractReaderWriter.java

License:Open Source License

private void setInternalID(User user, Element element) throws ReaderException {
    Element uriElement = element.getChild(URI);
    if (uriElement == null) {
        String error = "expected uri element not found in internalID element";
        throw new ReaderException(error);
    }/*w  w w . j  a  va 2 s  .  c  om*/
    String text = uriElement.getText();
    URI uri;
    try {
        uri = new URI(text);
    } catch (URISyntaxException e) {
        throw new ReaderException("Invalid InternalID URI " + text, e);
    }

    InternalID internalID = new InternalID(uri);
    setField(user, internalID, ID);
}

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.
 * //from ww w.j a  va 2  s  .  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 PlaneURI's from the planeURI elements found in the inputs
 * element, and adds them to the given Set of PlaneURI's.
 * //from  w w  w  .  ja v a2s . co  m
 * @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

/**
 * Build an CoordBounds2D from a JDOM representation of an element named
 * name./*from w w w .  j a  v  a2  s  .  c  om*/
 * 
 * @param name
 *            the name of the Element.
 * @param parent
 *            the parent Element.
 * @param namespace
 *            of the document.
 * @param required
 *            is the element expected to be found.
 * @return an CoordBounds2D, or null if the document doesn't contain element
 *         named name.
 * @throws ObservationParsingException
 */
protected CoordBounds2D getCoordBounds2D(String name, Element parent, Namespace namespace, boolean required)
        throws ObservationParsingException {
    Element element = getChildElement(name, parent, namespace, required);
    if (element == null || element.getContentSize() == 0) {
        return null;
    }

    // Look for a CoordCircle2D which has a center and a radius.
    CoordCircle2D circle = getCoordCircle2D("circle", element, namespace, false);
    if (circle != null) {
        return circle;
    }

    // Look for a CoordPolygon2D which has a list of Coord2D vertices.
    CoordPolygon2D polygon = getCoordPolygon2D("polygon", element, namespace, false);
    if (polygon != null) {
        return polygon;
    }

    // Unknown children.
    String error = "Unsupported element found in " + name + ": " + element.getText();
    throw new ObservationParsingException(error);
}

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

License:Open Source License

protected String getChildText(String name, Element element, Namespace ns, boolean required)
        throws ObservationParsingException {
    Element child = getChildElement(name, element, ns, required);
    if (child != null) {
        return cleanWhitespace(child.getText());
    }//from  w  ww . j  ava2  s. com
    return null;
}

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

License:Open Source License

protected Boolean getChildTextAsBoolean(String name, Element element, Namespace ns, boolean required)
        throws ObservationParsingException {
    Element child = getChildElement(name, element, ns, required);
    if (child != null) {
        return Boolean.valueOf(child.getText());
    }//  ww  w .  j  av a2 s  . com
    return null;
}

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

License:Open Source License

protected Integer getChildTextAsInteger(String name, Element element, Namespace ns, boolean required)
        throws ObservationParsingException {
    Element child = getChildElement(name, element, ns, required);
    if (child != null) {
        return Integer.valueOf(child.getText());
    }/* w  ww  .  j  a v a  2s. c om*/
    return null;
}