use of org.dom4j.DocumentException in project GenericKnimeNodes by genericworkflownodes.
the class SchemaValidator method validates.
/**
* Validate the given xml stream against the stored schemata.
*
* @param xmlstream
* The stream to validate.
* @return True if the file is valid, false otherwise.
*/
public boolean validates(InputStream xmlstream) {
SAXParserFactory factory = SAXParserFactory.newInstance();
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
SimpleErrorHandler errorHandler = new SimpleErrorHandler();
try {
factory.setSchema(schemaFactory.newSchema(getSchemaSources()));
SAXParser parser = factory.newSAXParser();
SAXReader reader = new SAXReader(parser.getXMLReader());
reader.setValidation(false);
reader.setErrorHandler(errorHandler);
reader.read(xmlstream);
} catch (SAXException e) {
throw new RuntimeException(e);
} catch (DocumentException e) {
throw new RuntimeException(e);
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
}
if (!errorHandler.isValid()) {
errorReport = errorHandler.getErrorReport();
return false;
}
return true;
}
use of org.dom4j.DocumentException in project xwiki-platform by xwiki.
the class Package method Import.
/**
* Load this package in memory from an InputStream. It may be installed later using {@link #install(XWikiContext)}.
*
* @param file an InputStream of a zipped package file
* @param context current XWikiContext
* @return an empty string, useless.
* @throws IOException while reading the ZipFile
* @throws XWikiException when package content is broken
* @since 2.3M2
*/
public String Import(InputStream file, XWikiContext context) throws IOException, XWikiException {
ZipArchiveInputStream zis;
ArchiveEntry entry;
Document description = null;
try {
zis = new ZipArchiveInputStream(file, XAR_FILENAME_ENCODING, false);
List<XWikiDocument> docsToLoad = new LinkedList<XWikiDocument>();
/*
* Loop 1: Cycle through the zip input stream and load out all of the documents, when we find the
* package.xml file we put it aside to so that we only include documents which are in the file.
*/
while ((entry = zis.getNextEntry()) != null) {
if (entry.isDirectory() || (entry.getName().indexOf("META-INF") != -1)) {
// (we use that directory to put meta data such as LICENSE/NOTICE files.)
continue;
} else if (entry.getName().compareTo(DefaultPackageFileName) == 0) {
// The entry is the manifest (package.xml). Read this differently.
description = fromXml(new CloseShieldInputStream(zis));
} else {
XWikiDocument doc = null;
try {
doc = readFromXML(new CloseShieldInputStream(zis));
} catch (Throwable e) {
LOGGER.warn("Failed to parse document [{}] from XML during import, thus it will not be installed. " + "The error was: " + ExceptionUtils.getRootCauseMessage(e));
// It will be listed in the "failed documents" section after the import.
addToErrors(entry.getName().replaceAll("/", "."), context);
continue;
}
// if no filters throw exceptions, add it to the list to import.
try {
this.filter(doc, context);
docsToLoad.add(doc);
} catch (ExcludeDocumentException e) {
LOGGER.info("Skip the document '" + doc.getDocumentReference() + "'");
}
}
}
// Make sure a manifest was included in the package...
if (description == null) {
throw new PackageException(XWikiException.ERROR_XWIKI_UNKNOWN, "Could not find the package definition");
}
/*
* Loop 2: Cycle through the list of documents and if they are in the manifest then add them, otherwise log
* a warning and add them to the skipped list.
*/
for (XWikiDocument doc : docsToLoad) {
if (documentExistInPackageFile(doc.getFullName(), doc.getLanguage(), description)) {
this.add(doc, context);
} else {
LOGGER.warn("document " + doc.getDocumentReference() + " does not exist in package definition." + " It will not be installed.");
// It will be listed in the "skipped documents" section after the
// import.
addToSkipped(doc.getFullName(), context);
}
}
updateFileInfos(description);
} catch (DocumentException e) {
throw new PackageException(XWikiException.ERROR_XWIKI_UNKNOWN, "Error when reading the XML");
}
return "";
}
use of org.dom4j.DocumentException in project xwiki-platform by xwiki.
the class AbstractXMLSerializer method parse.
@Override
public P parse(final InputStream stream) throws IOException {
final SAXReader reader = new SAXReader();
// Remove nodes generated by indentation.
reader.setStripWhitespaceText(true);
reader.setMergeAdjacentText(true);
final Document domdoc;
try {
domdoc = reader.read(stream);
} catch (DocumentException e) {
throw new IOException("Failed to parse XML, probably malformed input.");
}
return this.parse(domdoc.getRootElement());
}
use of org.dom4j.DocumentException in project my_curd by qinyou.
the class ToolFormatXml method formatXML.
public static String formatXML(String inputXML) {
String requestXML = null;
Document document = null;
try {
SAXReader reader = new SAXReader();
document = reader.read(new StringReader(inputXML));
} catch (DocumentException e1) {
e1.printStackTrace();
}
if (document != null) {
XMLWriter writer = null;
try {
StringWriter stringWriter = new StringWriter();
OutputFormat format = new OutputFormat(" ", true);
writer = new XMLWriter(stringWriter, format);
writer.write(document);
writer.flush();
requestXML = stringWriter.getBuffer().toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
}
}
}
}
return requestXML;
}
use of org.dom4j.DocumentException in project EssayJoke by qiyei2015.
the class Dom4jHelper method parseXml.
/**
* 解析xml文件
* @param xmlFile
*/
public static void parseXml(File xmlFile) {
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(xmlFile);
Element element = document.getRootElement();
readeNode(element, "");
} catch (DocumentException e) {
e.printStackTrace();
}
}
Aggregations