Search in sources :

Example 26 with Attribute

use of org.dom4j.Attribute in project OpenOLAT by OpenOLAT.

the class OutcomesProcessingParser method parse.

/**
 * @see org.olat.ims.qti.editor.beecom.IParser#parse(org.dom4j.Element)
 */
public Object parse(Element element) {
    // assert element.getName().equalsIgnoreCase("outcomes_processing");
    OutcomesProcessing outcomesProcessing = new OutcomesProcessing();
    List decvars = element.selectNodes("*/decvar");
    if (decvars.size() == 0)
        return outcomesProcessing;
    Element decvar = (Element) decvars.get(0);
    for (Iterator iter = decvar.attributeIterator(); iter.hasNext(); ) {
        Attribute attr = (Attribute) iter.next();
        outcomesProcessing.setField(attr.getName(), attr.getValue());
    }
    return outcomesProcessing;
}
Also used : Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) OutcomesProcessing(org.olat.ims.qti.editor.beecom.objects.OutcomesProcessing) Iterator(java.util.Iterator) List(java.util.List)

Example 27 with Attribute

use of org.dom4j.Attribute in project tdq-studio-se by Talend.

the class XmlPreviewer method createControls.

public void createControls(Composite parent, final Object data) throws ExplorerException {
    Element rootElem = getXml(data);
    if (rootElem == null)
        return;
    final Object[] root = new Object[] { rootElem };
    TreeViewer tree = new TreeViewer(parent, SWT.SINGLE);
    tree.setContentProvider(new ITreeContentProvider() {

        public void dispose() {
        }

        /**
         * Called to get the top level items
         */
        public Object[] getChildren(Object parentElement) {
            return root;
        }

        /**
         * Called to get the item's children
         */
        public Object[] getElements(Object inputElement) {
            Element elem = (Element) inputElement;
            return elem.elements().toArray();
        }

        public boolean hasChildren(Object element) {
            Element elem = (Element) element;
            List<Element> list = elem.elements();
            return list != null && list.size() > 0;
        }

        public Object getParent(Object element) {
            Element elem = (Element) element;
            return elem.getParent();
        }

        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        // Nothing
        }
    });
    tree.setLabelProvider(new LabelProvider() {

        public String getText(Object obj) {
            Element elem = (Element) obj;
            StringBuffer result = new StringBuffer();
            result.append('<');
            result.append(elem.getName());
            for (Attribute attr : elem.attributes()) {
                result.append(' ').append(attr.getName()).append('=').append('\"').append(attr.getValue()).append('\"');
            }
            if (!elem.hasContent())
                result.append('/');
            result.append('>');
            return result.toString();
        }
    });
    tree.expandToLevel(1);
}
Also used : ITreeContentProvider(org.eclipse.jface.viewers.ITreeContentProvider) Attribute(org.dom4j.Attribute) TreeViewer(org.eclipse.jface.viewers.TreeViewer) Element(org.dom4j.Element) List(java.util.List) Viewer(org.eclipse.jface.viewers.Viewer) TreeViewer(org.eclipse.jface.viewers.TreeViewer) LabelProvider(org.eclipse.jface.viewers.LabelProvider)

Example 28 with Attribute

use of org.dom4j.Attribute in project sagacity-sqltoy by chenrenfei.

the class XMLUtil method setAttributes.

/**
 * @todo 解析xml元素的属性映射到java对象属性
 * @param elt
 * @param entity
 * @param mapped
 */
public static void setAttributes(Element elt, Serializable entity, String... keyValues) throws Exception {
    if (elt == null)
        return;
    HashMap<String, String> realMap = asMap(keyValues);
    List<Attribute> attrs = elt.attributes();
    String name;
    String value;
    String[] properties = new String[attrs.size() + 1];
    String[] values = new String[attrs.size() + 1];
    int index = 0;
    for (Attribute attr : attrs) {
        name = attr.getName();
        value = attr.getValue();
        // 对照属性
        if (realMap.containsKey(name))
            properties[index] = realMap.get(name);
        else if (realMap.containsKey(name.toLowerCase()))
            properties[index] = realMap.get(name.toLowerCase());
        else if (realMap.containsKey(name.toUpperCase()))
            properties[index] = realMap.get(name.toUpperCase());
        else
            properties[index] = StringUtil.toHumpStr(name, false);
        values[index] = value;
        index++;
    }
    // 将元素body中的值作为元素自身
    name = elt.getName();
    if (realMap.containsKey(name))
        properties[index] = realMap.get(name);
    else if (realMap.containsKey(name.toLowerCase()))
        properties[index] = realMap.get(name.toLowerCase());
    else if (realMap.containsKey(name.toUpperCase()))
        properties[index] = realMap.get(name.toUpperCase());
    else
        properties[index] = StringUtil.toHumpStr(name, false);
    values[index] = elt.getText();
    Method[] realMethods = BeanUtil.matchSetMethods(entity.getClass(), properties);
    Method method;
    Class argType;
    String className;
    for (int i = 0; i < properties.length; i++) {
        // 属性值为空白的排除掉
        if (StringUtil.isNotBlank(values[i])) {
            method = realMethods[i];
            if (method != null) {
                try {
                    argType = method.getParameterTypes()[0];
                    className = argType.getTypeName();
                    className = className.substring(className.lastIndexOf(".") + 1);
                    if (argType.isArray()) {
                        String[] args = values[i].split(",");
                        className = className.substring(0, className.indexOf("["));
                        if (className.equals("int")) {
                            int[] arrayData = new int[args.length];
                            for (int j = 0; j < arrayData.length; j++) {
                                arrayData[j] = Integer.parseInt(args[j]);
                            }
                            method.invoke(entity, arrayData);
                        } else if (className.equals("long")) {
                            long[] arrayData = new long[args.length];
                            for (int j = 0; j < arrayData.length; j++) {
                                arrayData[j] = Long.parseLong(args[j]);
                            }
                            method.invoke(entity, arrayData);
                        } else if (className.equals("float")) {
                            float[] arrayData = new float[args.length];
                            for (int j = 0; j < arrayData.length; j++) {
                                arrayData[j] = Float.parseFloat(args[j]);
                            }
                            method.invoke(entity, arrayData);
                        } else if (className.equals("double")) {
                            double[] arrayData = new double[args.length];
                            for (int j = 0; j < arrayData.length; j++) {
                                arrayData[j] = Double.parseDouble(args[j]);
                            }
                            method.invoke(entity, arrayData);
                        } else if (className.equals("short")) {
                            short[] arrayData = new short[args.length];
                            for (int j = 0; j < arrayData.length; j++) {
                                arrayData[j] = Short.parseShort(args[j]);
                            }
                            method.invoke(entity, arrayData);
                        } else if (className.equals("boolean")) {
                            boolean[] arrayData = new boolean[args.length];
                            for (int j = 0; j < arrayData.length; j++) {
                                arrayData[j] = Boolean.parseBoolean(args[j]);
                            }
                            method.invoke(entity, arrayData);
                        } else {
                            Object valueAry = CollectionUtil.toArray(args, className.toLowerCase());
                            method.invoke(entity, valueAry);
                        }
                    } else if (BeanUtil.isBaseDataType(argType)) {
                        method.invoke(entity, BeanUtil.convertType(values[i], className.toLowerCase()));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    throw e;
                }
            }
        }
    }
}
Also used : Attribute(org.dom4j.Attribute) Method(java.lang.reflect.Method) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 29 with Attribute

use of org.dom4j.Attribute in project core by craftercms.

the class MergeCueResolverImpl method getMergeCue.

@Override
public MergeCueContext getMergeCue(Element parent, Element child) {
    MergeCue parentMergeCue;
    MergeCue childMergeCue;
    Attribute parentMergeCueAttribute = getMergeCueAttribute(parent, parentMergeCues);
    if (parentMergeCueAttribute != null) {
        parentMergeCue = parentMergeCues.get(parentMergeCueAttribute.getQName());
    } else {
        parentMergeCue = defaultParentMergeCue;
    }
    Attribute childMergeCueAttribute = getMergeCueAttribute(child, childMergeCues);
    if (childMergeCueAttribute != null) {
        childMergeCue = childMergeCues.get(childMergeCueAttribute.getQName());
    } else {
        childMergeCue = defaultChildMergeCue;
    }
    MergeCue chosenMergeCue;
    Map<String, String> mergeCueParams;
    if (parentMergeCue.getPriority() > childMergeCue.getPriority()) {
        chosenMergeCue = parentMergeCue;
        if (parentMergeCueAttribute != null) {
            mergeCueParams = getMergeCueParams(parent, parentMergeCueAttribute);
        } else {
            mergeCueParams = Collections.emptyMap();
        }
    } else {
        chosenMergeCue = childMergeCue;
        if (childMergeCueAttribute != null) {
            mergeCueParams = getMergeCueParams(child, childMergeCueAttribute);
        } else {
            mergeCueParams = Collections.emptyMap();
        }
    }
    return new MergeCueContext(chosenMergeCue, parent, child, mergeCueParams);
}
Also used : Attribute(org.dom4j.Attribute) MergeCueContext(org.craftercms.core.xml.mergers.impl.cues.MergeCueContext) MergeCue(org.craftercms.core.xml.mergers.impl.cues.MergeCue)

Example 30 with Attribute

use of org.dom4j.Attribute in project spring-framework by spring-projects.

the class SelectTagTests method validateOutput.

private void validateOutput(String output, boolean selected) throws DocumentException {
    SAXReader reader = new SAXReader();
    Document document = reader.read(new StringReader(output));
    Element rootElement = document.getRootElement();
    assertThat(rootElement.getName()).isEqualTo("select");
    assertThat(rootElement.attribute("name").getValue()).isEqualTo("country");
    List children = rootElement.elements();
    assertThat(children.size()).as("Incorrect number of children").isEqualTo(4);
    Element e = (Element) rootElement.selectSingleNode("option[@value = 'UK']");
    Attribute selectedAttr = e.attribute("selected");
    if (selected) {
        assertThat(selectedAttr != null && "selected".equals(selectedAttr.getValue())).isTrue();
    } else {
        assertThat(selectedAttr).isNull();
    }
}
Also used : Attribute(org.dom4j.Attribute) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.dom4j.Document)

Aggregations

Attribute (org.dom4j.Attribute)114 Element (org.dom4j.Element)88 Document (org.dom4j.Document)30 ArrayList (java.util.ArrayList)28 List (java.util.List)26 Iterator (java.util.Iterator)23 Node (org.dom4j.Node)14 HashMap (java.util.HashMap)12 File (java.io.File)11 SAXReader (org.dom4j.io.SAXReader)11 IOException (java.io.IOException)10 Map (java.util.Map)6 VFSItem (org.olat.core.util.vfs.VFSItem)6 QTIObject (org.olat.ims.qti.editor.beecom.objects.QTIObject)6 Namespace (org.dom4j.Namespace)5 QName (org.dom4j.QName)5 PropertyString (org.pentaho.commons.util.repository.type.PropertyString)5 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)4 OutcomesProcessing (org.olat.ims.qti.editor.beecom.objects.OutcomesProcessing)4 AnnotatedElement (java.lang.reflect.AnnotatedElement)3