use of org.dom4j.Document in project cuba by cuba-platform.
the class XMLConverter2 method processServiceMethodResult.
@Override
@Nonnull
public String processServiceMethodResult(Object result, Class resultType) throws Exception {
Document document = DocumentHelper.createDocument();
Element resultEl = document.addElement("result");
if (result instanceof Entity) {
Entity entity = (Entity) result;
Document convertedEntity = _process(entity, null);
resultEl.add(convertedEntity.getRootElement());
} else if (result instanceof Collection) {
if (!checkCollectionItemTypes((Collection) result, Entity.class))
throw new IllegalArgumentException("Items that are not instances of Entity class found in service method result");
ArrayList list = new ArrayList((Collection) result);
MetaClass metaClass = null;
if (!list.isEmpty())
metaClass = ((Entity) list.get(0)).getMetaClass();
Document processed = _process(list, metaClass, null);
resultEl.add(processed.getRootElement());
} else {
if (result != null && resultType != Void.TYPE) {
Datatype datatype = getDatatype(resultType);
resultEl.setText(datatype != null ? datatype.format(result) : result.toString());
} else {
encodeNull(resultEl);
}
}
return documentToString(document);
}
use of org.dom4j.Document in project cuba by cuba-platform.
the class XMLConverter2 method parseEntity.
@Override
public Entity parseEntity(String content) {
Document document = Dom4j.readDocument(content);
Element instanceEl = document.getRootElement();
return parseEntity(instanceEl, null, null);
}
use of org.dom4j.Document in project cuba by cuba-platform.
the class MetadataBuildSupport method readXml.
protected Element readXml(String path) {
InputStream stream = resources.getResourceAsStream(path);
try {
stream = resources.getResourceAsStream(path);
if (stream == null)
throw new IllegalStateException("Resource not found: " + path);
Document document = Dom4j.readDocument(stream);
return document.getRootElement();
} finally {
IOUtils.closeQuietly(stream);
}
}
use of org.dom4j.Document in project dq-easy-cloud by dq-open-cloud.
the class Dom4jTest method load.
public static Document load(String filename) {
Document document = null;
try {
SAXReader saxReader = new SAXReader();
// 读取XML文件,获得document对象
document = saxReader.read(new File(filename));
} catch (Exception ex) {
ex.printStackTrace();
}
return document;
}
use of org.dom4j.Document in project dq-easy-cloud by dq-open-cloud.
the class Dom4jTest method testAdd.
@Test
public void testAdd() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File(path));
Element resultMap = document.getRootElement().element("resultMap");
List list = resultMap.elements();
// 设置新增的person的信息
Element newEle = DocumentHelper.createElement("result");
newEle.addAttribute("column", "update_by");
newEle.addAttribute("jdbcType", "VARCHAR");
newEle.addAttribute("property", "updateBy");
list.add(2, newEle);
OutputFormat format = OutputFormat.createPrettyPrint();
format = new OutputFormat();
format.setIndentSize(4);
format.setNewlines(true);
format.setTrimText(true);
format.setPadText(true);
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(path)), format);
writer.write(document);
writer.close();
}
Aggregations