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