Example usage for org.dom4j Element setAttributeValue

List of usage examples for org.dom4j Element setAttributeValue

Introduction

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

Prototype

void setAttributeValue(QName qName, String value);

Source Link

Document

Sets the attribute value of the given fully qualified name.

Usage

From source file:com.devoteam.srit.xmlloader.genscript.Param.java

License:Open Source License

public String applySubstitution(String text, Msg msg) throws Exception {

    String msgAvecParametres = "";

    if (getRegle() != null) {
        String[] regexTab = getRegle().split("#");

        // Si la regle est sous forme d'expression rgulire
        if (regexTab[0].toUpperCase().contains("REGEX")) {
            GlobalLogger.instance().getApplicationLogger().debug(TextEvent.Topic.CORE,
                    "Replace parameter " + getRegle() + " => " + name);
            String[] regexRule = Arrays.copyOfRange(regexTab, 1, regexTab.length);
            msgAvecParametres = regexRemplace(regexRule, 0, text);
        }//from   w ww . j av a2s  . co  m
        // Si la regle est sous forme de xpath
        else if (regexTab[0].toUpperCase().contains("XPATH")) {
            GlobalLogger.instance().getApplicationLogger().debug(TextEvent.Topic.CORE,
                    "Replace parameter " + getRegle() + " => " + name);
            // Rcupration des paramtres
            String xpathRule = regexTab[1];
            String attribut = regexTab[2];

            attribut = attribut.replace("@", "");

            // Cration de l'arbre DOM correspondant au message
            SAXReader reader = new SAXReader();
            try {
                Document document = reader.read(new ByteArrayInputStream(text.getBytes("UTF-8")));
                // Cration de l'objet XPATH ) selectionner 
                XPath xpath = new DefaultXPath(xpathRule);

                // Rcupration des noeuds correspondants
                List<Node> nodes = xpath.selectNodes(document.getRootElement(), xpath);

                // Pour chaque noeuds  modifier
                Element aRemplacer = null;
                for (Node n : nodes) {
                    setRemplacedValue(n.getText());
                    if (n instanceof Element) {
                        aRemplacer = (Element) n;
                    } else {
                        aRemplacer = n.getParent();
                    }
                    String newValue = getName();
                    String oldValue = aRemplacer.attribute(attribut).getValue();
                    // On regarde si on est dans le cas de paramtres mixtes
                    if (regexTab.length > 3) {
                        if (regexTab[3].equals("REGEX")) {
                            setRemplacedValue(null);
                            String[] regexRule = Arrays.copyOfRange(regexTab, 4, regexTab.length);
                            newValue = regexRemplace(regexRule, 0, oldValue);
                        }
                    }
                    aRemplacer.setAttributeValue(attribut, newValue);
                }

                // Convertion en chane de caractre de l'arbre DOM du message
                msgAvecParametres = document.getRootElement().asXML();
            } catch (Exception e) {

            }
        }
        // si la rgle est sous forme de pathkey
        else if (regexTab[0].toUpperCase().contains("PATHKEY")) {

            String valeurARemplacer = null;
            // On rcupre la valeur  remplacer
            String pathKeyWord = regexTab[1];
            if (msg != null) {
                Parameter valeurParamARemplacer = msg.getParameter(pathKeyWord);
                if (valeurParamARemplacer.length() > 0) {
                    valeurARemplacer = valeurParamARemplacer.get(0).toString();
                }

                // On remplace dans tout le message par le parametre
                if (valeurARemplacer != null) {
                    msgAvecParametres = text.replace(valeurARemplacer, getName());
                    if (!msgAvecParametres.equals(text)) {
                        GlobalLogger.instance().getApplicationLogger().debug(TextEvent.Topic.CORE,
                                "Replace parameter " + valeurARemplacer + " => " + name);
                        setRemplacedValue(valeurARemplacer);
                    }
                }
            }
        }
    }
    // Si le message n'a pas subit de modification, on retourne null
    if (!isUsed()) {
        return null;
    }

    // Sinon on retourne le message modifi avec les paramtres
    return msgAvecParametres;
}

From source file:com.hihsoft.sso.systempublic.servlet.HihSoftInitDBServlet.java

License:Apache License

/**
 * xml,"import"?xml/*from  w w  w. j  av a  2s  . c  o  m*/
 * 
 * @param file
 */
@SuppressWarnings("deprecation")
private void loadFile(File file) throws Exception {
    XmlParseUtil xmlUtil = new XmlParseUtil();
    Document doc = xmlUtil.readXml(file);
    Element root = doc.getRootElement();
    Iterator<?> it = root.elementIterator();
    String path = file.getPath();
    String fileseparate = System.getProperty("file.separator");
    String dir = path.substring(0, path.lastIndexOf(fileseparate));
    Element el = null;
    String isLoad = null;
    String type = null;
    String resource = null;
    File file1 = null;
    Document doc1 = null;
    while (it.hasNext()) {
        el = (Element) it.next();
        isLoad = el.attributeValue("isLoad");
        type = el.attributeValue("type");
        resource = el.attributeValue("resource");
        // "import",xml?
        if (el.getName().equals("import") && isLoad != null && isLoad.equals("true")) {
            file1 = new FileSystemResource(dir + fileseparate + resource).getFile();
            if (file1.isDirectory()) {
                readFile(file1);
            } else {
                doc1 = xmlUtil.readXml(file1);
                // ?
                if (el.getName().equals("import") && isLoad != null && isLoad.equals("true") && type != null
                        && type.equals("framework")) {
                    initDB(doc1);
                    el.setAttributeValue("isLoad", "false");
                    doc1.getRootElement().element("models").setAttributeValue("isLoad", "false");
                    xmlUtil.writeXml(doc1, file1);

                }
            }
        } else if (el.getName().equals("models")) {
            initDB(doc);
            doc.getRootElement().element("models").setAttributeValue("isLoad", "false");
            // xmlUtil.writeXml(doc1, file1);
        }
        xmlUtil.writeXml(doc, file);
    }
    initFrameworkService.saveOrUpdateInitFramework();
}

From source file:com.poka.util.XmlSax.java

public boolean changeBankNumXmlAtt() {
    Document doc = load(bankFile, false);
    Element rootElm = doc.getRootElement();
    Element root1Elm = rootElm.element("bankInfos");
    if (root1Elm == null) {
        root1Elm = rootElm.addElement("bankInfos");
    }//  w  w w .  j  a v a2 s. com
    boolean flag = false;
    List nodes = root1Elm.elements("bankInfo");
    for (Iterator it = nodes.iterator(); it.hasNext();) {
        Element elm = (Element) it.next();
        String attr = elm.attributeValue("id");
        if (attr.equals("1")) {
            flag = true;
            elm.setAttributeValue("ip", this.ip);
            elm.setAttributeValue("port", this.port);
            elm.setAttributeValue("bankNum", this.bankNum);
            elm.setAttributeValue("NetWorkNum", this.networkNum);
        } else {
            continue;
        }
    }
    if (!flag) {
        Element elm = root1Elm.addElement("bankInfo");
        elm.setAttributeValue("ip", this.ip);
        elm.setAttributeValue("port", this.port);
        elm.setAttributeValue("bankNum", this.bankNum);
        elm.setAttributeValue("NetWorkNum", this.networkNum);
        elm.setAttributeValue("id", "1");
    }
    return writeToXml(doc, bankFile);
}

From source file:com.poka.util.XmlSax.java

public int updateOrAddMachineInfo(MachinesCfg cfg) {
    Document doc = load(bankFile, false);
    Element rootElm = doc.getRootElement();
    Element root1Elm = rootElm.element("machines");
    if (root1Elm == null) {
        root1Elm = rootElm.addElement("machines");
    }//from ww w. j  a  v  a 2s  . c om
    List nodes = root1Elm.elements("machine");
    boolean flag = false;
    for (Iterator it = nodes.iterator(); it.hasNext();) {
        Element elm = (Element) it.next();
        if (elm.attributeValue("ip").trim().equals(cfg.getIp().trim())
                && elm.attributeValue("type").trim().equals("" + cfg.getType())) {
            elm.setAttributeValue("machineType", cfg.getMachineType().trim());
            elm.setAttributeValue("machineNum", "" + cfg.getMachineNum());
            elm.setAttributeValue("user1", cfg.getUser1().trim());
            elm.setAttributeValue("user2", cfg.getUser2().trim());
            elm.setAttributeValue("type", "" + cfg.getType());
            flag = true;
            break;
        }
    }
    if (!flag) {
        Element tem = root1Elm.addElement("machine");
        tem.addAttribute("machineType", cfg.getMachineType().trim());
        tem.setAttributeValue("machineNum", "" + cfg.getMachineNum());
        tem.addAttribute("ip", cfg.getIp().trim());
        tem.addAttribute("user1", cfg.getUser1().trim());
        tem.addAttribute("user2", cfg.getUser2().trim());
        tem.addAttribute("type", "" + cfg.getType());
    }
    try {
        OutputFormat format = OutputFormat.createPrettyPrint();
        // format.setEncoding("UTF-8");
        format.setEncoding(encodingType);
        XMLWriter writer = new XMLWriter(new FileWriter(bankFile), format);
        writer.write(doc);
        writer.flush();
        writer.close();
        if (flag) {//
            return 0;
        } else {//
            return 1;
        }
        //  return true;
    } catch (IOException ex) {
        Logger.getLogger(XmlSax.class.getName()).log(Level.SEVERE, null, ex);
    }
    return -1;
}

From source file:com.poka.util.XmlSax.java

public void setSoftTitle(String title) {
    Document doc = load(bankFile, false);
    Element rootElm = doc.getRootElement();
    Element root1Elm = rootElm.element("bankInfos");
    if (root1Elm == null) {
        root1Elm = rootElm.addElement("bankInfos");
    }//w ww  . jav  a2 s  . c o  m
    boolean flag = false;
    List nodes = root1Elm.elements("bankInfo");
    for (Iterator it = nodes.iterator(); it.hasNext();) {
        Element elm = (Element) it.next();
        String attr = elm.attributeValue("id");
        if (attr.equals("2")) {
            flag = true;
            elm.setAttributeValue("myname", title);
        }
    }
    if (!flag) {
        Element elm = root1Elm.addElement("bankInfo");
        elm.setAttributeValue("myname", title);
        elm.setAttributeValue("id", "2");
    }
    writeToXml(doc, bankFile);
}

From source file:com.thoughtworks.cruise.utils.configfile.CruiseConfigFileEditor.java

License:Apache License

public void write(String xml) {
    FileChannel channel = null;//w  w w .  j ava2s .com
    FileInputStream inputStream = null;
    FileOutputStream outputStream = null;
    FileLock lock = null;
    try {
        File configFile = getCruiseConfigFile();
        if (configFile.exists()) {
            String serverId = XmlUtil.parse(FileUtil.readToEnd(configFile)).selectSingleNode(SERVER_ID_XPATH)
                    .getText();
            Document dom = XmlUtil.parse(xml);
            Element node = (Element) dom.selectSingleNode("//server");
            node.setAttributeValue("serverId", serverId);
            xml = dom.asXML();
        }
        System.err.println("Writing out config file to " + configFile.getCanonicalPath());
        configFile.getParentFile().mkdirs();
        configFile.createNewFile();
        RandomAccessFile randomAccessFile = new RandomAccessFile(configFile, "rw");

        channel = randomAccessFile.getChannel();
        lock = channel.lock();
        randomAccessFile.seek(0);
        randomAccessFile.setLength(0);
        outputStream = new FileOutputStream(randomAccessFile.getFD());
        IOUtils.write(xml, outputStream);
    } catch (Exception e) {
        throw bomb("Error writing config file: " + e.getMessage(), e);
    } finally {
        if (channel != null && lock != null) {
            try {
                lock.release();
                channel.close();
                IOUtils.closeQuietly(outputStream);
                IOUtils.closeQuietly(inputStream);
            } catch (IOException e) {
            }
        }
    }
    try {
        Thread.sleep(5000);
    } catch (InterruptedException ignored) {
    }
}

From source file:org.nuxeo.ecm.core.io.impl.transformers.SchemaRenamer.java

License:Open Source License

@Override
public boolean transform(ExportedDocument xdoc) throws IOException {
    Element root = xdoc.getDocument().getRootElement();

    List<Object> schemas = root.elements("schema");
    Element src = null;/* w  w  w.  j a v  a 2 s  . co m*/
    if (schemas != null) {
        for (Object s : schemas) {
            Element schema = (Element) s;
            String name = schema.attribute("name").getText();
            if (srcSchema.equalsIgnoreCase(name)) {
                Namespace ns = new Namespace(dstPrefix, "http://www.nuxeo.org/ecm/schemas/" + dstSchema);
                schema.add(ns);
                schema.setAttributeValue("name", dstSchema);
                List<Element> fields = schema.elements();
                for (Element field : fields) {
                    field.setQName(new QName(field.getName(), ns));
                    ;
                }
            }
        }
    }
    return true;
}

From source file:org.talend.mdm.webapp.browserecords.server.service.UploadService.java

License:Open Source License

private void setAttributeValue(Element currentElement, String value) {
    if (xsiTypeQName == null) {
        xsiTypeQName = new QName(Constants.XSI_TYPE_NAME,
                new Namespace(Constants.XSI_PREFIX, Constants.XSI_URI), Constants.XSI_TYPE_QUALIFIED_NAME);
    }/*from   w w w  .  j  ava2 s .  co m*/
    if (!value.equals(currentElement.attributeValue(xsiTypeQName))) {
        currentElement.setAttributeValue(xsiTypeQName, value);
    }
}