Example usage for org.jdom2 Element Element

List of usage examples for org.jdom2 Element Element

Introduction

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

Prototype

public Element(final String name) 

Source Link

Document

Create a new element with the supplied (local) name and no namespace.

Usage

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

License:Open Source License

/**
 * Get a JDOM element from a PosixDetails object.
 *
 * @param details The PosixDetails.//from ww  w .j ava  2  s  .  c  o m
 * @return A JDOM PosixDetails representation.
 */
protected final Element getElement(PosixDetails details) throws WriterException {
    if (details == null) {
        String error = "null PosixDetails";
        throw new WriterException(error);
    }

    Element detailsElement = new Element(POSIX_DETAILS);

    Element usernameElement = new Element(USERNAME);
    usernameElement.setText(details.getUsername());
    detailsElement.addContent(usernameElement);

    Element uidElement = new Element(UID);
    uidElement.setText(String.valueOf(details.getUid()));
    detailsElement.addContent(uidElement);

    Element gidElement = new Element(GID);
    gidElement.setText(String.valueOf(details.getGid()));
    detailsElement.addContent(gidElement);

    Element homeDirElement = new Element(HOME_DIRECTORY);
    homeDirElement.setText(details.getHomeDirectory());
    detailsElement.addContent(homeDirElement);

    return detailsElement;
}

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

License:Open Source License

/**
 * Get a JDOM element from a PersonalDetails object.
 *
 * @param details The PersonalDetails.// w w w  .  j  a  va  2s  .  co  m
 * @return JDOM PersonalDetails representation.
 */
protected final Element getElement(PersonalDetails details) throws WriterException {
    if (details == null) {
        String error = "null PersonalDetails";
        throw new WriterException(error);
    }

    Element detailsElement = new Element(PERSONAL_DETAILS);

    Element firstNameElement = new Element(FIRST_NAME);
    firstNameElement.setText(details.getFirstName());
    detailsElement.addContent(firstNameElement);

    Element lastNameElement = new Element(LAST_NAME);
    lastNameElement.setText(details.getLastName());
    detailsElement.addContent(lastNameElement);

    if (details.email != null) {
        Element emailElement = new Element(EMAIL);
        emailElement.setText(details.email);
        detailsElement.addContent(emailElement);
    }

    if (details.address != null) {
        Element addressElement = new Element(ADDRESS);
        addressElement.setText(details.address);
        detailsElement.addContent(addressElement);
    }

    if (details.institute != null) {
        Element instituteElement = new Element(INSTITUTE);
        instituteElement.setText(details.institute);
        detailsElement.addContent(instituteElement);
    }

    if (details.city != null) {
        Element cityElement = new Element(CITY);
        cityElement.setText(details.city);
        detailsElement.addContent(cityElement);
    }

    if (details.country != null) {
        Element countryElement = new Element(COUNTRY);
        countryElement.setText(details.country);
        detailsElement.addContent(countryElement);
    }

    return detailsElement;
}

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

License:Open Source License

/**
 * Get a JDOM element from a Group object.
 *
 * @param group The UserRequest.//from  w  w  w .  j av a 2 s  .co m
 * @param deepCopy Return all Group elements.
 * @return A JDOM Group representation.
 * @throws WriterException
 */
protected final Element getElement(Group group, boolean deepCopy) throws WriterException {
    if (group == null) {
        throw new WriterException("null Group");
    }

    // Create the root group element.
    Element groupElement = new Element(GROUP);
    String groupURI = group.getID().toString();
    groupElement.setAttribute(new Attribute(URI, groupURI));

    // Group owner
    if (group.getOwner() != null) {
        Element ownerElement = new Element(OWNER);
        Element userElement = getElement(group.getOwner());
        ownerElement.addContent(userElement);
        groupElement.addContent(ownerElement);
    }

    if (deepCopy) {
        // Group description
        if (group.description != null) {
            Element descriptionElement = new Element(DESCRIPTION);
            descriptionElement.setText(group.description);
            groupElement.addContent(descriptionElement);
        }

        // lastModified
        if (group.lastModified != null) {
            Element lastModifiedElement = new Element(LAST_MODIFIED);
            DateFormat df = DateUtil.getDateFormat(DateUtil.IVOA_DATE_FORMAT, DateUtil.UTC);
            lastModifiedElement.setText(df.format(group.lastModified));
            groupElement.addContent(lastModifiedElement);
        }

        // Group properties
        if (!group.getProperties().isEmpty()) {
            Element propertiesElement = new Element(PROPERTIES);
            for (GroupProperty property : group.getProperties()) {
                propertiesElement.addContent(getElement(property));
            }
            groupElement.addContent(propertiesElement);
        }

        // Group groupMembers.
        if ((group.getGroupMembers() != null) && (!group.getGroupMembers().isEmpty())) {
            Element groupMembersElement = new Element(GROUP_MEMBERS);
            for (Group groupMember : group.getGroupMembers()) {
                groupMembersElement.addContent(getElement(groupMember, false));
            }
            groupElement.addContent(groupMembersElement);
        }

        // Group userMembers
        if ((group.getUserMembers() != null) && (!group.getUserMembers().isEmpty())) {
            Element userMembersElement = new Element(USER_MEMBERS);
            for (User userMember : group.getUserMembers()) {
                userMembersElement.addContent(getElement(userMember));
            }
            groupElement.addContent(userMembersElement);
        }

        // Group groupAdmins.
        if ((group.getGroupAdmins() != null) && (!group.getGroupAdmins().isEmpty())) {
            Element groupAdminsElement = new Element(GROUP_ADMINS);
            for (Group groupMember : group.getGroupAdmins()) {
                groupAdminsElement.addContent(getElement(groupMember, false));
            }
            groupElement.addContent(groupAdminsElement);
        }

        // Group userAdmins
        if ((group.getUserAdmins() != null) && (!group.getUserAdmins().isEmpty())) {
            Element userAdminsElement = new Element(USER_ADMINS);
            for (User userMember : group.getUserAdmins()) {
                userAdminsElement.addContent(getElement(userMember));
            }
            groupElement.addContent(userAdminsElement);
        }
    }

    return groupElement;
}

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

License:Open Source License

/**
 * Get a JDOM element from a GroupProperty object.
 *
 * @param property The GroupProperty.//from w ww . j  ava 2s .  com
 * @return A JDOM GroupProperty representation.
 * @throws WriterException
 */
protected final Element getElement(GroupProperty property) throws WriterException {
    if (property == null) {
        throw new WriterException("null GroupProperty");
    }

    Element propertyElement = new Element(PROPERTY);
    propertyElement.setAttribute(KEY, property.getKey());
    if (property.isReadOnly()) {
        propertyElement.setAttribute(READ_ONLY, Boolean.TRUE.toString());
    }

    Object value = property.getValue();
    if ((value instanceof String)) {
        propertyElement.setAttribute(TYPE, STRING);
    } else if ((value instanceof Integer)) {
        propertyElement.setAttribute(TYPE, INTEGER);
    } else {
        String error = "Unsupported value type: " + value.getClass().getSimpleName();
        throw new IllegalArgumentException(error);
    }
    propertyElement.setText(String.valueOf(property.getValue()));

    return propertyElement;
}

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

License:Open Source License

/**
 * Get a JDOM element from a Collection of Group objects.
 *
 * @param groups Collection of Group's to write.
 * @return A JDOM Group list representation.
 * @throws WriterException//  www  .ja v a  2s  .  c o m
 */
protected final Element getElement(Collection<Group> groups) throws WriterException {
    Element groupsElement = new Element(GROUPS);

    for (Group group : groups) {
        groupsElement.addContent(getElement(group));
    }

    return groupsElement;
}

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

License:Open Source License

/**
 * Get a JDOM element from a Collection of User objects.
 *
 * @param users Collection of User's to write.
 * @return A JDOM Group list representation.
 * @throws WriterException/*  www .j  a  v  a2  s .co  m*/
 */
protected final <T extends Principal> Element getElement(Collection<User> users) throws WriterException {
    Element usersElement = new Element("users");

    for (User user : users) {
        usersElement.addContent(getElement(user));
    }

    return usersElement;
}

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

License:Open Source License

public void write(ArtifactAccess aa, Writer writer) throws IOException {
    Element root = new Element(ArtifactAccessReader.ENAMES.artifactAccess.name());

    Element ae = new Element(ArtifactAccessReader.ENAMES.artifact.name());
    root.addContent(ae);//  ww w.  ja  v  a2 s.  c  o  m

    addChild(ae, ArtifactAccessReader.ENAMES.uri.name(), aa.getArtifact().getURI().toASCIIString());
    addChild(ae, ArtifactAccessReader.ENAMES.productType.name(), aa.getArtifact().getProductType().getValue());
    addChild(ae, ArtifactAccessReader.ENAMES.releaseType.name(), aa.getArtifact().getReleaseType().getValue());
    if (aa.getArtifact().contentChecksum != null) {
        addChild(ae, ArtifactAccessReader.ENAMES.contentChecksum.name(),
                aa.getArtifact().contentChecksum.toASCIIString());
    }
    if (aa.getArtifact().contentLength != null) {
        addChild(ae, ArtifactAccessReader.ENAMES.contentLength.name(),
                aa.getArtifact().contentLength.toString());
    }
    if (aa.getArtifact().contentType != null) {
        addChild(ae, ArtifactAccessReader.ENAMES.contentType.name(), aa.getArtifact().contentType);
    }

    Element pub = new Element(ArtifactAccessReader.ENAMES.isPublic.name());
    pub.setText(Boolean.toString(aa.isPublic));
    root.addContent(pub);

    Element rg = new Element(ArtifactAccessReader.ENAMES.readGroups.name());
    if (!aa.getReadGroups().isEmpty() || writeEmptyCollections) {
        root.addContent(rg);
    }
    addGroups(aa.getReadGroups(), rg);

    Document doc = new Document(root);
    XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat(Format.getPrettyFormat());
    outputter.output(doc, writer);
}

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

License:Open Source License

private void addChild(Element parent, String ename, String eval) {
    Element uri = new Element(ename);
    uri.setText(eval);/*from   ww w  .ja  v a 2  s.  c o  m*/
    parent.addContent(uri);
}

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

License:Open Source License

private void addGroups(List<URI> groups, Element parent) {
    for (URI u : groups) {
        Element uri = new Element(ArtifactAccessReader.ENAMES.uri.name());
        uri.setText(u.toASCIIString());/*  w w  w. j  av a2  s .c o  m*/
        parent.addContent(uri);
    }
}

From source file:ca.nrc.cadc.tap.writer.RssTableWriter.java

License:Open Source License

@Override
public void write(ResultSet resultSet, Writer out, Long maxrec) throws IOException {
    if (selectList == null)
        throw new IllegalStateException("SelectList cannot be null, set using setSelectList()");

    List<Format<Object>> formats = formatFactory.getFormats(selectList);

    if (resultSet != null)
        try {/*  w ww. ja v  a 2  s.c  om*/
            log.debug("resultSet column count: " + resultSet.getMetaData().getColumnCount());
        } catch (Exception oops) {
            log.error("failed to check resultset column count", oops);
        }

    // JDOM document.
    Document document = new Document();

    // Root element.
    Element rss = new Element("rss");
    rss.setAttribute("version", "2.0");
    document.setRootElement(rss);

    // channel element.
    Element channel = new Element("channel");
    rss.addContent(channel);

    // channel title.
    Element channelTitle = new Element("title");
    channelTitle.setText(info);
    channel.addContent(channelTitle);

    StringBuilder qp = new StringBuilder();
    qp.append("http://");
    qp.append(NetUtil.getServerName(null));
    qp.append(job.getRequestPath());
    qp.append("?");
    for (Parameter parameter : job.getParameterList()) {
        qp.append(parameter.getName());
        qp.append("=");
        qp.append(parameter.getValue());
        qp.append("&");
    }
    String queryString = qp.substring(0, qp.length() - 1); // strip trailing &
    Element link = new Element("link");
    link.setText(queryString);
    channel.addContent(link);

    // items.
    int itemCount = 0;
    try {
        while (resultSet.next()) {
            // item element.
            Element item = new Element("item");

            // item description.
            Element itemDescription = new Element("description");
            StringBuilder sb = new StringBuilder();
            sb.append("<table>");

            // Loop through the ResultSet adding the table data elements.
            for (int columnIndex = 1; columnIndex <= resultSet.getMetaData().getColumnCount(); columnIndex++) {
                String columnLabel = resultSet.getMetaData().getColumnLabel(columnIndex);

                if (columnLabel.equalsIgnoreCase("rss_title")) {
                    String titleStr = resultSet.getString("rss_title");
                    log.debug("item title: " + titleStr);
                    Element itemTitle = new Element("title");
                    itemTitle.setText(titleStr);
                    item.addContent(itemTitle);
                } else if (columnLabel.equalsIgnoreCase("rss_link")) {
                    String linkStr = resultSet.getString("rss_link");
                    log.debug("item link: " + linkStr);
                    Element itemLink = new Element("link");
                    itemLink.setText(linkStr);
                    item.addContent(itemLink);
                } else if (columnLabel.equalsIgnoreCase("rss_pubDate")) {
                    Timestamp ts = resultSet.getTimestamp("rss_pubDate");
                    String pubDateStr = dateFormat.format(ts);
                    log.debug("item pubDate: " + pubDateStr);
                    Element itemPubDate = new Element("pubDate");
                    itemPubDate.setText(pubDateStr);
                    item.addContent(itemPubDate);
                } else {
                    ParamDesc paramDesc = selectList.get(columnIndex - 1);
                    sb.append("<tr><td align=\"right\">");
                    sb.append(paramDesc.name);
                    sb.append("</td><td align=\"left\">");
                    Format<Object> format = formats.get(columnIndex - 1);
                    Object obj = null;
                    if (format instanceof ResultSetFormat)
                        obj = ((ResultSetFormat) format).extract(resultSet, columnIndex);
                    else
                        obj = resultSet.getObject(columnIndex);
                    sb.append(format.format(obj));
                    sb.append("</td></tr>");
                }
            }
            sb.append("</table>");
            itemDescription.setText(sb.toString());
            item.addContent(itemDescription);
            channel.addContent(item);
            itemCount++;

            // Write MaxRows
            if (itemCount == maxrec)
                break;
        }
    } catch (SQLException e) {
        throw new RuntimeException(e.getMessage());
    }

    // channel description.
    Element channelDescription = new Element("description");
    channelDescription.setText("The " + itemCount + " most recent from " + info);
    channel.addContent(channelDescription);

    // Write out the VOTABLE.
    XMLOutputter outputter = new XMLOutputter(org.jdom2.output.Format.getPrettyFormat());
    outputter.output(document, out);

}