Search in sources :

Example 96 with JDOMException

use of org.jdom2.JDOMException in project mycore by MyCoRe-Org.

the class MCRIdentifierXSLUtils method getPIServiceInformation.

/**
 * Gets all available services which are configured.
 * e.g.
 *   <ul>
 *     <li>&lt;service id="service1" inscribed="false" permission="true" type="urn" /&gt;</li>
 *     <li>&lt;service id="service2" inscribed="true" permission="false" type="doi" /&gt;</li>
 *   </ul>
 *
 * @param objectID the object
 * @return a Nodelist
 * @throws JDOMException
 */
public static NodeList getPIServiceInformation(String objectID) throws JDOMException {
    Element e = new Element("list");
    MCRBase obj = MCRMetadataManager.retrieve(MCRObjectID.getInstance(objectID));
    MCRConfiguration.instance().getPropertiesMap("MCR.PI.Registration.").keySet().stream().map(s -> s.substring("MCR.PI.Registration.".length())).filter(id -> !id.contains(".")).map((serviceID) -> MCRPIRegistrationServiceManager.getInstance().getRegistrationService(serviceID)).map((rs -> {
        Element service = new Element("service");
        service.setAttribute("id", rs.getRegistrationServiceID());
        // Check if the inscriber of this service can read a PI
        try {
            if (rs.getMetadataManager().getIdentifier(obj, "").isPresent()) {
                service.setAttribute("inscribed", "true");
            } else {
                service.setAttribute("inscribed", "false");
            }
        } catch (MCRPersistentIdentifierException e1) {
            LOGGER.warn("Error happened while try to read PI from object: {}", objectID, e1);
            service.setAttribute("inscribed", "false");
        }
        // rights
        String permission = "register-" + rs.getRegistrationServiceID();
        Boolean canRegister = MCRAccessManager.checkPermission(objectID, "writedb") && MCRAccessManager.checkPermission(obj.getId(), permission);
        service.setAttribute("permission", canRegister.toString().toLowerCase(Locale.ROOT));
        // add the type
        service.setAttribute("type", rs.getType());
        return service;
    })).forEach(e::addContent);
    return new DOMOutputter().output(e).getElementsByTagName("service");
}
Also used : MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) NodeList(org.w3c.dom.NodeList) DOMOutputter(org.jdom2.output.DOMOutputter) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRConfiguration(org.mycore.common.config.MCRConfiguration) MCRPersistentIdentifier(org.mycore.pi.MCRPersistentIdentifier) MCRAccessManager(org.mycore.access.MCRAccessManager) MCRPIRegistrationService(org.mycore.pi.MCRPIRegistrationService) Logger(org.apache.logging.log4j.Logger) JDOMException(org.jdom2.JDOMException) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) Locale(java.util.Locale) MCRPersistentIdentifierManager(org.mycore.pi.MCRPersistentIdentifierManager) LogManager(org.apache.logging.log4j.LogManager) MCRPIRegistrationServiceManager(org.mycore.pi.MCRPIRegistrationServiceManager) Element(org.jdom2.Element) DOMOutputter(org.jdom2.output.DOMOutputter) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRPIRegistrationService(org.mycore.pi.MCRPIRegistrationService) Locale(java.util.Locale) Element(org.jdom2.Element) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException)

Example 97 with JDOMException

use of org.jdom2.JDOMException in project mycore by MyCoRe-Org.

the class MCRWCMSContentManager method getContent.

/**
 * Return a json object with the content of a MyCoRe webpage.
 * <p>
 * {
 *  type: "content",
 *  content: @see {@link MCRWCMSDefaultSectionProvider}
 * }
 * </p>
 * <p>
 * If an error occur (e.g. file not exist) the returning json
 * looks like:<br>
 * {
 *  type: "error",
 *  errorType: "invalidFile"
 *  webpageId: "myfolder/webpage1.xml"
 * }
 * </p>
 *
 * @param webpageId id of the webpage
 * @return json object
 * @see ErrorType
 */
public JsonObject getContent(String webpageId) throws IOException, JDOMException, SAXException {
    boolean isXML = webpageId.endsWith(".xml");
    URL resourceURL = null;
    try {
        resourceURL = MCRWebPagesSynchronizer.getURL(webpageId);
    } catch (MalformedURLException e) {
        throwError(ErrorType.invalidDirectory, webpageId);
    }
    // file is not in web application directory
    if (!isXML) {
        throwError(ErrorType.invalidFile, webpageId);
    }
    Document doc = null;
    if (resourceURL == null) {
        MyCoReWebPageProvider wpp = new MyCoReWebPageProvider();
        wpp.addSection("neuer Eintrag", new Element("p").setText("TODO"), "de");
        doc = wpp.getXML();
    } else {
        doc = new MCRURLContent(resourceURL).asXML();
    }
    Element rootElement = doc.getRootElement();
    if (!rootElement.getName().equals("MyCoReWebPage")) {
        throwError(ErrorType.notMyCoReWebPage, webpageId);
    }
    // return content
    return getContent(rootElement);
}
Also used : MalformedURLException(java.net.MalformedURLException) MyCoReWebPageProvider(org.mycore.tools.MyCoReWebPageProvider) JsonElement(com.google.gson.JsonElement) Element(org.jdom2.Element) MCRURLContent(org.mycore.common.content.MCRURLContent) Document(org.jdom2.Document) URL(java.net.URL)

Example 98 with JDOMException

use of org.jdom2.JDOMException in project mycore by MyCoRe-Org.

the class MCRViewerResource method buildResponseDocument.

/**
 * Builds the jdom configuration response document.
 *
 * @param config the mycore configuration object
 * @return jdom configuration object
 */
private static Document buildResponseDocument(MCRViewerConfiguration config) throws JDOMException, IOException, SAXException, JAXBException {
    String configJson = config.toJSON();
    Element startIviewClientElement = new Element("IViewConfig");
    Element configElement = new Element(JSON_CONFIG_ELEMENT_NAME);
    startIviewClientElement.addContent(configElement);
    startIviewClientElement.addContent(config.toXML().asXML().getRootElement().detach());
    configElement.addContent(configJson);
    return new Document(startIviewClientElement);
}
Also used : Element(org.jdom2.Element) Document(org.jdom2.Document)

Example 99 with JDOMException

use of org.jdom2.JDOMException in project mycore by MyCoRe-Org.

the class MCRWCMSUtil method convertToOldFormat.

/**
 * Converts the navigation.xml to the old format.
 */
private static byte[] convertToOldFormat(byte[] xml) throws JDOMException, IOException {
    SAXBuilder builder = new SAXBuilder();
    Document doc = builder.build(new ByteArrayInputStream(xml));
    Element rootElement = doc.getRootElement();
    rootElement.setAttribute("href", rootElement.getName());
    List<org.jdom2.Element> children = rootElement.getChildren();
    for (Element menu : children) {
        String id = menu.getAttributeValue("id");
        menu.setName(id);
        menu.setAttribute("href", id);
        menu.removeAttribute("id");
    }
    XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
    ByteArrayOutputStream bout = new ByteArrayOutputStream(xml.length);
    out.output(doc, bout);
    return bout.toByteArray();
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) SAXBuilder(org.jdom2.input.SAXBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) Element(org.jdom2.Element) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.jdom2.Document)

Example 100 with JDOMException

use of org.jdom2.JDOMException in project mycore by MyCoRe-Org.

the class MCRXEditorTransformer method createEmptyDocumentFromXPath.

private void createEmptyDocumentFromXPath(String xPath) throws JaxenException, JDOMException {
    Element root = createRootElement(xPath);
    editorSession.setEditedXML(new Document(root));
    editorSession.setBreakpoint("Starting with empty XML document");
}
Also used : Element(org.jdom2.Element) Document(org.jdom2.Document)

Aggregations

Element (org.jdom2.Element)157 Document (org.jdom2.Document)116 JDOMException (org.jdom2.JDOMException)91 IOException (java.io.IOException)76 SAXBuilder (org.jdom2.input.SAXBuilder)70 Test (org.junit.Test)36 File (java.io.File)33 ArrayList (java.util.ArrayList)22 InputStream (java.io.InputStream)17 StringReader (java.io.StringReader)16 Attribute (org.jdom2.Attribute)16 MCRNodeBuilder (org.mycore.common.xml.MCRNodeBuilder)14 SAXException (org.xml.sax.SAXException)14 HashMap (java.util.HashMap)13 XMLOutputter (org.jdom2.output.XMLOutputter)13 URL (java.net.URL)12 XmlFile (jmri.jmrit.XmlFile)12 List (java.util.List)11 MCRObject (org.mycore.datamodel.metadata.MCRObject)11 MCRException (org.mycore.common.MCRException)10