Example usage for org.dom4j Element getTextTrim

List of usage examples for org.dom4j Element getTextTrim

Introduction

In this page you can find the example usage for org.dom4j Element getTextTrim.

Prototype

String getTextTrim();

Source Link

Document

DOCUMENT ME!

Usage

From source file:com.haulmont.cuba.desktop.theme.impl.DesktopThemeLoaderImpl.java

License:Apache License

private DesktopStyle loadStyle(Element element) {
    final String componentsSubTag = "components";

    String styleName = element.attributeValue("name");

    List<Class> components = null;
    if (element.attributeValue("component") != null) {
        String className = element.attributeValue("component");
        try {//from w  ww.ja va  2 s.co  m
            components = Collections.singletonList((Class) Class.forName(className));
        } catch (ClassNotFoundException e) {
            log.error("Unknown component class: " + className);
        }
    } else {
        Element componentsElement = element.element(componentsSubTag);
        if (componentsElement != null) {
            String componentsStr = componentsElement.getTextTrim();
            StrTokenizer tokenizer = new StrTokenizer(componentsStr);
            components = new ArrayList<>();
            for (String className : tokenizer.getTokenArray()) {
                try {
                    components.add(Class.forName(className));
                } catch (ClassNotFoundException e) {
                    log.error("Unknown component class: " + className);
                }
            }
        }
    }

    List<ComponentDecorator> decorators = new ArrayList<>();
    for (Element childElement : (List<Element>) element.elements()) {
        if (!componentsSubTag.equals(childElement.getName())) {
            ComponentDecorator decorator = loadDecorator(childElement);
            if (decorator != null) {
                decorators.add(decorator);
            }
        }
    }

    return new DesktopStyle(styleName, decorators, components);
}

From source file:com.haulmont.cuba.gui.components.filter.UserSetHelper.java

License:Apache License

public static String removeEntities(String filterXml, Collection ids) {
    Document document;// w  w w . ja va  2 s .c o  m
    try {
        document = DocumentHelper.parseText(filterXml);
    } catch (DocumentException e) {
        throw new RuntimeException(e);
    }
    Element param = document.getRootElement().element("and").element("c").element("param");
    String currentIds = param.getTextTrim();
    Set<String> set = parseSet(currentIds);
    String listOfIds = removeIds(set, ids);
    param.setText(listOfIds);
    return document.asXML();
}

From source file:com.haulmont.cuba.gui.components.filter.UserSetHelper.java

License:Apache License

public static String addEntities(String filterXml, Collection ids) {
    Document document;//from   w  w  w .j a va2 s  . c o m
    try {
        document = DocumentHelper.parseText(filterXml);
    } catch (DocumentException e) {
        throw new RuntimeException(e);
    }
    Element param = document.getRootElement().element("and").element("c").element("param");
    String currentIds = param.getTextTrim();
    Set<String> set = parseSet(currentIds);
    String listOfIds = createIdsString(set, ids);
    param.setText(listOfIds);
    return document.asXML();
}

From source file:com.haulmont.cuba.gui.xml.data.DsContextLoader.java

License:Apache License

@Nullable
private Class<?> getDatasourceClass(Element element) {
    String datasourceClass = element.attributeValue("datasourceClass");
    if (StringUtils.isEmpty(datasourceClass)) {
        Element datasourceClassElement = element.element("datasourceClass");
        if (datasourceClassElement != null)
            datasourceClass = datasourceClassElement.getTextTrim();
    }//w w  w. j  av  a2  s. c om
    if (StringUtils.isEmpty(datasourceClass)) {
        return null;
    } else {
        Class<?> aClass = scripting.loadClass(datasourceClass);
        if (aClass == null)
            throw new IllegalStateException("Datasource class " + datasourceClass + " is not found");
        return aClass;
    }
}

From source file:com.haulmont.cuba.restapi.XMLConverter2.java

License:Apache License

@Override
public ServiceRequest parseServiceRequest(String content) throws Exception {
    Document document = Dom4j.readDocument(content);
    Element rootElement = document.getRootElement();
    Element serviceEl = rootElement.element("service");
    if (serviceEl == null) {
        throw new IllegalArgumentException("Service name not specified in request");
    }//  w  ww. j a  v a 2  s.c  o  m
    String serviceName = serviceEl.getTextTrim();
    Element methodEl = rootElement.element("method");
    if (methodEl == null) {
        throw new IllegalArgumentException("Method name not specified in request");
    }
    String methodName = methodEl.getTextTrim();
    String viewName = null;
    Element viewEl = rootElement.element("view");
    if (viewEl != null)
        viewName = viewEl.getTextTrim();

    ServiceRequest serviceRequest = new ServiceRequest(serviceName, methodName, this);

    Element paramsEl = rootElement.element("params");
    if (paramsEl != null) {
        int idx = 0;
        while (true) {
            String paramName = "param" + idx;
            Element paramEl = findParamByName(paramsEl, paramName);
            if (paramEl == null)
                break;
            serviceRequest.getParamValuesString().add(paramElementContentAsString(paramEl));

            Element paramTypeEl = findParamByName(paramsEl, paramName + "_type");
            if (paramTypeEl != null) {
                String type = paramTypeEl.getText();
                serviceRequest.getParamTypes().add(ClassUtils.forName(type, null));
            } else {
                if (!serviceRequest.getParamTypes().isEmpty()) {
                    //types should be defined for all parameters or for none of them
                    throw new RestServiceException("Parameter type for param" + idx + " is not defined");
                }
            }
            idx++;
        }
    }

    return serviceRequest;
}

From source file:com.haulmont.cuba.restapi.XMLConverter2.java

License:Apache License

@Override
public QueryRequest parseQueryRequest(String content)
        throws IllegalArgumentException, ClassNotFoundException, ParseException {
    Document document = Dom4j.readDocument(content);
    Element rootElement = document.getRootElement();
    QueryRequest queryRequest = new QueryRequest();

    Element entityElem = rootElement.element("entity");
    String entity = entityElem.getTextTrim();
    queryRequest.setEntity(entity);//ww w  .  ja  v  a 2 s .  co m

    Element queryElem = rootElement.element("query");
    String query = queryElem.getTextTrim();
    queryRequest.setQuery(query);

    if (rootElement.element("view") != null) {
        Element viewElem = rootElement.element("view");
        String view = viewElem.getTextTrim();
        queryRequest.setViewName(view);
    }

    if (rootElement.element("max") != null) {
        Element maxElem = rootElement.element("max");
        String maxString = maxElem.getTextTrim();
        int max = Integer.parseInt(maxString);
        queryRequest.setMax(max);
    }

    if (rootElement.element("first") != null) {
        Element firstElem = rootElement.element("first");
        String firstString = firstElem.getTextTrim();
        int first = Integer.parseInt(firstString);
        queryRequest.setFirst(first);
    }

    if (rootElement.element("dynamicAttributes") != null) {
        Element dynamicAttributesElem = rootElement.element("dynamicAttributes");
        String dynamicAttributesString = dynamicAttributesElem.getTextTrim();
        Boolean dynamicAttributes = Boolean.valueOf(dynamicAttributesString);
        queryRequest.setDynamicAttributes(dynamicAttributes);
    }

    if (rootElement.element("params") != null) {
        Element paramsElem = rootElement.element("params");
        List paramList = paramsElem.elements("param");
        for (Object obj : paramList) {
            if (obj instanceof Element) {
                Element paramElem = (Element) obj;

                Element nameElem = paramElem.element("name");
                String paramName = nameElem.getStringValue();

                Element valueElem = paramElem.element("value");
                String paramValue = valueElem.getStringValue();

                Object value = null;
                if (paramElem.element("type") != null) {
                    Element typeElem = paramElem.element("type");
                    String typeString = typeElem.getStringValue();
                    Class type = ClassUtils.forName(typeString, null);
                    value = ParseUtils.toObject(type, paramValue, this);
                } else {
                    value = ParseUtils.tryParse(paramValue);
                }

                queryRequest.getParams().put(paramName, value != null ? value : paramValue);
            }
        }
    }

    return queryRequest;
}

From source file:com.heren.turtle.gear.meta.AbstractService.java

License:Open Source License

protected Map<String, Object> getMessage(String message) throws DocumentException {
    Document document = DocumentHelper.parseText(message);
    Element rootElement = document.getRootElement();
    List elements = rootElement.elements();
    Map<String, Object> result = new HashMap<String, Object>();
    for (Iterator it = elements.iterator(); it.hasNext();) {
        Element subElement = (Element) it.next();
        if (subElement.isTextOnly()) {
            result.put(subElement.getName(), subElement.getTextTrim());
        } else {/*from ww w.  j a  v a 2 s  .c o  m*/
            List<String> subList = new ArrayList<String>();
            List subEle = subElement.elements();
            for (Iterator iterator = subEle.iterator(); iterator.hasNext();) {
                Element itemElements = (Element) iterator.next();
                subList.add(itemElements.getTextTrim());
            }
            result.put(subElement.getName(), subList);
        }
    }
    return result;
}

From source file:com.heren.turtle.server.utils.XmlUtils.java

License:Open Source License

/**
 * delete the label of xml that is not use
 *
 * @param message// ww  w. j  a va2  s . co  m
 * @return
 */
public static Map<String, Object> getMessage(String message) throws DocumentException {
    message = replaceWrongPart(message);
    Document document = DocumentHelper.parseText(message);
    Element rootElement = document.getRootElement();
    Element request = rootElement.element("request");
    List elements = request.elements();
    Map<String, Object> result = new HashMap<>();
    for (Object element : elements) {
        Element subElement = (Element) element;
        if (subElement.isTextOnly()) {
            result.put(subElement.getName(), subElement.getTextTrim());
        } else {
            List<String> subList = new ArrayList<>();
            List subEle = subElement.elements();
            for (Object aSubEle : subEle) {
                Element itemElements = (Element) aSubEle;
                subList.add(itemElements.getTextTrim());
            }
            result.put(subElement.getName(), subList);
        }
    }
    return result;
}

From source file:com.heren.turtle.server.utils.XmlUtils.java

License:Open Source License

public static Map<String, Object> getMessageReceiveSample(String message) throws DocumentException {
    message = replaceWrongPart(message);
    Document document = DocumentHelper.parseText(message);
    Element rootElement = document.getRootElement();
    Element request = rootElement.element("request");
    List elements = request.elements();
    Map<String, Object> result = new HashMap<>();
    for (Object element : elements) {
        Element subElement = (Element) element;
        String name = subElement.getName();
        if (name.equalsIgnoreCase("item")) {
            List subElements = subElement.elements();
            for (Object subEle : subElements) {
                Element grantSubElement = (Element) subEle;
                result.put(grantSubElement.getName(), grantSubElement.getTextTrim());
            }/*w w w .  ja v a 2s  .c om*/
        } else if (subElement.isTextOnly()) {
            result.put(name, subElement.getTextTrim());
        }
    }
    return result;
}

From source file:com.heren.turtle.server.utils.XmlUtils.java

License:Open Source License

public static List<Map<String, Object>> getMessageReceiveSampleContainItems(String message)
        throws DocumentException {
    message = replaceWrongPart(message);
    Document document = DocumentHelper.parseText(message);
    Element rootElement = document.getRootElement();
    Element request = rootElement.element("request");
    Element items = request.element("items");
    List elements = items.elements();
    List<Map<String, Object>> resultList = new ArrayList<>();
    for (Object element : elements) {
        Map<String, Object> result = new HashMap<>();
        Element subElement = (Element) element;
        String name = subElement.getName();
        if (name.equalsIgnoreCase("item")) {
            List subElements = subElement.elements();
            for (Object subEle : subElements) {
                Element grantSubElement = (Element) subEle;
                result.put(grantSubElement.getName(), grantSubElement.getTextTrim());
            }/*from w  ww .ja  va  2  s .c om*/
        } else if (subElement.isTextOnly()) {
            result.put(name, subElement.getTextTrim());
        }
        resultList.add(result);
    }
    return resultList;
}