Search in sources :

Example 51 with Content

use of org.jdom2.Content 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 52 with Content

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

the class MCRDefaultAltoChangeApplier method applyChange.

@Override
public void applyChange(MCRAltoChangeSet changeSet) {
    String derivateID = changeSet.getDerivateID();
    changeSet.getWordChanges().stream().forEach(change -> {
        List<MCRAltoWordChange> list = fileChangeMap.computeIfAbsent(change.getFile(), (k) -> new ArrayList<>());
        list.add(change);
    });
    fileChangeMap.keySet().forEach(file -> {
        LOGGER.info("Open file {} to apply changes!", file);
        MCRPath altoFilePath = MCRPath.getPath(derivateID, file);
        if (!Files.exists(altoFilePath)) {
            LOGGER.warn("Could not find file {} which was referenced by alto change!", altoFilePath);
            throw new MCRException(new IOException("Alto-File " + altoFilePath + " does not exist"));
        }
        Document altoDocument = readALTO(altoFilePath);
        List<MCRAltoWordChange> wordChangesInThisFile = fileChangeMap.get(file);
        wordChangesInThisFile.stream().forEach(wordChange -> {
            String xpath = String.format(Locale.ROOT, "//alto:String[number(@HPOS)=number('%d') and number(@VPOS)=number('%d')]", wordChange.getHpos(), wordChange.getVpos());
            List<Element> wordToChange = XPathFactory.instance().compile(xpath, Filters.element(), null, MCRConstants.ALTO_NAMESPACE).evaluate(altoDocument);
            if (wordToChange.size() != 1) {
                LOGGER.warn("Found {} words to change.", wordToChange.size());
            }
            wordToChange.forEach(word -> {
                word.setAttribute("CONTENT", wordChange.getTo());
                word.setAttribute("WC", "1");
            });
        });
        storeALTO(altoFilePath, altoDocument);
    });
}
Also used : MCRAltoWordChange(org.mycore.viewer.alto.model.MCRAltoWordChange) MCRException(org.mycore.common.MCRException) Element(org.jdom2.Element) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document)

Example 53 with Content

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

the class JSONSectionProviderTest method toJSON.

@Test
public void toJSON() throws Exception {
    Map<String, String> properties = new HashMap<>();
    properties.put("MCR.WCMS2.mycoreTagList", "");
    MCRConfiguration.instance().initialize(properties, true);
    SAXBuilder builder = new SAXBuilder();
    Document doc = builder.build(new File("src/test/resources/navigation/content.xml"));
    MCRWCMSDefaultSectionProvider prov = new MCRWCMSDefaultSectionProvider();
    JsonArray sectionArray = prov.toJSON(doc.getRootElement());
    assertEquals(2, sectionArray.size());
    // test section one
    JsonObject section1 = (JsonObject) sectionArray.get(0);
    assertEquals("Title one", section1.getAsJsonPrimitive("title").getAsString());
    assertEquals("de", section1.getAsJsonPrimitive("lang").getAsString());
    assertEquals("<div><p>Content one</p><br /></div>", section1.getAsJsonPrimitive("data").getAsString());
    // test section two
    JsonObject section2 = (JsonObject) sectionArray.get(1);
    assertEquals("Title two", section2.getAsJsonPrimitive("title").getAsString());
    assertEquals("en", section2.getAsJsonPrimitive("lang").getAsString());
    assertEquals("Content two", section2.getAsJsonPrimitive("data").getAsString());
}
Also used : JsonArray(com.google.gson.JsonArray) SAXBuilder(org.jdom2.input.SAXBuilder) MCRWCMSDefaultSectionProvider(org.mycore.wcms2.navigation.MCRWCMSDefaultSectionProvider) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) Document(org.jdom2.Document) File(java.io.File) Test(org.junit.Test)

Example 54 with Content

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

the class NavigationTest method toXML.

@Test
public void toXML() throws Exception {
    JAXBContext jc = JAXBContext.newInstance(MCRNavigation.class);
    Marshaller m = jc.createMarshaller();
    JDOMResult JDOMResult = new JDOMResult();
    m.marshal(this.navigation, JDOMResult);
    Element navigationElement = JDOMResult.getDocument().getRootElement();
    XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
    out.output(navigationElement, System.out);
    // test attributes
    assertEquals("template_mysample", navigationElement.getAttributeValue("template"));
    assertEquals("/content", navigationElement.getAttributeValue("dir"));
    assertEquals("History Title", navigationElement.getAttributeValue("historyTitle"));
    assertEquals("/content/below/index.xml", navigationElement.getAttributeValue("hrefStartingPage"));
    assertEquals("Main Title", navigationElement.getAttributeValue("mainTitle"));
    // test children
    assertEquals(2, navigationElement.getChildren("menu").size());
    assertEquals(1, navigationElement.getChildren("insert").size());
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) Marshaller(javax.xml.bind.Marshaller) JDOMResult(org.jdom2.transform.JDOMResult) JsonElement(com.google.gson.JsonElement) Element(org.jdom2.Element) JAXBContext(javax.xml.bind.JAXBContext) Test(org.junit.Test)

Example 55 with Content

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

the class MCRSwapElements method swap.

public static MCRChangeData swap(Element parent, int posA, int posB) {
    Content a = parent.getContent().get(posA);
    Content b = parent.getContent().get(posB);
    return swap(parent, posA, a, posB, b);
}
Also used : Content(org.jdom2.Content)

Aggregations

Element (org.jdom2.Element)77 Document (org.jdom2.Document)27 IOException (java.io.IOException)18 JDOMException (org.jdom2.JDOMException)16 File (java.io.File)11 MCRException (org.mycore.common.MCRException)11 Content (org.jdom2.Content)10 MCRContent (org.mycore.common.content.MCRContent)10 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)10 XMLOutputter (org.jdom2.output.XMLOutputter)9 SAXBuilder (org.jdom2.input.SAXBuilder)8 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 Attribute (org.jdom2.Attribute)6 MCRPath (org.mycore.datamodel.niofs.MCRPath)6 SAXException (org.xml.sax.SAXException)6 JsonElement (com.google.gson.JsonElement)5 Color (java.awt.Color)4 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)4 MCRObject (org.mycore.datamodel.metadata.MCRObject)4