Search in sources :

Example 91 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project adempiere by adempiere.

the class MArchive method getBinaryDataFromFileSystem.

/**
	 * @return attachment data
	 */
private byte[] getBinaryDataFromFileSystem() {
    if ("".equals(m_archivePathRoot)) {
        throw new IllegalArgumentException("no attachmentPath defined");
    }
    byte[] data = super.getBinaryData();
    m_deflated = null;
    m_inflated = null;
    if (data == null) {
        return null;
    }
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {
        final DocumentBuilder builder = factory.newDocumentBuilder();
        final Document document = builder.parse(new ByteArrayInputStream(data));
        final NodeList entries = document.getElementsByTagName("entry");
        if (entries.getLength() != 1) {
            log.severe("no archive entry found");
        }
        final Node entryNode = entries.item(0);
        final NamedNodeMap attributes = entryNode.getAttributes();
        final Node fileNode = attributes.getNamedItem("file");
        if (fileNode == null) {
            log.severe("no filename for entry");
            return null;
        }
        String filePath = fileNode.getNodeValue();
        log.fine("filePath: " + filePath);
        if (filePath != null) {
            filePath = filePath.replaceFirst(ARCHIVE_FOLDER_PLACEHOLDER, m_archivePathRoot.replaceAll("\\\\", "\\\\\\\\"));
            //just to be shure...
            String replaceSeparator = File.separator;
            if (!replaceSeparator.equals("/")) {
                replaceSeparator = "\\\\";
            }
            filePath = filePath.replaceAll("/", replaceSeparator);
            filePath = filePath.replaceAll("\\\\", replaceSeparator);
        }
        log.fine("filePath: " + filePath);
        final File file = new File(filePath);
        if (file.exists()) {
            // read files into byte[]
            final byte[] dataEntry = new byte[(int) file.length()];
            try {
                final FileInputStream fileInputStream = new FileInputStream(file);
                fileInputStream.read(dataEntry);
                fileInputStream.close();
            } catch (FileNotFoundException e) {
                log.severe("File Not Found.");
                e.printStackTrace();
            } catch (IOException e1) {
                log.severe("Error Reading The File.");
                e1.printStackTrace();
            }
            return dataEntry;
        } else {
            log.severe("file not found: " + file.getAbsolutePath());
            return null;
        }
    } catch (SAXException sxe) {
        // Error generated during parsing)
        Exception x = sxe;
        if (sxe.getException() != null)
            x = sxe.getException();
        x.printStackTrace();
        log.severe(x.getMessage());
    } catch (ParserConfigurationException pce) {
        // Parser with specified options can't be built
        pce.printStackTrace();
        log.severe(pce.getMessage());
    } catch (IOException ioe) {
        // I/O error
        ioe.printStackTrace();
        log.severe(ioe.getMessage());
    }
    return null;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File)

Example 92 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project adempiere by adempiere.

the class MAttachment method loadLOBDataFromFileSystem.

//	loadLOBData
/**
	 * 	Load Data from file system
	 *	@return true if success
	 */
private boolean loadLOBDataFromFileSystem() {
    if ("".equals(m_attachmentPathRoot)) {
        log.severe("no attachmentPath defined");
        return false;
    }
    // Reset
    m_items = new ArrayList<MAttachmentEntry>();
    //
    byte[] data = getBinaryData();
    if (data == null)
        return true;
    log.fine("TextFileSize=" + data.length);
    if (data.length == 0)
        return true;
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {
        final DocumentBuilder builder = factory.newDocumentBuilder();
        final Document document = builder.parse(new ByteArrayInputStream(data));
        final NodeList entries = document.getElementsByTagName("entry");
        for (int i = 0; i < entries.getLength(); i++) {
            final Node entryNode = entries.item(i);
            final NamedNodeMap attributes = entryNode.getAttributes();
            final Node fileNode = attributes.getNamedItem("file");
            final Node nameNode = attributes.getNamedItem("name");
            if (fileNode == null || nameNode == null) {
                log.severe("no filename for entry " + i);
                m_items = null;
                return false;
            }
            log.fine("name: " + nameNode.getNodeValue());
            String filePath = fileNode.getNodeValue();
            log.fine("filePath: " + filePath);
            if (filePath != null) {
                filePath = filePath.replaceFirst(ATTACHMENT_FOLDER_PLACEHOLDER, m_attachmentPathRoot.replaceAll("\\\\", "\\\\\\\\"));
                //just to be shure...
                String replaceSeparator = File.separator;
                if (!replaceSeparator.equals("/")) {
                    replaceSeparator = "\\\\";
                }
                filePath = filePath.replaceAll("/", replaceSeparator);
                filePath = filePath.replaceAll("\\\\", replaceSeparator);
            }
            log.fine("filePath: " + filePath);
            final File file = new File(filePath);
            if (file.exists()) {
                // read files into byte[]
                final byte[] dataEntry = new byte[(int) file.length()];
                try {
                    final FileInputStream fileInputStream = new FileInputStream(file);
                    fileInputStream.read(dataEntry);
                    fileInputStream.close();
                } catch (FileNotFoundException e) {
                    log.severe("File Not Found.");
                    e.printStackTrace();
                } catch (IOException e1) {
                    log.severe("Error Reading The File.");
                    e1.printStackTrace();
                }
                final MAttachmentEntry entry = new MAttachmentEntry(filePath, dataEntry, m_items.size() + 1);
                m_items.add(entry);
            } else {
                log.severe("file not found: " + file.getAbsolutePath());
            }
        }
    } catch (SAXException sxe) {
        // Error generated during parsing)
        Exception x = sxe;
        if (sxe.getException() != null)
            x = sxe.getException();
        x.printStackTrace();
        log.severe(x.getMessage());
    } catch (ParserConfigurationException pce) {
        // Parser with specified options can't be built
        pce.printStackTrace();
        log.severe(pce.getMessage());
    } catch (IOException ioe) {
        // I/O error
        ioe.printStackTrace();
        log.severe(ioe.getMessage());
    }
    return true;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File)

Example 93 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project poi by apache.

the class XSSFEventBasedExcelExtractor method processSheet.

/**
     * Processes the given sheet
     */
public void processSheet(SheetContentsHandler sheetContentsExtractor, StylesTable styles, CommentsTable comments, ReadOnlySharedStringsTable strings, InputStream sheetInputStream) throws IOException, SAXException {
    DataFormatter formatter;
    if (locale == null) {
        formatter = new DataFormatter();
    } else {
        formatter = new DataFormatter(locale);
    }
    InputSource sheetSource = new InputSource(sheetInputStream);
    try {
        XMLReader sheetParser = SAXHelper.newXMLReader();
        ContentHandler handler = new XSSFSheetXMLHandler(styles, comments, strings, sheetContentsExtractor, formatter, formulasNotResults);
        sheetParser.setContentHandler(handler);
        sheetParser.parse(sheetSource);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException("SAX parser appears to be broken - " + e.getMessage());
    }
}
Also used : InputSource(org.xml.sax.InputSource) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader) ContentHandler(org.xml.sax.ContentHandler) DataFormatter(org.apache.poi.ss.usermodel.DataFormatter) XSSFSheetXMLHandler(org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler)

Example 94 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project poi by apache.

the class ReadOnlySharedStringsTable method readFrom.

/**
     * Read this shared strings table from an XML file.
     *
     * @param is The input stream containing the XML document.
     * @throws IOException if an error occurs while reading.
     * @throws SAXException if parsing the XML data fails.
     */
public void readFrom(InputStream is) throws IOException, SAXException {
    // test if the file is empty, otherwise parse it
    PushbackInputStream pis = new PushbackInputStream(is, 1);
    int emptyTest = pis.read();
    if (emptyTest > -1) {
        pis.unread(emptyTest);
        InputSource sheetSource = new InputSource(pis);
        try {
            XMLReader sheetParser = SAXHelper.newXMLReader();
            sheetParser.setContentHandler(this);
            sheetParser.parse(sheetSource);
        } catch (ParserConfigurationException e) {
            throw new RuntimeException("SAX parser appears to be broken - " + e.getMessage());
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) PushbackInputStream(java.io.PushbackInputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader)

Example 95 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project poi by apache.

the class DocumentHelper method newDocumentBuilder.

/**
     * Creates a new document builder, with sensible defaults
     *
     * @throws IllegalStateException If creating the DocumentBuilder fails, e.g.
     *  due to {@link ParserConfigurationException}.
     */
public static synchronized DocumentBuilder newDocumentBuilder() {
    try {
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        documentBuilder.setEntityResolver(SAXHelper.IGNORING_ENTITY_RESOLVER);
        documentBuilder.setErrorHandler(new DocHelperErrorHandler());
        return documentBuilder;
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException("cannot create a DocumentBuilder", e);
    }
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1353 SAXException (org.xml.sax.SAXException)975 IOException (java.io.IOException)891 Document (org.w3c.dom.Document)710 DocumentBuilder (javax.xml.parsers.DocumentBuilder)631 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)569 Element (org.w3c.dom.Element)372 InputSource (org.xml.sax.InputSource)246 NodeList (org.w3c.dom.NodeList)226 Node (org.w3c.dom.Node)210 SAXParser (javax.xml.parsers.SAXParser)175 TransformerException (javax.xml.transform.TransformerException)163 File (java.io.File)162 InputStream (java.io.InputStream)158 SAXParserFactory (javax.xml.parsers.SAXParserFactory)137 ByteArrayInputStream (java.io.ByteArrayInputStream)129 StringReader (java.io.StringReader)117 ArrayList (java.util.ArrayList)115 DOMSource (javax.xml.transform.dom.DOMSource)109 StreamResult (javax.xml.transform.stream.StreamResult)93