Example usage for org.jdom2 Element getDocument

List of usage examples for org.jdom2 Element getDocument

Introduction

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

Prototype

public Document getDocument() 

Source Link

Document

Return this child's owning document or null if the branch containing this child is currently not attached to a document.

Usage

From source file:org.mycore.common.content.MCRSourceContent.java

License:Open Source License

public MCRSourceContent(Source source) {
    if (source == null) {
        throw new NullPointerException("Source cannot be null");
    }/*from www. j a va  2  s  .  c  o m*/
    this.source = source;
    MCRContent baseContent = null;
    if (source instanceof JDOMSource) {
        JDOMSource src = (JDOMSource) source;
        Document xml = src.getDocument();
        if (xml != null) {
            baseContent = new MCRJDOMContent(xml);
        } else {
            for (Object node : src.getNodes()) {
                if (node instanceof Element) {
                    Element element = (Element) node;
                    Document doc = element.getDocument();
                    if (doc == null) {
                        baseContent = new MCRJDOMContent(element);
                    } else {
                        if (doc.getRootElement() == element) {
                            baseContent = new MCRJDOMContent(doc);
                        } else {
                            baseContent = new MCRJDOMContent((Element) element.clone());
                        }
                    }
                    break;
                } else if (node instanceof Document) {
                    baseContent = new MCRJDOMContent((Document) node);
                    break;
                }
            }
        }
    } else if (source instanceof SAXSource) {
        SAXSource src = (SAXSource) source;
        baseContent = new MCRSAXContent(src.getXMLReader(), src.getInputSource());
    } else if (source instanceof DOMSource) {
        Node node = ((DOMSource) source).getNode();
        baseContent = new MCRDOMContent(node.getOwnerDocument());
    } else if (source instanceof StreamSource) {
        InputStream inputStream = ((StreamSource) source).getInputStream();
        if (inputStream != null) {
            baseContent = new MCRStreamContent(inputStream);
        } else {
            try {
                URL url = new URL(source.getSystemId());
                baseContent = new MCRURLContent(url);
            } catch (MalformedURLException e) {
                throw new MCRException(
                        "Could not create instance of MCRURLContent for SYSTEMID: " + source.getSystemId(), e);
            }
        }
    }
    if (baseContent == null) {
        throw new MCRException("Could not get MCRContent from " + source.getClass().getCanonicalName()
                + ", systemId:" + source.getSystemId());
    }
    baseContent.setSystemId(getSystemId());
    this.setBaseContent(baseContent);
}

From source file:org.mycore.datamodel.metadata.MCRMetaElement.java

License:Open Source License

/**
 * This methode read the XML input stream part from a DOM part for the
 * metadata of the document./*w w  w  . j  a  v  a2s.c  om*/
 * 
 * @param element
 *            a relevant JDOM element for the metadata
 * @exception MCRException
 *                if the class can't loaded
 */
@SuppressWarnings("unchecked")
public final void setFromDOM(org.jdom2.Element element) throws MCRException {

    String fullname;
    Class<? extends MCRMetaInterface> forName;
    try {
        String classname = element.getAttributeValue("class");
        if (classname == null) {
            throw new MCRException("Missing required class attribute in element " + element.getName()
                    + " of object " + element.getDocument().getRootElement().getAttributeValue("ID"));
        }
        fullname = META_PACKAGE_NAME + classname;
        forName = (Class<? extends MCRMetaInterface>) Class.forName(fullname);
        setClass(forName);
    } catch (ClassNotFoundException e) {
        throw new MCRException(e);
    }
    tag = element.getName();
    String heritable = element.getAttributeValue("heritable");
    if (heritable != null)
        setHeritable(Boolean.valueOf(heritable));

    String notInherit = element.getAttributeValue("notinherit");
    if (notInherit != null)
        setNotInherit(Boolean.valueOf(notInherit));

    List<Element> element_list = element.getChildren();
    for (Element anElement_list : element_list) {
        Element subtag = (Element) anElement_list;
        MCRMetaInterface obj;

        try {
            obj = forName.newInstance();
            obj.setFromDOM(subtag);
        } catch (IllegalAccessException e) {
            throw new MCRException(fullname + " IllegalAccessException");
        } catch (InstantiationException e) {
            throw new MCRException(fullname + " InstantiationException");
        }

        list.add(obj);
    }
}

From source file:org.mycore.restapi.v1.MCRRestAPIClassifications.java

License:Open Source License

/**
 * Output JSON/*from  w  w  w.  j  a v a  2  s  .com*/
 * @param eRoot - the category element
 * @param lang - the language to be filtered for or null if all languages should be displayed
 * @param style - the style
 * @return a string representation of a JSON object
 * @throws IOException
 */
private String writeJSON(Element eRoot, String lang, String style) throws IOException {
    StringWriter sw = new StringWriter();
    JsonWriter writer = new JsonWriter(sw);
    writer.setIndent("  ");
    if (style.contains("checkboxtree")) {
        if (lang == null) {
            lang = "de";
        }
        writer.beginObject();
        writer.name("identifier").value(eRoot.getAttributeValue("ID"));
        for (Element eLabel : eRoot.getChildren("label")) {
            if (lang.equals(eLabel.getAttributeValue("lang", Namespace.XML_NAMESPACE))) {
                writer.name("label").value(eLabel.getAttributeValue("text"));
            }
        }
        writer.name("items");

        writeChildrenAsJSONCBTree(eRoot = eRoot.getChild("categories"), writer, lang,
                style.contains("checked"));
        writer.endObject();
    } else if (style.contains("jstree")) {
        if (lang == null) {
            lang = "de";
        }
        writeChildrenAsJSONJSTree(eRoot = eRoot.getChild("categories"), writer, lang, style.contains("opened"),
                style.contains("disabled"), style.contains("selected"));
    } else {
        writer.beginObject(); // {
        writer.name("ID").value(eRoot.getAttributeValue("ID"));
        writer.name("label");
        writer.beginArray();
        for (Element eLabel : eRoot.getChildren("label")) {
            if (lang == null || lang.equals(eLabel.getAttributeValue("lang", Namespace.XML_NAMESPACE))) {
                writer.beginObject();
                writer.name("lang").value(eLabel.getAttributeValue("lang", Namespace.XML_NAMESPACE));
                writer.name("text").value(eLabel.getAttributeValue("text"));
                if (eLabel.getAttributeValue("description") != null) {
                    writer.name("description").value(eLabel.getAttributeValue("description"));
                }
                writer.endObject();
            }
        }
        writer.endArray();

        if (eRoot.equals(eRoot.getDocument().getRootElement())) {
            writeChildrenAsJSON(eRoot.getChild("categories"), writer, lang);
        } else {
            writeChildrenAsJSON(eRoot, writer, lang);
        }

        writer.endObject();
    }
    writer.close();
    return sw.toString();
}

From source file:org.mycore.user2.MCRRealmFactory.java

License:Open Source License

/**
 * //from w  w w.java2s. c  o  m
 */
private static void loadRealms() {
    Element root;
    try {
        root = getRealms().getRootElement();
    } catch (SAXException | JDOMException | TransformerException | IOException e) {
        throw new MCRException("Could not load realms from URI: " + realmsURI);
    }
    String localRealmID = root.getAttributeValue("local");
    /** Map of defined realms, key is the ID of the realm */
    HashMap<String, MCRRealm> realmsMap = new HashMap<String, MCRRealm>();

    HashMap<String, MCRUserAttributeMapper> attributeMapper = new HashMap<String, MCRUserAttributeMapper>();

    /** List of defined realms */
    List<MCRRealm> realmsList = new ArrayList<MCRRealm>();

    List<Element> realms = (List<Element>) (root.getChildren("realm"));
    for (Element child : realms) {
        String id = child.getAttributeValue("id");
        MCRRealm realm = new MCRRealm(id);

        List<Element> labels = (List<Element>) (child.getChildren("label"));
        for (Element label : labels) {
            String text = label.getTextTrim();
            String lang = label.getAttributeValue("lang", Namespace.XML_NAMESPACE);
            realm.setLabel(lang, text);
        }

        realm.setPasswordChangeURL(child.getChildTextTrim("passwordChangeURL"));
        Element login = child.getChild("login");
        if (login != null) {
            realm.setLoginURL(login.getAttributeValue("url"));
            realm.setRedirectParameter(login.getAttributeValue("redirectParameter"));
            realm.setRealmParameter(login.getAttributeValue("realmParameter"));
        }
        Element createElement = child.getChild("create");
        if (createElement != null) {
            realm.setCreateURL(createElement.getAttributeValue("url"));
        }

        attributeMapper.put(id, MCRUserAttributeMapper.instance(child));

        realmsMap.put(id, realm);
        realmsList.add(realm);
        if (localRealmID.equals(id)) {
            localRealm = realm;
        }
    }
    MCRRealmFactory.realmsDocument = root.getDocument();
    MCRRealmFactory.realmsMap = realmsMap;
    MCRRealmFactory.realmsList = realmsList;
    MCRRealmFactory.attributeMapper = attributeMapper;
}

From source file:org.rometools.feed.module.sle.io.ItemParser.java

License:Apache License

/**
 * Parses the XML node (JDOM element) extracting module information.
 * <p>/*from  w w w . j  a v  a  2  s . c  o  m*/
 *
 * @param element the XML node (JDOM element) to extract module information from.
 * @return a module instance, <b>null</b> if the element did not have module information.
 */
public Module parse(Element element) {
    SleEntryImpl sle = new SleEntryImpl();
    ArrayList values = new ArrayList();
    List groups = element.getChildren("group", ModuleParser.TEMP);

    for (int i = 0; (groups != null) && (i < groups.size()); i++) {
        Element group = (Element) groups.get(i);
        StringValue value = new StringValue();
        value.setElement(group.getAttributeValue("element"));
        value.setLabel(group.getAttributeValue("label"));
        value.setValue(group.getAttributeValue("value"));
        if (group.getAttributeValue("ns") != null)
            value.setNamespace(Namespace.getNamespace(group.getAttributeValue("ns")));
        else {
            value.setNamespace(element.getDocument().getRootElement().getNamespace());
        }
        values.add(value);
        element.removeContent(group);
    }

    sle.setGroupValues((EntryValue[]) values.toArray(new EntryValue[values.size()]));
    values = (values.size() == 0) ? values : new ArrayList();

    List sorts = new ArrayList(element.getChildren("sort", ModuleParser.TEMP));
    //System.out.println("]]] sorts on element"+sorts.size());
    for (int i = 0; (sorts != null) && (i < sorts.size()); i++) {
        Element sort = (Element) sorts.get(i);
        String dataType = sort.getAttributeValue("data-type");
        //System.out.println("Doing datatype "+dataType +" :: "+sorts.size());
        if ((dataType == null) || dataType.equals(Sort.TEXT_TYPE)) {
            StringValue value = new StringValue();
            value.setElement(sort.getAttributeValue("element"));
            value.setLabel(sort.getAttributeValue("label"));
            value.setValue(sort.getAttributeValue("value"));
            if (sort.getAttributeValue("ns") != null)
                value.setNamespace(Namespace.getNamespace(sort.getAttributeValue("ns")));
            else
                value.setNamespace(element.getDocument().getRootElement().getNamespace());
            values.add(value);

            element.removeContent(sort);

        } else if (dataType.equals(Sort.DATE_TYPE)) {
            DateValue value = new DateValue();
            value.setElement(sort.getAttributeValue("element"));
            value.setLabel(sort.getAttributeValue("label"));
            if (sort.getAttributeValue("ns") != null)
                value.setNamespace(Namespace.getNamespace(sort.getAttributeValue("ns")));
            else
                value.setNamespace(element.getDocument().getRootElement().getNamespace());
            Date dateValue = null;

            try {
                dateValue = DateParser.parseRFC822(sort.getAttributeValue("value"));
                dateValue = (dateValue == null) ? DateParser.parseW3CDateTime(sort.getAttributeValue("value"))
                        : dateValue;
            } catch (Exception e) {
                ; // ignore parse exceptions
            }

            value.setValue(dateValue);
            values.add(value);
            element.removeContent(sort);
        } else if (dataType.equals(Sort.NUMBER_TYPE)) {
            NumberValue value = new NumberValue();
            value.setElement(sort.getAttributeValue("element"));
            value.setLabel(sort.getAttributeValue("label"));
            if (sort.getAttributeValue("ns") != null)
                value.setNamespace(Namespace.getNamespace(sort.getAttributeValue("ns")));
            else
                value.setNamespace(element.getDocument().getRootElement().getNamespace());

            try {
                value.setValue(new BigDecimal(sort.getAttributeValue("value")));
            } catch (NumberFormatException nfe) {
                ; // ignore
                values.add(value);
                element.removeContent(sort);
            }
        } else {
            throw new RuntimeException("Unknown datatype");
        }
    }
    //System.out.println("Values created "+values.size()+" from sorts" +sorts.size());
    sle.setSortValues((EntryValue[]) values.toArray(new EntryValue[values.size()]));

    return sle;
}

From source file:org.yawlfoundation.yawl.resourcing.util.DataSchemaBuilder.java

License:Open Source License

/**
 * Constructs the expanded data schema, using the user-defined-types map passed
 * in via the constructor.// w  ww.  j  a  va  2 s .  c o m
 * @param rootName the name of the root element (task or root net name)
 * @param parameters the List of parameters to build the schema for
 * @return the constructed schema (as a string)
 */
public String buildSchema(String rootName, List<? extends YVariable> parameters) {
    Namespace defNS = getDefaultNamespace();

    // create a new schema doc preamble (down to first sequence element)
    Element sequence = createPreamble(rootName, defNS);

    // for each param build an appropriate element
    for (YVariable param : parameters) {
        sequence.addContent(createParamElement(param, defNS));
    }

    return completeSchema(sequence.getDocument());
}

From source file:org.yawlfoundation.yawl.resourcing.util.DataSchemaBuilder.java

License:Open Source License

/**
 * Constructs a data schema for a single variable name and data type
 * @param rootName the name to give to the root element
 * @param varName the name to give to the data element
 * @param dataType the datatype for the data element
 * @return a schema for the datatype that variables of the name supplied can be
 * validated against//  w w  w. j a v a  2  s  .  c  o  m
 */
public String buildSchema(String rootName, String varName, String dataType) {
    Namespace defNS = getDefaultNamespace();

    // create a new schema doc preamble (down to first sequence element)
    Element sequence = createPreamble(rootName, defNS);

    // build an appropriate element for the data type
    sequence.addContent(createDataTypeElement(varName, dataType, defNS));

    return completeSchema(sequence.getDocument());
}

From source file:password.pwm.config.stored.StoredConfigurationImpl.java

License:Open Source License

private static void updateMetaData(final Element settingElement, final UserIdentity userIdentity) {
    final Element settingsElement = settingElement.getDocument().getRootElement()
            .getChild(XML_ELEMENT_SETTINGS);
    settingElement.setAttribute(XML_ATTRIBUTE_MODIFY_TIME, JavaHelper.toIsoDate(Instant.now()));
    settingsElement.setAttribute(XML_ATTRIBUTE_MODIFY_TIME, JavaHelper.toIsoDate(Instant.now()));
    settingElement.removeAttribute(XML_ATTRIBUTE_MODIFY_USER);
    settingsElement.removeAttribute(XML_ATTRIBUTE_MODIFY_USER);
    if (userIdentity != null) {
        settingElement.setAttribute(XML_ATTRIBUTE_MODIFY_USER, userIdentity.toDelimitedKey());
        settingsElement.setAttribute(XML_ATTRIBUTE_MODIFY_USER, userIdentity.toDelimitedKey());
    }// w w w .  j  ava2 s . c o m
}

From source file:password.pwm.config.StoredConfiguration.java

License:Open Source License

private static void updateMetaData(final Element settingElement, final UserIdentity userIdentity) {
    final Element settingsElement = settingElement.getDocument().getRootElement()
            .getChild(XML_ELEMENT_SETTINGS);
    settingElement.setAttribute(XML_ATTRIBUTE_MODIFY_TIME,
            PwmConstants.DEFAULT_DATETIME_FORMAT.format(new Date()));
    settingsElement.setAttribute(XML_ATTRIBUTE_MODIFY_TIME,
            PwmConstants.DEFAULT_DATETIME_FORMAT.format(new Date()));
    settingElement.removeAttribute(XML_ATTRIBUTE_MODIFY_USER);
    settingsElement.removeAttribute(XML_ATTRIBUTE_MODIFY_USER);
    if (userIdentity != null) {
        settingElement.setAttribute(XML_ATTRIBUTE_MODIFY_USER, userIdentity.toDelimitedKey());
        settingsElement.setAttribute(XML_ATTRIBUTE_MODIFY_USER, userIdentity.toDelimitedKey());
    }//from  w  w  w. j  a v a2 s  . co m
}

From source file:se.miun.itm.input.export.PropertiesExporter.java

License:Open Source License

private void initProperties(Properties prop, Element parent) {
    String spaceID = parent.getDocument().getRootElement().getAttributeValue(Q.REF_ATTR);
    ParamStore store = ParamStore.getInstance(spaceID);
    initProperties(prop, store, parent);
}