Search in sources :

Example 1 with ShieldInputStream

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

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 2 with ShieldInputStream

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

the class ExcelOOXMLDocument method parseSheets.

private String parseSheets(Map<String, String> sharedStrings, VFSLeaf leaf) throws IOException, DocumentException {
    try (InputStream stream = leaf.getInputStream();
        ZipInputStream zip = new ZipInputStream(stream)) {
        ZipEntry entry = zip.getNextEntry();
        LimitedContentWriter writer = new LimitedContentWriter(100000, FileDocumentFactory.getMaxFileSize());
        while (entry != null) {
            if (writer.accept()) {
                String name = entry.getName();
                if (name.startsWith(SHEET) && name.endsWith(".xml")) {
                    OfficeDocumentHandler dh = new OfficeDocumentHandler(writer, sharedStrings);
                    parse(new ShieldInputStream(zip), dh);
                }
            }
            entry = zip.getNextEntry();
        }
        return writer.toString();
    } catch (DocumentException e) {
        throw e;
    } catch (Exception e) {
        throw new DocumentException(e.getMessage());
    }
}
Also used : LimitedContentWriter(org.olat.core.util.io.LimitedContentWriter) 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 3 with ShieldInputStream

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

the class WordOOXMLDocument 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.endsWith("word/document.xml")) {
                contents.add(name);
            } else if (name.startsWith(HEADER) && name.endsWith(".xml")) {
                contents.add(name);
            } else if (name.startsWith(FOOTER) && name.endsWith(".xml")) {
                contents.add(name);
            }
        }
        if (contents.size() > 1) {
            Collections.sort(contents, new WordDocumentComparator());
        }
        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 4 with ShieldInputStream

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

the class QTI21ImportProcessor method process.

public List<QuestionItem> process(File file) {
    // export zip file
    List<QuestionItem> items = new ArrayList<>();
    try (FileSystem fs = FileSystems.newFileSystem(file.toPath(), null)) {
        Path fPath = fs.getPath("/");
        if (fPath != null) {
            ImsManifestVisitor visitor = new ImsManifestVisitor();
            Files.walkFileTree(fPath, visitor);
            List<Path> imsmanifests = visitor.getImsmanifestFiles();
            for (Path imsmanifest : imsmanifests) {
                InputStream in = Files.newInputStream(imsmanifest);
                ManifestBuilder manifestBuilder = ManifestBuilder.read(new ShieldInputStream(in));
                List<ResourceType> resources = manifestBuilder.getResourceList();
                for (ResourceType resource : resources) {
                    ManifestMetadataBuilder metadataBuilder = manifestBuilder.getMetadataBuilder(resource, true);
                    QuestionItem qitem = processResource(resource, imsmanifest, metadataBuilder);
                    if (qitem != null) {
                        items.add(qitem);
                    }
                }
            }
        }
    } catch (IOException e) {
        log.error("", e);
    }
    return items;
}
Also used : Path(java.nio.file.Path) ManifestBuilder(org.olat.ims.qti21.model.xml.ManifestBuilder) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ResourceType(org.olat.imscp.xml.manifest.ResourceType) IOException(java.io.IOException) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) ManifestMetadataBuilder(org.olat.ims.qti21.model.xml.ManifestMetadataBuilder) FileSystem(java.nio.file.FileSystem) QuestionItem(org.olat.modules.qpool.QuestionItem)

Example 5 with ShieldInputStream

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

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