Search in sources :

Example 31 with Document

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);
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass) Element(org.dom4j.Element) Document(org.dom4j.Document) Datatype(com.haulmont.chile.core.datatypes.Datatype) Nonnull(javax.annotation.Nonnull)

Example 32 with 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);
}
Also used : Element(org.dom4j.Element) Document(org.dom4j.Document)

Example 33 with Document

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);
    }
}
Also used : InputStream(java.io.InputStream) Document(org.dom4j.Document)

Example 34 with Document

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;
}
Also used : SAXReader(org.dom4j.io.SAXReader) Document(org.dom4j.Document) File(java.io.File) DocumentException(org.dom4j.DocumentException)

Example 35 with 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();
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) FileOutputStream(java.io.FileOutputStream) OutputFormat(org.dom4j.io.OutputFormat) List(java.util.List) OutputStreamWriter(java.io.OutputStreamWriter) Document(org.dom4j.Document) File(java.io.File) XMLWriter(org.dom4j.io.XMLWriter) Test(org.junit.Test)

Aggregations

Document (org.dom4j.Document)891 Element (org.dom4j.Element)492 SAXReader (org.dom4j.io.SAXReader)252 File (java.io.File)135 IOException (java.io.IOException)135 StringReader (java.io.StringReader)111 ArrayList (java.util.ArrayList)110 List (java.util.List)107 Test (org.junit.Test)101 DocumentException (org.dom4j.DocumentException)93 HashMap (java.util.HashMap)90 InputStream (java.io.InputStream)82 Node (org.dom4j.Node)80 Test (org.junit.jupiter.api.Test)80 XMLWriter (org.dom4j.io.XMLWriter)53 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)48 FileInputStream (java.io.FileInputStream)45 Map (java.util.Map)41 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)40 XMLParser (org.olat.core.util.xml.XMLParser)40