Search in sources :

Example 1 with OdfPackage

use of org.odftoolkit.odfdom.pkg.OdfPackage in project structr by structr.

the class ODFExporter method exportImage.

public static void exportImage(final ODFExporter thisNode, final String uuid) {
    final File output = thisNode.getResultDocument();
    try {
        final App app = StructrApp.getInstance();
        final Image result = app.nodeQuery(Image.class).and(GraphObject.id, uuid).getFirst();
        String imageName = result.getProperty(new StringProperty("name"));
        String contentType = result.getProperty(new StringProperty("contentType"));
        String templateImagePath = null;
        OdfDocument doc = OdfDocument.loadDocument(output.getFileOnDisk().getAbsolutePath());
        NodeList nodes = doc.getContentRoot().getElementsByTagName(ODF_IMAGE_PARENT_NAME);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node currentNode = nodes.item(i);
            NamedNodeMap attrs = currentNode.getAttributes();
            Node fieldName = attrs.getNamedItem(ODF_IMAGE_ATTRIBUTE_PARENT_IMAGE_NAME);
            if (fieldName != null && fieldName.getTextContent().equals(imageName)) {
                NamedNodeMap childAttrs = currentNode.getFirstChild().getAttributes();
                Node filePath = childAttrs.getNamedItem(ODF_IMAGE_ATTRIBUTE_FILE_PATH);
                templateImagePath = filePath.getTextContent();
                filePath.setTextContent(ODF_IMAGE_DIRECTORY + imageName);
            }
        }
        OdfPackage pkg = doc.getPackage();
        if (templateImagePath != null && templateImagePath.length() > 0) {
            pkg.remove(templateImagePath);
        }
        pkg.insert(new URI(result.getFileOnDisk().getAbsolutePath()), ODF_IMAGE_DIRECTORY + imageName, contentType);
        pkg.save(output.getFileOnDisk().getAbsolutePath());
        pkg.close();
        doc.close();
    } catch (Exception e) {
        logger.error("Error while exporting image to document", e);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) AbstractNode(org.structr.core.entity.AbstractNode) StringProperty(org.structr.core.property.StringProperty) Image(org.structr.web.entity.Image) URI(java.net.URI) FrameworkException(org.structr.common.error.FrameworkException) OdfPackage(org.odftoolkit.odfdom.pkg.OdfPackage) OdfDocument(org.odftoolkit.odfdom.doc.OdfDocument) File(org.structr.web.entity.File)

Aggregations

URI (java.net.URI)1 OdfDocument (org.odftoolkit.odfdom.doc.OdfDocument)1 OdfPackage (org.odftoolkit.odfdom.pkg.OdfPackage)1 FrameworkException (org.structr.common.error.FrameworkException)1 App (org.structr.core.app.App)1 StructrApp (org.structr.core.app.StructrApp)1 AbstractNode (org.structr.core.entity.AbstractNode)1 StringProperty (org.structr.core.property.StringProperty)1 File (org.structr.web.entity.File)1 Image (org.structr.web.entity.Image)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1