Example usage for org.jdom2 Element getParent

List of usage examples for org.jdom2 Element getParent

Introduction

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

Prototype

public Parent getParent() 

Source Link

Document

Return this child's parent, or null if this child is currently not attached.

Usage

From source file:org.mycore.common.xml.MCRXPathBuilder.java

License:Open Source License

/**
 * Builds an absolute XPath expression for the given element within a JDOM XML structure.
 * In case any ancestor element in context is not the first one, the XPath will also contain position predicates.
 * For all namespaces commonly used in MyCoRe, their namespace prefixes will be used.
 *
 * @param element a JDOM element//from  ww  w  . ja  va2s  . c o  m
 * @return absolute XPath of that element. In case there is a root Document, it will begin with a "/".
 */
public static String buildXPath(Element element) {
    if (element == null)
        return "";

    String parentXPath = buildXPath(element.getParent());
    if ((!parentXPath.isEmpty()) || (element.getParent() instanceof Document))
        parentXPath += "/";
    return parentXPath + buildChildPath(element);
}

From source file:org.mycore.common.xml.MCRXPathBuilder.java

License:Open Source License

private static String buildPositionPredicate(Element element) {
    Parent parent = element.getParent();
    if ((parent instanceof Document) || (parent == null))
        return "";

    Element parentElement = (Element) parent;
    int pos = parentElement.getChildren(element.getName(), element.getNamespace()).indexOf(element);
    return (pos == 0 ? "" : "[" + ++pos + "]");
}

From source file:org.onebusaway.rss.impl.ServiceAlertRssModuleGenerator.java

License:Apache License

@Override
public void generate(Module module, 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();
    }/*from  ww w .j a va2  s .com*/
    root.addNamespaceDeclaration(SERVICE_ALERT_NS);
    Element sas = null;

    Element channel = getElement(root, "channel");
    if (channel == null) {
        throw new RuntimeException("missing channel from element=" + root);
    }
    sas = getElement(channel, "ServiceAlerts");
    if (sas == null) {
        sas = generateSimpleElement("ServiceAlerts", "");
        sas.setName("ServiceAlerts");
        channel.addContent(sas);
    }

    if (module instanceof IServiceAlert) {
        IServiceAlert isa = (IServiceAlert) module;
        if (isa.getId() != null) {
            Element sa = generateSimpleElement("ServiceAlert", "");
            sa.setName("ServiceAlert");
            sa.addContent(generateSimpleElement("id", isa.getId()));
            sa.addContent(generateSimpleElement("summary", isa.getSummary()));
            sa.addContent(generateSimpleElement("description", isa.getDescription()));
            sa.addContent(generateSimpleElement("reason", isa.getReason()));
            sa.addContent(generateSimpleElement("severity", isa.getSeverity()));
            if (isa.getPublicationWindows() != null && !isa.getPublicationWindows().isEmpty()) {
                Element publicationWindows = generateSimpleElement("PublicationWindows", "");
                publicationWindows.setName("PublicationWindows");
                sa.addContent(publicationWindows);
                for (TimeRangeRssBean trb : isa.getPublicationWindows()) {
                    Element timeRange = generateSimpleElement("TimeRange", "");
                    timeRange.addContent(generateSimpleElement("from", "" + trb.getFrom()));
                    timeRange.addContent(generateSimpleElement("to", "" + trb.getTo()));
                    publicationWindows.addContent(timeRange);
                }
            }
            if (isa.getAffectsClauses() != null && !isa.getAffectsClauses().isEmpty()) {
                Element affectsClauses = generateSimpleElement("AffectsClauses", "");
                affectsClauses.setName("AffectsClauses");
                sa.addContent(affectsClauses);
                for (AffectsClauseRssBean acrb : isa.getAffectsClauses()) {
                    Element affect = generateSimpleElement("Affect", "");
                    if (acrb.getAgencyId() != null && acrb.getRouteId() == null && acrb.getStopId() == null
                            && acrb.getTripId() == null)
                        affect.addContent(generateAgencyElement("agencyId", acrb.getAgencyId()));
                    if (acrb.getRouteId() != null)
                        affect.addContent(generateAgencyElement("routeId", acrb.getRouteId()));
                    if (acrb.getStopId() != null)
                        affect.addContent(generateAgencyElement("stopId", acrb.getStopId()));
                    if (acrb.getTripId() != null)
                        affect.addContent(generateAgencyElement("tripId", acrb.getTripId()));
                    affectsClauses.addContent(affect);
                }
            }

            sas.addContent(sa);

        }
    } else {
        _log.error("unknown type=" + module);
    }

}

From source file:org.rometools.feed.module.content.io.ContentModuleGenerator.java

License:Open Source License

public void generate(com.sun.syndication.feed.module.Module module, org.jdom2.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) root.getParent();
    }// ww w. j  a va 2  s .  c  om

    root.addNamespaceDeclaration(CONTENT_NS);

    if (!(module instanceof ContentModule)) {
        return;
    }

    ContentModule cm = (ContentModule) module;

    List encodeds = cm.getEncodeds();

    //
    if (encodeds != null) {
        System.out.println(cm.getEncodeds().size());
        for (int i = 0; i < encodeds.size(); i++) {
            element.addContent(generateCDATAElement("encoded", encodeds.get(i).toString()));
        }
    }

    List contentItems = cm.getContentItems();

    if ((contentItems != null) && (contentItems.size() > 0)) {
        Element items = new Element("items", CONTENT_NS);
        Element bag = new Element("Bag", RDF_NS);
        items.addContent(bag);

        for (int i = 0; i < contentItems.size(); i++) {
            ContentItem contentItem = (ContentItem) contentItems.get(i);
            Element li = new Element("li", RDF_NS);
            Element item = new Element("item", CONTENT_NS);

            if (contentItem.getContentAbout() != null) {
                Attribute about = new Attribute("about", contentItem.getContentAbout(), RDF_NS);
                item.setAttribute(about);
            }

            if (contentItem.getContentFormat() != null) {
                //System.out.println( "Format");
                Element format = new Element("format", CONTENT_NS);
                Attribute formatResource = new Attribute("resource", contentItem.getContentFormat(), RDF_NS);
                format.setAttribute(formatResource);

                item.addContent(format);
            }

            if (contentItem.getContentEncoding() != null) {
                //System.out.println( "Encoding");
                Element encoding = new Element("encoding", CONTENT_NS);
                Attribute encodingResource = new Attribute("resource", contentItem.getContentEncoding(),
                        RDF_NS);
                encoding.setAttribute(encodingResource);
                item.addContent(encoding);
            }

            if (contentItem.getContentValue() != null) {
                Element value = new Element("value", RDF_NS);

                if (contentItem.getContentValueParseType() != null) {
                    Attribute parseType = new Attribute("parseType", contentItem.getContentValueParseType(),
                            RDF_NS);
                    value.setAttribute(parseType);
                }

                if (contentItem.getContentValueNamespaces() != null) {
                    List namespaces = contentItem.getContentValueNamespaces();

                    for (int ni = 0; ni < namespaces.size(); ni++) {
                        value.addNamespaceDeclaration((Namespace) namespaces.get(ni));
                    }
                }

                List detached = new ArrayList();

                for (int c = 0; c < contentItem.getContentValueDOM().size(); c++) {
                    detached.add(
                            ((Content) ((Content) contentItem.getContentValueDOM().get(c)).clone()).detach());
                }

                value.setContent(detached);
                item.addContent(value);
            } // end value

            li.addContent(item);
            bag.addContent(li);
        } //end contentItems loop

        element.addContent(items);
    }
}

From source file:org.rometools.feed.module.georss.GMLGenerator.java

License:Apache License

public void generate(Module module, 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();
    }/*from   w  w  w  .  ja  v  a  2s.c o  m*/
    root.addNamespaceDeclaration(GeoRSSModule.SIMPLE_NS);
    root.addNamespaceDeclaration(GeoRSSModule.GML_NS);

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

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

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

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

        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) {
        PositionList posList = ((LineString) geometry).getPositionList();

        Element lineElement = new Element("LineString", GeoRSSModule.GML_NS);
        lineElement.addContent(createPosListElement(posList));
        whereElement.addContent(lineElement);
    } else if (geometry instanceof Polygon) {
        Element polygonElement = new Element("Polygon", GeoRSSModule.GML_NS);
        {
            AbstractRing ring = ((Polygon) geometry).getExterior();
            if (ring instanceof LinearRing) {
                Element exteriorElement = new Element("exterior", GeoRSSModule.GML_NS);
                polygonElement.addContent(exteriorElement);
                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());
            }
        }
        List interiorList = ((Polygon) geometry).getInterior();
        Iterator it = interiorList.iterator();
        while (it.hasNext()) {
            AbstractRing ring = (AbstractRing) it.next();
            if (ring instanceof LinearRing) {
                Element interiorElement = new Element("interior", GeoRSSModule.GML_NS);
                polygonElement.addContent(interiorElement);
                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) {
        Envelope envelope = (Envelope) geometry;
        Element envelopeElement = new Element("Envelope", GeoRSSModule.GML_NS);
        whereElement.addContent(envelopeElement);

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

        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());
    }
}

From source file:org.rometools.feed.module.georss.SimpleGenerator.java

License:Apache License

public void generate(Module module, 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();
    }/* w ww  .j  a  v a2  s .  com*/
    root.addNamespaceDeclaration(GeoRSSModule.SIMPLE_NS);

    GeoRSSModule geoRSSModule = (GeoRSSModule) module;

    AbstractGeometry geometry = geoRSSModule.getGeometry();
    if (geometry instanceof Point) {
        Position pos = ((Point) geometry).getPosition();

        Element pointElement = new Element("point", GeoRSSModule.SIMPLE_NS);
        pointElement.addContent(pos.getLatitude() + " " + pos.getLongitude());
        element.addContent(pointElement);
    } else if (geometry instanceof LineString) {
        PositionList posList = ((LineString) geometry).getPositionList();

        Element lineElement = new Element("line", GeoRSSModule.SIMPLE_NS);

        lineElement.addContent(posListToString(posList));
        element.addContent(lineElement);
    } else if (geometry instanceof Polygon) {
        AbstractRing ring = ((Polygon) geometry).getExterior();
        if (ring instanceof LinearRing) {
            PositionList posList = ((LinearRing) ring).getPositionList();
            Element polygonElement = new Element("polygon", GeoRSSModule.SIMPLE_NS);

            polygonElement.addContent(posListToString(posList));
            element.addContent(polygonElement);
        } else {
            System.err.println("GeoRSS simple format can't handle rings of type: " + ring.getClass().getName());
        }
        if (((Polygon) geometry).getInterior() != null || !((Polygon) geometry).getInterior().isEmpty()) {
            System.err.println("GeoRSS simple format can't handle interior rings (ignored)");
        }
    } else if (geometry instanceof Envelope) {
        Envelope envelope = (Envelope) geometry;
        Element boxElement = new Element("box", GeoRSSModule.SIMPLE_NS);
        boxElement.addContent(envelope.getMinLatitude() + " " + envelope.getMinLongitude() + " "
                + envelope.getMaxLatitude() + " " + envelope.getMaxLongitude());
        element.addContent(boxElement);
    } else {
        System.err.println(
                "GeoRSS simple format can't handle geometries of type: " + geometry.getClass().getName());
    }
}

From source file:org.rometools.feed.module.georss.W3CGeoGenerator.java

License:Apache License

public void generate(Module module, 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();
    }/*from w  w  w  . jav  a 2 s .co m*/
    root.addNamespaceDeclaration(GeoRSSModule.W3CGEO_NS);

    Element pointElement = element;
    if (!isShort) {
        pointElement = new Element("Point", GeoRSSModule.W3CGEO_NS);
        element.addContent(pointElement);
    }

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

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

        Element latElement = new Element("lat", GeoRSSModule.W3CGEO_NS);
        latElement.addContent(String.valueOf(pos.getLatitude()));
        pointElement.addContent(latElement);
        Element lngElement = new Element("long", GeoRSSModule.W3CGEO_NS);
        lngElement.addContent(String.valueOf(pos.getLongitude()));
        pointElement.addContent(lngElement);
    } else {
        System.err.println("W3C Geo format can't handle geometries of type: " + geometry.getClass().getName());
    }
}

From source file:org.rometools.feed.module.itunes.io.ITunesGenerator.java

License:Open Source License

public void generate(Module module, Element element) {
    Element root = element;

    while ((root.getParent() != null) && root.getParent() instanceof Element) {
        root = (Element) root.getParent();
    }//from w  ww  .j a  va2  s  . c  om

    root.addNamespaceDeclaration(NS);

    if (!(module instanceof AbstractITunesObject)) {
        return;
    }

    AbstractITunesObject itunes = (AbstractITunesObject) module;

    if (itunes instanceof FeedInformationImpl) {
        //Do Channel Specific Stuff.
        FeedInformationImpl info = (FeedInformationImpl) itunes;
        Element owner = this.generateSimpleElement("owner", "");
        Element email = this.generateSimpleElement("email", info.getOwnerEmailAddress());
        owner.addContent(email);

        Element name = this.generateSimpleElement("name", info.getOwnerName());
        owner.addContent(name);
        element.addContent(owner);

        if (info.getImage() != null) {
            Element image = this.generateSimpleElement("image", "");
            image.setAttribute("href", info.getImage().toExternalForm());
            element.addContent(image);
        }

        for (Iterator it = info.getCategories().iterator(); it.hasNext();) {
            Category cat = (Category) it.next();
            Element category = this.generateSimpleElement("category", "");
            category.setAttribute("text", cat.getName());

            if (cat.getSubcategory() != null) {
                Element subcat = this.generateSimpleElement("category", "");
                subcat.setAttribute("text", cat.getSubcategory().getName());
                category.addContent(subcat);
            }

            element.addContent(category);
        }
    } else if (itunes instanceof EntryInformationImpl) {
        EntryInformationImpl info = (EntryInformationImpl) itunes;

        if (info.getDuration() != null) {
            element.addContent(this.generateSimpleElement("duration", info.getDuration().toString()));
        }
    }

    if (itunes.getAuthor() != null) {
        element.addContent(this.generateSimpleElement("author", itunes.getAuthor()));
    }

    if (itunes.getBlock()) {
        element.addContent(this.generateSimpleElement("block", ""));
    }

    if (itunes.getExplicit()) {
        element.addContent(this.generateSimpleElement("explicit", "yes"));
    } else {
        element.addContent(this.generateSimpleElement("explicit", "no"));
    }

    if (itunes.getKeywords() != null) {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < itunes.getKeywords().length; i++) {
            if (i != 0) {
                sb.append(", ");
            }

            sb.append(itunes.getKeywords()[i]);
        }

        element.addContent(this.generateSimpleElement("keywords", sb.toString()));
    }

    if (itunes.getSubtitle() != null) {
        element.addContent(this.generateSimpleElement("subtitle", itunes.getSubtitle()));
    }

    if (itunes.getSummary() != null) {
        element.addContent(this.generateSimpleElement("summary", itunes.getSummary()));
    }
}

From source file:org.rometools.feed.module.opensearch.impl.OpenSearchModuleParser.java

License:Apache License

/** Use feed links and/or xml:base attribute to determine baseURI of feed */
private static URL findBaseURI(Element root) {
    URL baseURI = null;//from ww w . j av a  2 s . co m
    List linksList = root.getChildren("link", OS_NS);
    if (linksList != null) {
        for (Iterator links = linksList.iterator(); links.hasNext();) {
            Element link = (Element) links.next();
            if (!root.equals(link.getParent()))
                break;
            String href = link.getAttribute("href").getValue();
            if (link.getAttribute("rel", OS_NS) == null
                    || link.getAttribute("rel", OS_NS).getValue().equals("alternate")) {
                href = resolveURI(null, link, href);
                try {
                    baseURI = new URL(href);
                    break;
                } catch (MalformedURLException e) {
                    System.err.println("Base URI is malformed: " + href);
                }
            }
        }
    }
    return baseURI;
}

From source file:se.miun.itm.input.util.ParamUtil.java

License:Open Source License

/**
 * derive parameter id for a given InPUT element.
 * //from w w  w.  j  ava2s .  c  o m
 * @param element
 * @param name
 * @return
 */
private static String deriveParamId(final Element element, String name) {
    // 1) not null, 2) is Element, 3) not root -> next!
    if (element.getParent() != null && element.getParent() instanceof Element) {
        Element parent = (Element) element.getParent();
        if (!parent.isRootElement())
            name = deriveParamId(parent, parent.getAttributeValue(Q.ID_ATTR) + "." + name);
    }

    return name;
}