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;
}
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;
}
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());
}
}
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());
}
}
}
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);
}
}
Aggregations