Example usage for org.jdom2 Parent getParent

List of usage examples for org.jdom2 Parent getParent

Introduction

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

Prototype

Parent getParent();

Source Link

Document

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

Usage

From source file:com.rometools.modules.opensearch.impl.OpenSearchModuleParser.java

License:Apache License

/**
 * Use xml:base attributes at feed and entry level to resolve relative links
 *///from  ww  w  . jav a  2  s . c  om
private static String resolveURI(final URL baseURI, final Parent parent, String url) {
    url = url.equals(".") || url.equals("./") ? "" : url;
    if (isRelativeURI(url) && parent != null && parent instanceof Element) {
        final Attribute baseAtt = ((Element) parent).getAttribute("base", Namespace.XML_NAMESPACE);
        String xmlBase = baseAtt == null ? "" : baseAtt.getValue();
        if (!isRelativeURI(xmlBase) && !xmlBase.endsWith("/")) {
            xmlBase = xmlBase.substring(0, xmlBase.lastIndexOf("/") + 1);
        }
        return resolveURI(baseURI, parent.getParent(), xmlBase + url);
    } else if (isRelativeURI(url) && parent == null) {
        return baseURI + url;
    } else if (baseURI != null && url.startsWith("/")) {
        String hostURI = baseURI.getProtocol() + "://" + baseURI.getHost();
        if (baseURI.getPort() != baseURI.getDefaultPort()) {
            hostURI = hostURI + ":" + baseURI.getPort();
        }
        return hostURI + url;
    }
    return url;
}

From source file:com.rometools.rome.io.impl.Atom10Parser.java

License:Open Source License

/**
 * Resolve URI via base URL and parent element. Resolve URI based considering xml:base and
 * baseURI.//  w  w w  .  jav a  2  s  .com
 *
 * @param baseURI Base URI used to fetch the XML document
 * @param parent Parent element from which to consider xml:base
 * @param url URL to be resolved
 */
public static String resolveURI(final String baseURI, final Parent parent, String url) {

    if (!resolveURIs) {
        return url;
    }

    if (isRelativeURI(url)) {

        if (".".equals(url) || "./".equals(url)) {
            url = "";
        }

        if (url.startsWith("/") && baseURI != null) {
            String base = null;
            final int slashslash = baseURI.indexOf("//");
            final int nextslash = baseURI.indexOf("/", slashslash + 2);
            if (nextslash != -1) {
                base = baseURI.substring(0, nextslash);
            }
            return formURI(base, url);
        }

        // Relative URI with parent
        if (parent != null && parent instanceof Element) {

            // Do we have an xml:base?
            String xmlbase = ((Element) parent).getAttributeValue("base", Namespace.XML_NAMESPACE);
            if (xmlbase != null && xmlbase.trim().length() > 0) {
                if (isAbsoluteURI(xmlbase)) {
                    // Absolute xml:base, so form URI right now
                    if (url.startsWith("/")) {
                        // Host relative URI
                        final int slashslash = xmlbase.indexOf("//");
                        final int nextslash = xmlbase.indexOf("/", slashslash + 2);
                        if (nextslash != -1) {
                            xmlbase = xmlbase.substring(0, nextslash);
                        }
                        return formURI(xmlbase, url);
                    }
                    if (!xmlbase.endsWith("/")) {
                        // Base URI is filename, strip it off
                        xmlbase = xmlbase.substring(0, xmlbase.lastIndexOf("/"));
                    }
                    return formURI(xmlbase, url);
                } else {
                    // Relative xml:base, so walk up tree
                    return resolveURI(baseURI, parent.getParent(),
                            stripTrailingSlash(xmlbase) + "/" + stripStartingSlash(url));
                }
            }
            // No xml:base so walk up tree
            return resolveURI(baseURI, parent.getParent(), url);

            // Relative URI with no parent (i.e. top of tree), so form URI
            // right now
        } else if (parent == null || parent instanceof Document) {
            return formURI(baseURI, url);
        }
    }

    return url;

}

From source file:com.sun.syndication.io.impl.Atom10Parser.java

License:Open Source License

/**
 * Resolve URI via base URL and parent element.
 * Resolve URI based considering xml:base and baseURI.
 * @param baseURI Base URI used to fetch the XML document
 * @param parent  Parent element from which to consider xml:base
 * @param url     URL to be resolved/*  w  w  w .j  a va  2s  .c o m*/
 */
public static String resolveURI(String baseURI, Parent parent, String url) {
    if (!resolveURIs) {
        return url;
    }
    if (isRelativeURI(url)) {
        url = (!".".equals(url) && !"./".equals(url)) ? url : "";

        if (url.startsWith("/") && baseURI != null) {
            String base = null;
            int slashslash = baseURI.indexOf("//");
            int nextslash = baseURI.indexOf("/", slashslash + 2);
            if (nextslash != -1)
                base = baseURI.substring(0, nextslash);
            return formURI(base, url);
        }

        // Relative URI with parent
        if (parent != null && parent instanceof Element) {

            // Do we have an xml:base?         
            String xmlbase = ((Element) parent).getAttributeValue("base", Namespace.XML_NAMESPACE);
            if (xmlbase != null && xmlbase.trim().length() > 0) {
                if (isAbsoluteURI(xmlbase)) {
                    // Absolute xml:base, so form URI right now 
                    if (url.startsWith("/")) {
                        // Host relative URI
                        int slashslash = xmlbase.indexOf("//");
                        int nextslash = xmlbase.indexOf("/", slashslash + 2);
                        if (nextslash != -1)
                            xmlbase = xmlbase.substring(0, nextslash);
                        return formURI(xmlbase, url);
                    }
                    if (!xmlbase.endsWith("/")) {
                        // Base URI is filename, strip it off 
                        xmlbase = xmlbase.substring(0, xmlbase.lastIndexOf("/"));
                    }
                    return formURI(xmlbase, url);
                } else {
                    // Relative xml:base, so walk up tree
                    return resolveURI(baseURI, parent.getParent(),
                            stripTrailingSlash(xmlbase) + "/" + stripStartingSlash(url));
                }
            }
            // No xml:base so walk up tree
            return resolveURI(baseURI, parent.getParent(), url);

            // Relative URI with no parent (i.e. top of tree), so form URI right now
        } else if (parent == null || parent instanceof Document) {
            return formURI(baseURI, url);
        }
    }
    return url;
}

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

License:Apache License

/** Use xml:base attributes at feed and entry level to resolve relative links */
private static String resolveURI(URL baseURI, Parent parent, String url) {
    url = (url.equals(".") || url.equals("./")) ? "" : url;
    if (isRelativeURI(url) && parent != null && parent instanceof Element) {
        Attribute baseAtt = ((Element) parent).getAttribute("base", Namespace.XML_NAMESPACE);
        String xmlBase = (baseAtt == null) ? "" : baseAtt.getValue();
        if (!isRelativeURI(xmlBase) && !xmlBase.endsWith("/")) {
            xmlBase = xmlBase.substring(0, xmlBase.lastIndexOf("/") + 1);
        }//from w  ww .j a v a2 s .  com
        return resolveURI(baseURI, parent.getParent(), xmlBase + url);
    } else if (isRelativeURI(url) && parent == null) {
        return baseURI + url;
    } else if (baseURI != null && url.startsWith("/")) {
        String hostURI = baseURI.getProtocol() + "://" + baseURI.getHost();
        if (baseURI.getPort() != baseURI.getDefaultPort()) {
            hostURI = hostURI + ":" + baseURI.getPort();
        }
        return hostURI + url;
    }
    return url;
}