use of org.dom4j.io.OutputFormat in project sagacity-sqltoy by chenrenfei.
the class XMLUtil method updateXML.
/**
* @todo 编辑xml文件
* @param xmlFile
* @param charset
* @param isValidator
* @param handler
* @return
* @throws Exception
*/
public static boolean updateXML(File xmlFile, String charset, boolean isValidator, XMLCallbackHandler handler) throws Exception {
if (handler == null || xmlFile == null || !xmlFile.exists())
return false;
InputStream is = null;
FileOutputStream fos = null;
try {
SAXReader saxReader = new SAXReader();
if (!isValidator)
saxReader.setFeature(NO_VALIDATOR_FEATURE, false);
if (charset != null)
saxReader.setEncoding(charset);
is = new FileInputStream(xmlFile);
if (null != is) {
org.dom4j.Document doc = saxReader.read(is);
if (null != doc) {
handler.process(doc, doc.getRootElement());
OutputFormat format = OutputFormat.createPrettyPrint();
if (charset != null)
format.setEncoding(charset);
fos = new FileOutputStream(xmlFile);
XMLWriter output = new XMLWriter(fos, format);
output.write(doc);
}
is.close();
}
} catch (Exception e) {
logger.error("修改XML文件:{}错误:{}!", xmlFile, e.getMessage());
throw e;
} finally {
if (fos != null)
fos.close();
if (is != null)
is.close();
}
return true;
}
use of org.dom4j.io.OutputFormat in project tmdm-studio-se by Talend.
the class Util method formatXsdSource.
public static String formatXsdSource(String xsdSource, boolean suppressDeclaration) {
try {
SAXReader reader = new SAXReader();
org.dom4j.Document document = reader.read(new StringReader(xsdSource));
StringWriter writer = new StringWriter();
OutputFormat format = OutputFormat.createPrettyPrint();
// $NON-NLS-1$
format.setEncoding("UTF-8");
format.setSuppressDeclaration(suppressDeclaration);
XMLWriter xmlwriter = new XMLWriter(writer, format);
xmlwriter.write(document);
String str = writer.toString();
writer.close();
xmlwriter.close();
return str;
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return xsdSource;
}
use of org.dom4j.io.OutputFormat in project OpenOLAT by OpenOLAT.
the class QTIExportProcessor method assembleTest.
/**
* <li>List all items
* <li>Rewrite path
* <li>Assemble qti.xml
* <li>Write files at new path
* @param fullItems
* @param zout
*/
public void assembleTest(List<QuestionItemFull> fullItems, ZipOutputStream zout) {
ItemsAndMaterials itemAndMaterials = new ItemsAndMaterials();
for (QuestionItemFull fullItem : fullItems) {
collectMaterials(fullItem, itemAndMaterials);
}
try {
byte[] buffer = new byte[FileUtils.BSIZE];
// write qti.xml
Element sectionEl = createSectionBasedAssessment("Assessment");
for (Element itemEl : itemAndMaterials.getItemEls()) {
// generate new ident per item
String ident = getAttributeValue(itemEl, "ident");
String exportIdent = QTIEditHelper.generateNewIdent(ident);
itemEl.addAttribute("ident", exportIdent);
sectionEl.add(itemEl);
}
zout.putNextEntry(new ZipEntry("qti.xml"));
XMLWriter xw = new XMLWriter(zout, new OutputFormat(" ", true));
xw.write(sectionEl.getDocument());
zout.closeEntry();
// write materials
for (ItemMaterial material : itemAndMaterials.getMaterials()) {
String exportPath = material.getExportUri();
zout.putNextEntry(new ZipEntry(exportPath));
InputStream in = material.getLeaf().getInputStream();
int c;
while ((c = in.read(buffer, 0, buffer.length)) != -1) {
zout.write(buffer, 0, c);
}
IOUtils.closeQuietly(in);
zout.closeEntry();
}
} catch (IOException e) {
log.error("", e);
}
}
use of org.dom4j.io.OutputFormat in project OpenOLAT by OpenOLAT.
the class FilePersister method createResultsReporting.
/**
* Persist results for this user/aiid as an XML document. dlPointer is aiid in
* this case.
*
* @param doc
* @param type
* @param info
*/
public static void createResultsReporting(Document doc, Identity subj, String type, long aiid) {
File fUserdataRoot = new File(WebappHelper.getUserDataRoot());
String path = RES_REPORTING + File.separator + subj.getName() + File.separator + type;
File fReportingDir = new File(fUserdataRoot, path);
try {
fReportingDir.mkdirs();
OutputStream os = new FileOutputStream(new File(fReportingDir, aiid + ".xml"));
Element element = doc.getRootElement();
XMLWriter xw = new XMLWriter(os, new OutputFormat(" ", true));
xw.write(element);
// closing steams
xw.close();
os.close();
} catch (Exception e) {
throw new OLATRuntimeException(FilePersister.class, "Error persisting results reporting for subject: '" + subj.getName() + "'; assessment id: '" + aiid + "'", e);
}
}
use of org.dom4j.io.OutputFormat in project xwiki-platform by xwiki.
the class XWikiDocument method toXMLDocument.
/**
* Serialize the document to an XML {@link DOMDocument}. You should prefer
* {@link #toXML(OutputStream, boolean, boolean, boolean, boolean, XWikiContext)} or
* {@link #toXML(com.xpn.xwiki.internal.xml.XMLWriter, boolean, boolean, boolean, boolean, XWikiContext)} when
* possible to reduce memory load.
*
* @param bWithObjects include XObjects
* @param bWithRendering include the rendered content
* @param bWithAttachmentContent include attachments content
* @param bWithVersions include archived versions
* @param context current XWikiContext
* @return a {@link DOMDocument} containing the serialized document.
* @throws XWikiException when an errors occurs during wiki operations
* @deprecated since 9.0RC1, use {@link #toXML(OutputTarget, boolean, boolean, boolean, boolean, boolean, String)}
* instead
*/
@Deprecated
public Document toXMLDocument(boolean bWithObjects, boolean bWithRendering, boolean bWithAttachmentContent, boolean bWithVersions, XWikiContext context) throws XWikiException {
Document doc = new DOMDocument();
DOMXMLWriter wr = new DOMXMLWriter(doc, new OutputFormat("", true, context.getWiki().getEncoding()));
try {
toXML(wr, bWithObjects, bWithRendering, bWithAttachmentContent, bWithVersions, context);
return doc;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Aggregations