Example usage for org.dom4j Element attributes

List of usage examples for org.dom4j Element attributes

Introduction

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

Prototype

List<Attribute> attributes();

Source Link

Document

Returns the Attribute instances this element contains as a backed List so that the attributes may be modified directly using the List interface.

Usage

From source file:com.rdvmedecin.proprietes.Properties.java

public Properties() throws DocumentException {
    File fichier = new File("configuration.xml");
    SAXReader reader = new SAXReader();
    Document doc = reader.read(fichier);
    Element root = doc.getRootElement();
    List attributes = root.attributes();
    List elements = root.elements();
    QName qName = root.getQName();
    String nom = qName.getName();
    // lecture du nom de l'espace de noms
    String nomEspaceDeNoms = qName.getNamespaceURI();
    // lecture du prfixe utilis pour cet espace de nom 
    String nomPrefix = qName.getNamespacePrefix();
    String texte = root.getText();
    Attribute attribute = (Attribute) attributes.iterator().next();
    QName attributeQName = attribute.getQName();
    String value = attribute.getValue();
    String nomAttribut = attribute.getName();
}

From source file:com.safi.workshop.sqlexplorer.preview.XmlPreviewer.java

License:Open Source License

public void createControls(Composite parent, final Object data) throws ExplorerException {
    Element rootElem = getXml(data);
    if (rootElem == null)
        return;//www . ja v a2s .  c  o m
    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() {
        @Override
        public String getText(Object obj) {
            Element elem = (Element) obj;
            StringBuffer result = new StringBuffer();
            result.append('<');
            result.append(elem.getName());

            for (Object o : elem.attributes()) {
                Attribute attr = (Attribute) o;
                result.append(' ').append(attr.getName()).append('=').append('\"').append(attr.getValue())
                        .append('\"');
            }
            if (!elem.hasContent())
                result.append('/');
            result.append('>');
            return result.toString();
        }
    });

    tree.expandToLevel(1);
}

From source file:com.seer.datacruncher.utils.generic.StreamsUtils.java

License:Open Source License

/**
 * Publishes stream to destination database.
 * /*from   w  w w.jav a 2s.co m*/
 * @param datastreamDTO - keeps stream info
 */
public static void publishStreamToDB(DatastreamDTO datastreamDTO) {
    Long schemaId = datastreamDTO.getIdSchema();
    QuickDBRecognizer quickDBRecognizer = new QuickDBRecognizer(schemaId);
    log.debug("quickDBRecognizer started");
    try {
        List<Element> xmlTextNodes = parseStreamXml(datastreamDTO.getOutput());
        StreamToDbDynamicObject streamObj = quickDBRecognizer.traceDefine(xmlTextNodes);
        Map<String, Map<String, String>> schemaFields = streamObj.getInsertedFields();
        //boolean isLoadedFields = streamObj.isLoadedFields();
        Object toPersist = streamObj.getObject();
        for (Element el : xmlTextNodes) {
            String fieldPath = formatPathForXmlNode(el.getPath());
            String fieldValue = el.getText();
            for (Map.Entry<String, Map<String, String>> entry : schemaFields.entrySet()) {
                String entryFieldPath = getFieldPathPrefix(datastreamDTO.getIdStreamType())
                        + entry.getValue().get("path");
                if (entry.getValue().get(IS_ATTR).equals("true")) {
                    for (Object o : el.attributes()) {
                        DefaultAttribute attr = (DefaultAttribute) o;
                        String attrPath = fieldPath + "/" + attr.getName().toUpperCase();
                        if (attrPath.equals(entryFieldPath)) {
                            fieldPath = attrPath;
                            fieldValue = attr.getValue();
                            break;
                        }
                    }
                }
                if (entryFieldPath.equals(fieldPath)) {
                    String fieldJavaType = entry.getValue().get("type");
                    Method meth = toPersist.getClass().getMethod("set" + entry.getKey(),
                            Class.forName(fieldJavaType));
                    if (fieldJavaType.contains("String")) {
                        meth.invoke(toPersist, fieldValue);
                    } else if (fieldJavaType.contains("Long")) {
                        meth.invoke(toPersist, Long.valueOf(fieldValue));
                    } else if (fieldJavaType.contains("Double")) {
                        meth.invoke(toPersist, Double.valueOf(fieldValue));
                    } else if (fieldJavaType.contains("Integer")) {
                        meth.invoke(toPersist, Integer.valueOf(fieldValue));
                    } else if (fieldJavaType.contains("Date")) {
                        //never invoked because now all dates are of string type
                        meth.invoke(toPersist, new Date());
                    }
                }
            }
        }
        toPersist.getClass().getMethod("setPath", String.class).invoke(toPersist, getAllPaths(schemaFields));
        int inserito = quickDBRecognizer.insertTrace(streamObj);
        if (inserito > 0) {
            log.info("Stream published successfully");
        }
    } catch (ReflectionException ex) {
        log.error(ex);
    } catch (ClassNotFoundException ex) {
        log.error(ex);
    } catch (IOException ex) {
        log.error(ex);
    } catch (InstantiationException ex) {
        log.error(ex);
    } catch (IllegalAccessException ex) {
        log.error(ex);
    } catch (SecurityException ex) {
        log.error(ex);
    } catch (CannotCompileException ex) {
        log.error(ex);
    } catch (NoSuchFieldException ex) {
        log.error(ex);
    } catch (NoSuchMethodException ex) {
        log.error(ex);
    } catch (InvocationTargetException ex) {
        log.error(ex);
    } catch (NotFoundException ex) {
        log.error(ex);
    } catch (DocumentException e) {
        log.error("Stream parse exception", e);
    }
}

From source file:com.taobao.android.builder.tasks.app.manifest.StandardizeLibManifestTask.java

License:Apache License

/**
 * ?mainifest//from w  w  w  . j a  v  a  2s.co m
 *
 * @param manifestFile
 * @return
 */
public static ManifestInfo getManifestFileObject(File manifestFile) throws DocumentException {

    ManifestInfo manifestFileObject = new ManifestInfo();
    manifestFileObject.setManifestFile(manifestFile);
    if (manifestFile.exists()) {
        Document document = XmlHelper.readXml(manifestFile);// ?XML
        Element root = document.getRootElement();// 
        for (Attribute attribute : root.attributes()) {
            if (StringUtils.isNotBlank(attribute.getNamespacePrefix())) {
                manifestFileObject.addManifestProperty(
                        attribute.getNamespacePrefix() + ":" + attribute.getName(), attribute.getValue());
            } else {
                manifestFileObject.addManifestProperty(attribute.getName(), attribute.getValue());
            }
        }
        Element useSdkElement = root.element("uses-sdk");
        Element applicationElement = root.element("application");
        if (null != useSdkElement) {
            for (Attribute attribute : useSdkElement.attributes()) {
                if (StringUtils.isNotBlank(attribute.getNamespacePrefix())) {
                    manifestFileObject.addUseSdkProperty(
                            attribute.getNamespacePrefix() + ":" + attribute.getName(), attribute.getValue());
                } else {
                    manifestFileObject.addUseSdkProperty(attribute.getName(), attribute.getValue());
                }
            }
        }
        if (null != applicationElement) {
            for (Attribute attribute : applicationElement.attributes()) {
                if (StringUtils.isNotBlank(attribute.getNamespacePrefix())) {
                    manifestFileObject.addApplicationProperty(
                            attribute.getNamespacePrefix() + ":" + attribute.getName(), attribute.getValue());
                } else {
                    manifestFileObject.addApplicationProperty(attribute.getName(), attribute.getValue());
                }
            }
        }
    }
    manifestFileObject.init();
    return manifestFileObject;
}

From source file:com.taobao.android.builder.tasks.tpatch.DiffBundleInfoTask.java

License:Apache License

private static ArtifactBundleInfo getMainArtifactBundInfo(File manifestFile) {
    ArtifactBundleInfo mainBundleInfo = new ArtifactBundleInfo();
    SAXReader reader = new SAXReader();
    Document document = null;// ?XML
    try {/*from w  ww .j  a  va 2s  .  c  o  m*/
        document = reader.read(manifestFile);
    } catch (DocumentException e) {
        throw new GradleException(e.getMessage(), e);
    }
    Element root = document.getRootElement();// 

    List<? extends Node> metadataNodes = root.selectNodes("//meta-data");
    for (Node node : metadataNodes) {
        Element element = (Element) node;
        Attribute attribute = element.attribute("name");
        if (attribute.getValue().equals("label")) {
            Attribute labelAttribute = element.attribute("value");
            mainBundleInfo.setName(labelAttribute.getValue());
        }
    }

    List<? extends Node> applicatNodes = root.selectNodes("//application");
    for (Node node : applicatNodes) {
        Element element = (Element) node;
        Attribute attribute = element.attribute("name");
        if (attribute != null) {
            mainBundleInfo.setApplicationName(attribute.getValue());
        }
    }

    if ("manifest".equalsIgnoreCase(root.getName())) {
        List<Attribute> attributes = root.attributes();
        for (Attribute attr : attributes) {
            if (StringUtils.equalsIgnoreCase(attr.getName(), "versionName")) {
                String versionName = attr.getValue();
                mainBundleInfo.setVersion(versionName);
            }
        }
    }
    String pkgName = root.attributeValue("package");
    mainBundleInfo.setPkgName(pkgName);
    return mainBundleInfo;
}

From source file:com.taobao.android.builder.tools.manifest.ManifestFileUtils.java

License:Apache License

/**
 * libManifest//from  w w w.  java  2  s  .c o  m
 *
 * @param libManifestFile
 * @param mainManifestFileObject param updateSdkVersion
 */
public static void updatePreProcessManifestFile(File libManifestFile, ManifestFileObject mainManifestFileObject,
        boolean updateSdkVersion) throws IOException, DocumentException {

    if (!libManifestFile.exists()) {
        return;
    }

    File orgManifestFile = new File(libManifestFile.getParentFile(), "AndroidManifest-org.xml");
    if (orgManifestFile.exists()) {
        return;
    }

    libManifestFile.renameTo(orgManifestFile);

    SAXReader reader = new SAXReader();
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");// XML??

    Document document = reader.read(orgManifestFile);// ?XML
    Element root = document.getRootElement();// 
    if (updateSdkVersion) {
        Element useSdkElement = root.element("uses-sdk");

        if (null == useSdkElement) {
            useSdkElement = root.addElement("uses-sdk");
        }

        if (null != useSdkElement) {
            updateElement(useSdkElement, mainManifestFileObject.getUseSdkProperties());
        }
    }

    // ?tools:removetools:replace?ManifestMerge??
    Element applicationElement = root.element("application");
    Map<String, String> replaceAttrs = mainManifestFileObject.getReplaceApplicationAttribute();
    List<String> removeAttrs = mainManifestFileObject.getRemoveApplicationAttribute();

    if (null != applicationElement) {
        // libtools
        List<Attribute> toRomoved = new ArrayList<Attribute>();
        for (Attribute attribute : applicationElement.attributes()) {
            if (attribute.getName().equals("replace") || attribute.getName().equals("remove")) {
                // applicationElement.remove(attribute);
                // applicationElement.attributes().remove(attribute);
                toRomoved.add(attribute);
            }
        }
        if (toRomoved.size() > 0) {
            for (Attribute attribute : toRomoved) {
                applicationElement.remove(attribute);
            }
        }

        updateApplicationElement(applicationElement, replaceAttrs, removeAttrs);
    }

    //?packageName TODO
    String packageName = root.attributeValue("package");
    if (StringUtils.isEmpty(packageName)) {
        packageName = ManifestFileUtils.getPackage(orgManifestFile);
    }

    List<? extends Node> applicatNodes = root.selectNodes("//application");
    for (Node node : applicatNodes) {
        Element element = (Element) node;
        Attribute attribute = element.attribute("name");
        if (attribute != null) {
            if (!attribute.getValue().startsWith(packageName)) {
                attribute.setValue(packageName + attribute.getValue());
            }
        }
    }

    fillFullClazzName(root, packageName, "activity");
    fillFullClazzName(root, packageName, "provider");
    fillFullClazzName(root, packageName, "receiver");
    fillFullClazzName(root, packageName, "service");

    saveFile(document, format, libManifestFile);
}

From source file:com.taobao.android.builder.tools.manifest.ManifestFileUtils.java

License:Apache License

/**
 * ?mainifest// w w w. java 2  s . c om
 *
 * @param manifestFile
 * @return
 */
public static ManifestFileObject getManifestFileObject(File manifestFile) throws DocumentException {
    SAXReader reader = new SAXReader();
    ManifestFileObject manifestFileObject = new ManifestFileObject();
    manifestFileObject.setManifestFile(manifestFile);
    if (manifestFile.exists()) {
        Document document = reader.read(manifestFile);// ?XML
        Element root = document.getRootElement();// 
        for (Attribute attribute : root.attributes()) {
            if (StringUtils.isNotBlank(attribute.getNamespacePrefix())) {
                manifestFileObject.addManifestProperty(
                        attribute.getNamespacePrefix() + ":" + attribute.getName(), attribute.getValue());
            } else {
                manifestFileObject.addManifestProperty(attribute.getName(), attribute.getValue());
            }
        }
        Element useSdkElement = root.element("uses-sdk");
        Element applicationElement = root.element("application");
        if (null != useSdkElement) {
            for (Attribute attribute : useSdkElement.attributes()) {
                if (StringUtils.isNotBlank(attribute.getNamespacePrefix())) {
                    manifestFileObject.addUseSdkProperty(
                            attribute.getNamespacePrefix() + ":" + attribute.getName(), attribute.getValue());
                } else {
                    manifestFileObject.addUseSdkProperty(attribute.getName(), attribute.getValue());
                }
            }
        }
        if (null != applicationElement) {
            for (Attribute attribute : applicationElement.attributes()) {
                if (StringUtils.isNotBlank(attribute.getNamespacePrefix())) {
                    manifestFileObject.addApplicationProperty(
                            attribute.getNamespacePrefix() + ":" + attribute.getName(), attribute.getValue());
                } else {
                    manifestFileObject.addApplicationProperty(attribute.getName(), attribute.getValue());
                }
            }
        }
    }
    return manifestFileObject;
}

From source file:com.taobao.android.builder.tools.manifest.ManifestFileUtils.java

License:Apache License

/**
 * ?minSdkVersiontargetSdkVersion/*ww w.j  av a 2 s  .  c o  m*/
 *
 * @param androidManifestFile
 * @throws IOException
 * @throws DocumentException
 */
public static String getVersionName(File androidManifestFile) throws IOException, DocumentException {
    SAXReader reader = new SAXReader();
    String versionName = "";
    if (androidManifestFile.exists()) {
        Document document = reader.read(androidManifestFile);// ?XML
        Element root = document.getRootElement();// 
        if ("manifest".equalsIgnoreCase(root.getName())) {
            List<Attribute> attributes = root.attributes();
            for (Attribute attr : attributes) {
                if (StringUtils.equalsIgnoreCase(attr.getName(), "versionName")) {
                    versionName = attr.getValue();
                }
            }
        }
    }
    return versionName;
}

From source file:com.tedi.engine.XMLOutput.java

License:Open Source License

/**
 * Clean the element./*from  w w w  . j a  v a2  s. co  m*/
 * 
 * @param ele
 *            The element.
 */
private void cleanElement(Element ele) {

    if (logger.isDebugEnabled()) {
        logger.debug("Cleaning the element " + ele);
    }
    // Attribute check shouldn't be necessary since only trying to write
    // populated attributes
    // but leave for now just in case
    for (Iterator i = ele.attributes().iterator(); i.hasNext();) {
        Attribute a = (Attribute) i.next();
        if (!mapFile.isSuppressAttribIfEmpty() || a.getText().length() > 0)
            continue;
        if (!mapFile.isSuppressAttribIfHasOnlyWhitespace() || a.getText().trim().length() > 0)
            continue;
        ele.remove(a);
    }
    for (Iterator i = ele.elements().iterator(); i.hasNext();) {
        cleanElement((Element) i.next());
    }
    if (!mapFile.isSuppressElementIfHasNoChildren() || ele.elements().size() > 0)
        return;
    if (!mapFile.isSuppressElementIfHasOnlyEmptyAttribs() || ele.attributes().size() > 0)
        return;
    if (!mapFile.isSuppressElementIfEmpty() || ele.getText().length() > 0)
        return;
    if (!mapFile.isSuppressElementIfHasOnlyWhitespace() || ele.getTextTrim().length() > 0)
        return;
    ele.getParent().remove(ele);
}

From source file:com.testmax.framework.BasePage.java

License:CDDL license

protected void setupPage(Element testdata, PageURL page) {
    this.actionResult = new HashMap<String, Integer>();
    this.pageUrl = page;
    this.testdata = testdata;
    this.globalExecution = "";
    List attributes = testdata.attributes();

    for (Object seter : attributes.toArray()) {
        Attribute el = (Attribute) seter;
        String attrName = el.getName();
        if (attrName.equalsIgnoreCase("threads")) {
            this.setThreadCount(Integer.valueOf(testdata.attributeValue(attrName)));
        } else if (attrName.equalsIgnoreCase("timeout")) {
            this.timeout = Integer.valueOf(testdata.attributeValue(attrName));
        } else if (attrName.equalsIgnoreCase("action")) {
            this.action = testdata.attributeValue(attrName);
        } else if (attrName.equalsIgnoreCase("name")) {
            this.testname = testdata.attributeValue(attrName);
        } else if (attrName.equalsIgnoreCase("browsers")) {
            this.browsers = testdata.attributeValue(attrName).trim().toLowerCase();
            if (this.browsers == null || this.browsers.isEmpty()) {
                this.browsers = ConfigLoader.getConfig("SELENIUM_DRIVER").toLowerCase();
                if (this.browsers == null || this.browsers.isEmpty()) {
                    this.browsers = "firefox";
                }/*www  .  ja  v  a2s .c  om*/
            }
        } else if (attrName.equalsIgnoreCase("page")) {
            this.pagename = testdata.attributeValue(attrName);
        } else if (attrName.equalsIgnoreCase("datasetextension")) {
            this.datasetextension = testdata.attributeValue(attrName);
        } else if (attrName.equalsIgnoreCase("groupbythread")) {
            this.groupbythread = testdata.attributeValue(attrName);
        } else if (attrName.equalsIgnoreCase("serviceurl")) {
            this.serviceurl = testdata.attributeValue(attrName);
        } else if (attrName.equalsIgnoreCase("overrideattributes")) {
            this.overrideattributes = testdata.attributeValue(attrName);
        } else if (attrName.equalsIgnoreCase("baseurl")) {
            this.baseurl = testdata.attributeValue(attrName).trim();
            if (this.baseurl != null && !this.baseurl.isEmpty() && this.baseurl.contains("http")) {
                ConfigLoader.setConfigProperty("BASE_APPLICATION_URL", this.baseurl);
                ConfigLoader.setConfigProperty("WEB_SERVICE_URL", this.baseurl);
            }
        } else if (attrName.equalsIgnoreCase("env")) {
            String env = testdata.attributeValue(attrName).trim();
            if (env != null && !env.isEmpty()) {
                ConfigLoader.setConfigProperty("QA_TEST_ENV", env);
            }
        }

    }
    //initialize request object and create an execution session before invoke() is called
    //this.request= new ExecuteHttpRequest(this); 
    //initialize urlConfig
    try {
        this.urlConfig = this.pageUrl.getUrlConfig(this.action);
    } catch (Exception e) {
        WmLog.printMessage("** ERROR : Please verify your test configuration for page and action name !  "
                + e.getMessage());
        WmLog.printMessage(
                "** ERROR : Probably, you have passed a WRONG Page name or Action Name to run which cloud not found in module configuration data files. ");
    }

}