use of org.dom4j.io.OutputFormat in project core by craftercms.
the class XmlUtils method documentToPrettyString.
/**
* Returns the given document as a XML string in a "pretty" format.
*
* @param document
* @return the document as an XML string
*/
public static String documentToPrettyString(Document document) {
StringWriter stringWriter = new StringWriter();
OutputFormat prettyPrintFormat = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(stringWriter, prettyPrintFormat);
try {
xmlWriter.write(document);
} catch (IOException e) {
// Ignore, shouldn't happen.
}
return stringWriter.toString();
}
use of org.dom4j.io.OutputFormat in project mustangproject by ZUGFeRD.
the class ZUGFeRDValidator method validate.
/**
* performs a validation on the file filename
*
* @param filename the complete absolute filename of a PDF or XML
* @return a xml string with the validation result
*/
public String validate(String filename) {
boolean xmlValidity;
context.clear();
StringBuffer finalStringResult = new StringBuffer();
SimpleDateFormat isoDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
startTime = Calendar.getInstance().getTimeInMillis();
try {
Path path = Paths.get(filename);
// set filename without path
context.setFilename(path.getFileName().toString());
} catch (NullPointerException ex) {
// ignore
}
finalStringResult.append("<validation filename='" + context.getFilename() + "' datetime='" + isoDF.format(date) + "'>");
boolean isPDF = false;
try {
if (filename == null) {
optionsRecognized = false;
context.addResultItem(new ValidationResultItem(ESeverity.fatal, "Filename not specified").setSection(10).setPart(EPart.pdf));
}
PDFValidator pdfv = new PDFValidator(context);
File file = new File(filename);
if (!file.exists()) {
context.addResultItem(new ValidationResultItem(ESeverity.fatal, "File not found").setSection(1).setPart(EPart.pdf));
} else if (file.length() < 32) {
// with less then 32 bytes it can not even be a proper XML file
context.addResultItem(new ValidationResultItem(ESeverity.fatal, "File too small").setSection(5).setPart(EPart.pdf));
} else {
BigFileSearcher searcher = new BigFileSearcher();
XMLValidator xv = new XMLValidator(context);
if (disableNotices) {
xv.disableNotices();
}
byte[] pdfSignature = { '%', 'P', 'D', 'F' };
isPDF = searcher.indexOf(file, pdfSignature) == 0;
if (isPDF) {
pdfv.setFilename(filename);
optionsRecognized = true;
try {
if (!file.exists()) {
context.addResultItem(new ValidationResultItem(ESeverity.exception, "File " + filename + " not found").setSection(1));
}
} catch (IrrecoverableValidationError irx) {
// @todo log
}
finalStringResult.append("<pdf>");
optionsRecognized = true;
try {
pdfv.validate();
sha1Checksum = calcSHA1(file);
// Validate PDF
finalStringResult.append(pdfv.getXMLResult());
pdfValidity = context.isValid();
Signature = context.getSignature();
// clear sets valid to true again
context.clear();
if (pdfv.getRawXML() != null) {
xv.setStringContent(pdfv.getRawXML());
displayXMLValidationOutput = true;
} else {
context.addResultItem(new ValidationResultItem(ESeverity.exception, "XML could not be extracted").setSection(17));
}
} catch (IrrecoverableValidationError irx) {
// @todo log
}
finalStringResult.append("</pdf>\n");
context.clearCustomXML();
} else {
boolean isXML = false;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
byte[] content = Files.readAllBytes(file.toPath());
content = XMLTools.removeBOM(content);
String s = new String(content, StandardCharsets.UTF_8);
InputSource is = new InputSource(new StringReader(s));
Document doc = db.parse(is);
Element root = doc.getDocumentElement();
// no exception so far
isXML = true;
} catch (Exception ex) {
// probably no xml file, sth like SAXParseException content not allowed in prolog
// ignore isXML is already false
// in the tests, this may error-out anyway
// ex.printStackTrace();
}
if (isXML) {
pdfValidity = true;
optionsRecognized = true;
xv.setFilename(filename);
if (file.exists()) {
sha1Checksum = calcSHA1(file);
}
displayXMLValidationOutput = true;
} else {
optionsRecognized = false;
context.addResultItem(new ValidationResultItem(ESeverity.exception, "File does not look like PDF nor XML (contains neither %PDF nor <?xml)").setSection(8));
}
}
if ((optionsRecognized) && (displayXMLValidationOutput)) {
finalStringResult.append("<xml>");
try {
xv.validate();
} catch (IrrecoverableValidationError irx) {
// @todo log
}
finalStringResult.append(xv.getXMLResult());
finalStringResult.append("</xml>");
context.clearCustomXML();
}
if ((isPDF) && (!pdfValidity)) {
context.setInvalid();
}
}
} catch (IrrecoverableValidationError irx) {
// @todo log
} finally {
finalStringResult.append(context.getXMLResult());
finalStringResult.append("</validation>");
}
OutputFormat format = OutputFormat.createPrettyPrint();
StringWriter sw = new StringWriter();
org.dom4j.Document document = null;
try {
document = DocumentHelper.parseText(new String(finalStringResult));
} catch (DocumentException e1) {
LOGGER.error(e1.getMessage());
}
XMLWriter writer = new XMLWriter(sw, format);
try {
writer.write(document);
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
xmlValidity = context.isValid();
long duration = Calendar.getInstance().getTimeInMillis() - startTime;
String toBeAppended = "";
if (logAppend != null) {
toBeAppended = logAppend;
}
String pdfResult = "invalid";
if (!isPDF) {
pdfResult = "absent";
} else if (pdfValidity) {
pdfResult = "valid";
}
LOGGER.info("Parsed PDF:" + pdfResult + " XML:" + (xmlValidity ? "valid" : "invalid") + " Signature:" + Signature + " Checksum:" + sha1Checksum + " Profile:" + context.getProfile() + " Version:" + context.getGeneration() + " Took:" + duration + "ms Errors:[" + context.getCSVResult() + "] " + toBeAppended);
wasCompletelyValid = ((pdfValidity) && (xmlValidity));
return sw.toString();
}
use of org.dom4j.io.OutputFormat in project mustangproject by ZUGFeRD.
the class ZUGFeRD1PullProvider method getXML.
@Override
public byte[] getXML() {
byte[] res = zugferdData;
final StringWriter sw = new StringWriter();
Document document = null;
try {
document = DocumentHelper.parseText(new String(zugferdData));
} catch (final DocumentException e1) {
Logger.getLogger(ZUGFeRD1PullProvider.class.getName()).log(Level.SEVERE, null, e1);
}
try {
final OutputFormat format = OutputFormat.createPrettyPrint();
format.setTrimText(false);
final XMLWriter writer = new XMLWriter(sw, format);
writer.write(document);
res = sw.toString().getBytes("UTF-8");
} catch (final IOException e) {
Logger.getLogger(ZUGFeRD1PullProvider.class.getName()).log(Level.SEVERE, null, e);
}
return res;
}
use of org.dom4j.io.OutputFormat in project mustangproject by ZUGFeRD.
the class ZUGFeRD2PullProvider method getXML.
@Override
public byte[] getXML() {
byte[] res = zugferdData;
final StringWriter sw = new StringWriter();
Document document = null;
try {
document = DocumentHelper.parseText(new String(zugferdData));
} catch (final DocumentException e1) {
Logger.getLogger(ZUGFeRD2PullProvider.class.getName()).log(Level.SEVERE, null, e1);
}
try {
final OutputFormat format = OutputFormat.createPrettyPrint();
format.setTrimText(false);
final XMLWriter writer = new XMLWriter(sw, format);
writer.write(document);
res = sw.toString().getBytes("UTF-8");
} catch (final IOException e) {
Logger.getLogger(ZUGFeRD2PullProvider.class.getName()).log(Level.SEVERE, null, e);
}
return res;
}
use of org.dom4j.io.OutputFormat in project atlas by alibaba.
the class UpdatePomTask method updatePomXml.
private void updatePomXml(File xml) throws IOException, DocumentException {
Map<String, DependencyExtraInfo> extraInfoMap = getExtraMap();
File backupFile = new File(xml.getParentFile(), "pom-backup.xml");
FileUtils.deleteQuietly(backupFile);
FileUtils.moveFile(xml, backupFile);
// Declares the object that writes XML
XMLWriter writer = null;
SAXReader reader = new SAXReader();
OutputFormat format = OutputFormat.createPrettyPrint();
// Sets the encoding format for the XML file
format.setEncoding("UTF-8");
FileOutputStream fos = new FileOutputStream(xml);
try {
// Read the XML file
Document document = reader.read(backupFile);
Element dependencies = document.getRootElement().element("dependencies");
if ((null != dependencies) && null != dependencies.elements()) {
List<Element> list = dependencies.elements();
for (Element element : list) {
List<Element> itemList = element.elements();
String group = "";
String name = "";
String type;
String scope;
Element scopeEl = null;
Element typeEl = null;
for (Element element1 : itemList) {
if ("groupId".equals(element1.getQName().getName())) {
group = element1.getStringValue();
} else if ("artifactId".equals(element1.getQName().getName())) {
name = element1.getStringValue();
} else if ("scope".equals(element1.getQName().getName())) {
scope = element1.getStringValue();
scopeEl = element1;
} else if ("type".equals(element1.getQName().getName())) {
type = element1.getStringValue();
typeEl = element1;
}
}
DependencyExtraInfo dependencyExtraInfo = extraInfoMap.get(group + ":" + name);
if (null == dependencyExtraInfo) {
continue;
}
// update scope
if (StringUtils.isNotEmpty(dependencyExtraInfo.scope)) {
if (null != scopeEl) {
scopeEl.setText(dependencyExtraInfo.scope);
} else {
Element newEl = element.addElement("scope");
newEl.setText(dependencyExtraInfo.scope);
}
}
if (StringUtils.isNotEmpty(dependencyExtraInfo.type)) {
if (null != typeEl) {
typeEl.setText(dependencyExtraInfo.type);
} else {
Element newEl = element.addElement("type");
newEl.setText(dependencyExtraInfo.type);
}
}
}
}
writer = new XMLWriter(fos, format);
writer.write(document);
} finally {
if (null != writer) {
writer.close();
}
IOUtils.closeQuietly(fos);
}
}
Aggregations