Search in sources :

Example 76 with Content

use of org.jdom2.Content in project jspwiki by apache.

the class RSS10Feed method addItemList.

private void addItemList(Element root) {
    SimpleDateFormat iso8601fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    WikiEngine engine = m_wikiContext.getEngine();
    for (Entry entry : m_entries) {
        String url = entry.getURL();
        Element item = new Element("item", NS_XMNLS);
        item.setAttribute("about", url, NS_RDF);
        item.addContent(new Element("title", NS_XMNLS).addContent(entry.getTitle()));
        item.addContent(new Element("link", NS_XMNLS).addContent(url));
        Element content = new Element("description", NS_XMNLS);
        // TODO: Add a size limiter here
        content.addContent(entry.getContent());
        item.addContent(content);
        WikiPage p = entry.getPage();
        if (p.getVersion() != -1) {
            item.addContent(new Element("version", NS_WIKI).addContent(Integer.toString(p.getVersion())));
        }
        if (p.getVersion() > 1) {
            item.addContent(new Element("diff", NS_WIKI).addContent(engine.getURL(WikiContext.DIFF, p.getName(), "r1=-1", true)));
        }
        // 
        // Modification date.
        Calendar cal = Calendar.getInstance();
        cal.setTime(p.getLastModified());
        cal.add(Calendar.MILLISECOND, -(cal.get(Calendar.ZONE_OFFSET) + (cal.getTimeZone().inDaylightTime(p.getLastModified()) ? cal.get(Calendar.DST_OFFSET) : 0)));
        item.addContent(new Element("date", NS_DC).addContent(iso8601fmt.format(cal.getTime())));
        // 
        // Author
        String author = entry.getAuthor();
        if (author == null) {
            author = "unknown";
        }
        Element contributor = new Element("creator", NS_DC);
        item.addContent(contributor);
        /*
            Element description = new Element("Description", NS_RDF);
            if( m_wikiContext.getEngine().pageExists(author) ) {
                description.setAttribute( "link", engine.getURL( WikiContext.VIEW, author, null, true ), NS_XMNLS );
            }

            description.addContent( new Element("value", NS_XMNLS).addContent( author) );
            contributor.addContent( description );
           */
        // Not too many aggregators seem to like this.  Therefore we're just adding the name here.
        contributor.addContent(author);
        // 
        // PageHistory
        item.addContent(new Element("history", NS_WIKI).addContent(engine.getURL(WikiContext.INFO, p.getName(), null, true)));
        // 
        // Add to root
        root.addContent(item);
    }
}
Also used : Element(org.jdom2.Element) WikiPage(org.apache.wiki.WikiPage) Calendar(java.util.Calendar) SimpleDateFormat(java.text.SimpleDateFormat) WikiEngine(org.apache.wiki.WikiEngine)

Example 77 with Content

use of org.jdom2.Content in project jspwiki by apache.

the class IndexPlugin method getLink.

private Element getLink(String href, String content) {
    Element a = new Element("a", xmlns_XHTML);
    a.setAttribute("href", href);
    a.addContent(content);
    return a;
}
Also used : Element(org.jdom2.Element)

Example 78 with Content

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

the class MCRXMLHelperTest method testList.

@Test
public void testList() throws Exception {
    Element child1 = new Element("child").setText("Hallo Welt");
    Element child2 = new Element("child").setText("hello world");
    Element child3 = new Element("child").setText("Bonjour le monde");
    List<Content> l1 = new ArrayList<>();
    l1.add(child1);
    l1.add(child2);
    l1.add(child3);
    Element root = new Element("root");
    root.addContent(l1);
    String formattedXML = "<root>\n<child>Hallo Welt</child>\n" + "<child>hello world</child>" + "<child>Bonjour le monde</child>\n</root>";
    SAXBuilder b = new SAXBuilder();
    Document doc = b.build(new ByteArrayInputStream(formattedXML.getBytes(Charset.forName("UTF-8"))));
    assertEquals("Elements should be equal", true, MCRXMLHelper.deepEqual(root, doc.getRootElement()));
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) Content(org.jdom2.Content) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) Document(org.jdom2.Document) Test(org.junit.Test)

Example 79 with Content

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

the class MCRWCMSDefaultSectionProvider method getContent.

/**
 * Returns the content of an element as string. The element itself
 * is ignored.
 *
 * @param e the element to get the content from
 * @return the content as string
 */
protected String getContent(Element e) throws IOException {
    XMLOutputter out = new XMLOutputter();
    StringWriter writer = new StringWriter();
    for (Content child : e.getContent()) {
        if (child instanceof Element) {
            out.output((Element) child, writer);
        } else if (child instanceof Text) {
            Text t = (Text) child;
            String trimmedText = t.getTextTrim();
            if (!trimmedText.equals("")) {
                Text newText = new Text(trimmedText);
                out.output(newText, writer);
            }
        }
    }
    return writer.toString();
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) StringWriter(java.io.StringWriter) Content(org.jdom2.Content) JsonElement(com.google.gson.JsonElement) Element(org.jdom2.Element) Text(org.jdom2.Text)

Example 80 with Content

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

the class MCRWCMSContentManager method save.

/**
 * Saves the content of the given items.
 *
 * Returns a json object, if everything is ok:
 * <p>
 * { type: "saveDone" }
 * </p>
 *
 * <p>
 * If one or more files could'nt be saved because of an error the returning
 * json looks like:<br>
 * {
 *   type: "error",
 *   errorArray: [
 *       {type: "error", errorType: "invalidDirectory", webpageId: "abc.xml"},
 *       {type: "error", errorType: "invalidFile", webpageId: "abc2.xml"}
 *       ...
 *  ]
 * }
 * </p>
 */
public void save(JsonArray items) {
    XMLOutputter out = new XMLOutputter(Format.getRawFormat().setEncoding("UTF-8"));
    for (JsonElement e : items) {
        if (!e.isJsonObject()) {
            LOGGER.warn("Invalid json element in items {}", e);
            continue;
        }
        JsonObject item = e.getAsJsonObject();
        if (!item.has("dirty") || !item.get("dirty").getAsBoolean()) {
            continue;
        }
        JsonElement webpageIdElement = item.has("href") ? item.get("href") : (item.has("hrefStartingPage") ? item.get("hrefStartingPage") : null);
        if (webpageIdElement == null || !webpageIdElement.isJsonPrimitive()) {
            continue;
        }
        // trotzdem umgeschrieben werden -> check auf file exists
        if (!item.has("content") || !item.get("content").isJsonArray()) {
            continue;
        }
        String webpageId = webpageIdElement.getAsString();
        if (!webpageId.endsWith(".xml")) {
            throwError(ErrorType.invalidFile, webpageId);
        }
        JsonArray content = item.get("content").getAsJsonArray();
        Element mycoreWebpage = this.sectionProvider.fromJSON(content);
        // save
        try (OutputStream fout = MCRWebPagesSynchronizer.getOutputStream(webpageId)) {
            out.output(new Document(mycoreWebpage), fout);
        } catch (Exception exc) {
            LOGGER.error("Error while saving {}", webpageId, exc);
            throwError(ErrorType.couldNotSave, webpageId);
        }
    }
}
Also used : JsonArray(com.google.gson.JsonArray) XMLOutputter(org.jdom2.output.XMLOutputter) JsonElement(com.google.gson.JsonElement) JsonElement(com.google.gson.JsonElement) Element(org.jdom2.Element) OutputStream(java.io.OutputStream) JsonObject(com.google.gson.JsonObject) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) WebApplicationException(javax.ws.rs.WebApplicationException)

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