Search in sources :

Example 6 with ShieldInputStream

use of org.olat.core.util.io.ShieldInputStream in project openolat by klemens.

the class ExcelOOXMLDocument method parseSharedStrings.

private Map<String, String> parseSharedStrings(VFSLeaf leaf) throws IOException, DocumentException {
    SharedStringsHandler dh = new SharedStringsHandler();
    try (InputStream stream = leaf.getInputStream();
        ZipInputStream zip = new ZipInputStream(stream)) {
        ZipEntry entry = zip.getNextEntry();
        while (entry != null) {
            String name = entry.getName();
            if (name.endsWith("xl/sharedStrings.xml")) {
                parse(new ShieldInputStream(zip), dh);
                break;
            }
            entry = zip.getNextEntry();
        }
        return dh.getMap();
    } catch (DocumentException e) {
        throw e;
    } catch (Exception e) {
        throw new DocumentException(e.getMessage());
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipInputStream(java.util.zip.ZipInputStream) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 7 with ShieldInputStream

use of org.olat.core.util.io.ShieldInputStream in project openolat by klemens.

the class OpenDocument method readContent.

public FileContent readContent(VFSLeaf leaf) throws DocumentException {
    final OpenDocumentHandler dh = new OpenDocumentHandler();
    InputStream stream = null;
    ZipInputStream zip = null;
    try {
        stream = leaf.getInputStream();
        zip = new ZipInputStream(stream);
        ZipEntry entry = zip.getNextEntry();
        while (entry != null) {
            if (entry.getName().endsWith("content.xml")) {
                parse(new ShieldInputStream(zip), dh);
                // we parsed only content
                break;
            }
            entry = zip.getNextEntry();
        }
    } catch (DocumentException e) {
        throw e;
    } catch (Exception e) {
        throw new DocumentException(e.getMessage());
    } finally {
        FileUtils.closeSafely(zip);
        FileUtils.closeSafely(stream);
    }
    return new FileContent(dh.getContent());
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipInputStream(java.util.zip.ZipInputStream) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) IOException(java.io.IOException)

Example 8 with ShieldInputStream

use of org.olat.core.util.io.ShieldInputStream in project OpenOLAT by OpenOLAT.

the class ExcelOOXMLDocument method parseSharedStrings.

private Map<String, String> parseSharedStrings(VFSLeaf leaf) throws IOException, DocumentException {
    SharedStringsHandler dh = new SharedStringsHandler();
    try (InputStream stream = leaf.getInputStream();
        ZipInputStream zip = new ZipInputStream(stream)) {
        ZipEntry entry = zip.getNextEntry();
        while (entry != null) {
            String name = entry.getName();
            if (name.endsWith("xl/sharedStrings.xml")) {
                parse(new ShieldInputStream(zip), dh);
                break;
            }
            entry = zip.getNextEntry();
        }
        return dh.getMap();
    } catch (DocumentException e) {
        throw e;
    } catch (Exception e) {
        throw new DocumentException(e.getMessage());
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipInputStream(java.util.zip.ZipInputStream) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 9 with ShieldInputStream

use of org.olat.core.util.io.ShieldInputStream in project OpenOLAT by OpenOLAT.

the class PowerPointOOXMLDocument method readContent.

@Override
public FileContent readContent(VFSLeaf leaf) throws IOException, DocumentException {
    File file = ((JavaIOItem) leaf).getBasefile();
    LimitedContentWriter writer = new LimitedContentWriter(100000, FileDocumentFactory.getMaxFileSize());
    try (ZipFile wordFile = new ZipFile(file)) {
        List<String> contents = new ArrayList<>();
        for (Enumeration<? extends ZipEntry> entriesEnumeration = wordFile.entries(); entriesEnumeration.hasMoreElements(); ) {
            ZipEntry entry = entriesEnumeration.nextElement();
            String name = entry.getName();
            if (name.startsWith(SLIDE) && name.endsWith(".xml")) {
                contents.add(name);
            }
        }
        if (contents.size() > 1) {
            Collections.sort(contents, new PowerPointDocumentComparator());
        }
        for (String content : contents) {
            if (writer.accept()) {
                ZipEntry entry = wordFile.getEntry(content);
                InputStream zip = wordFile.getInputStream(entry);
                OfficeDocumentHandler dh = new OfficeDocumentHandler(writer);
                parse(new ShieldInputStream(zip), dh);
                zip.close();
            }
        }
    } catch (DocumentException e) {
        throw e;
    } catch (Exception e) {
        throw new DocumentException(e.getMessage());
    }
    return new FileContent(writer.toString());
}
Also used : JavaIOItem(org.olat.core.util.vfs.JavaIOItem) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) IOException(java.io.IOException) LimitedContentWriter(org.olat.core.util.io.LimitedContentWriter) ZipFile(java.util.zip.ZipFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 10 with ShieldInputStream

use of org.olat.core.util.io.ShieldInputStream in project OpenOLAT by OpenOLAT.

the class OpenXMLDocument method convertLaTeX.

public List<Node> convertLaTeX(String latex) {
    List<Node> mathEls = new ArrayList<Node>();
    try {
        // convert latex -> mathml
        String mathml = ConvertFromLatexToMathML.convertToMathML(latex);
        // convert mathml to word docx
        ByteArrayOutputStream out = new ByteArrayOutputStream(20000);
        ConvertFromMathMLToWord.writeWordDocStreamFromMathML(out, mathml);
        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
        out = null;
        // extract docx
        ZipInputStream zip = new ZipInputStream(in);
        ZipEntry entry = zip.getNextEntry();
        while (entry != null) {
            String name = entry.getName();
            if (name.endsWith("word/document.xml")) {
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                factory.setValidating(false);
                factory.setNamespaceAware(false);
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document doc = builder.parse(new ShieldInputStream(zip));
                NodeList bodyList = doc.getElementsByTagName("w:body");
                if (bodyList.getLength() == 1) {
                    Node body = bodyList.item(0);
                    for (Node node = body.getFirstChild(); node != null; node = node.getNextSibling()) {
                        Node importedNode = document.importNode(node, true);
                        mathEls.add(importedNode);
                    }
                }
            }
            entry = zip.getNextEntry();
        }
    } catch (Exception e) {
        log.error("", e);
    }
    return mathEls;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Node(org.w3c.dom.Node) ZipEntry(java.util.zip.ZipEntry) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) URISyntaxException(java.net.URISyntaxException) DOMException(org.w3c.dom.DOMException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Aggregations

ShieldInputStream (org.olat.core.util.io.ShieldInputStream)18 IOException (java.io.IOException)16 ZipEntry (java.util.zip.ZipEntry)16 InputStream (java.io.InputStream)12 ZipInputStream (java.util.zip.ZipInputStream)12 SAXException (org.xml.sax.SAXException)10 ArrayList (java.util.ArrayList)8 LimitedContentWriter (org.olat.core.util.io.LimitedContentWriter)6 File (java.io.File)4 ZipFile (java.util.zip.ZipFile)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 JavaIOItem (org.olat.core.util.vfs.JavaIOItem)4 SAXParseException (org.xml.sax.SAXParseException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 URISyntaxException (java.net.URISyntaxException)2 FileSystem (java.nio.file.FileSystem)2 Path (java.nio.file.Path)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2