Example usage for org.dom4j Element addAttribute

List of usage examples for org.dom4j Element addAttribute

Introduction

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

Prototype

Element addAttribute(QName qName, String value);

Source Link

Document

Adds the attribute value of the given fully qualified name.

Usage

From source file:com.orange.atk.atkUI.corecli.Campaign.java

License:Apache License

/**
 * Save current campaign in .mcl file//w  w  w  . j a va  2s  . co  m
 * 
 * @param clFileName
 * @throws IOException
 */
public static void save(String clFileName, Campaign camp) {
    Document document = DocumentHelper.createDocument();
    Element root = document.addElement("campaign");
    if (camp.getName() != null)
        root.addAttribute("name", camp.getName());
    if (camp.getAuthor() != null)
        root.addAttribute("author", camp.getAuthor());
    if (camp.getDate() != null)
        root.addAttribute("date", camp.getDate());
    if (camp.getDescription() != null)
        root.addAttribute("description", camp.getDescription());
    int stepNumber = 0;
    for (Step step : camp) {
        step.save(root, stepNumber);
        stepNumber++;
    }
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = null;
    try {
        writer = new XMLWriter(new FileWriter(clFileName), format);
        writer.write(document);
        writer.close();
    } catch (IOException e) {
        Alert.raise(e, "Unable to save check-list in a file.");
    }
}

From source file:com.orange.atk.atkUI.corecli.utils.FileUtilities.java

License:Apache License

/**
 * Copy a source html file into a destination file, patching the style sheet
 * on the fly for the given one.//from  ww  w. j a v a 2s .  c om
 * 
 * @param in
 *            source html file
 * @param out
 *            destination file
 * @param newStyleSheetPath
 *            the new css style sheet absolute path
 * @throws Exception
 */
public static void copyHTMLFilePrettyPrint(File in, File out, String newStyleSheetPath) throws Exception {
    SAXReader reader = new SAXReader();
    Document document = reader.read(in.getAbsolutePath());
    Element linkElem = (Element) document.selectSingleNode("/html/head/link");
    if (linkElem != null) {
        linkElem.addAttribute("href", newStyleSheetPath);
    }
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(new FileOutputStream(out), format);
    writer.write(document);
    writer.close();
}

From source file:com.org.dms.action.service.claimmng.WorkDispatchMngAction.java

/**
 * //from  www .ja va 2  s .  co m
 * @param type 2?  1 
 * @param remark  
 * @param type workId
 * @return
 */
public String dispatchResult(String type, Map<String, String> map) {

    Document domresult = DocumentFactory.getInstance().createDocument();
    Element root = domresult.addElement("DBSET");
    Element r = root.addElement("R");

    Element c01 = r.addElement("C");
    c01.addAttribute("N", "BACK_ID");//???ID
    if (map.get("workId") != null && !"".equals(map.get("workId"))) {
        c01.addText(map.get("workId"));
    }

    Element c02 = r.addElement("C");
    c02.addAttribute("N", "JOBORDER_PK");//400?
    if (map.get("joborderPk") != null && !"".equals(map.get("joborderPk"))) {
        c02.addText(map.get("joborderPk"));
    }

    Element c03 = r.addElement("C");
    c03.addAttribute("N", "JOBORDER_CODE");//400??
    if (map.get("joborderCoe") != null && !"".equals(map.get("joborderCoe"))) {
        c03.addText(map.get("joborderCoe"));
    }

    Element c04 = r.addElement("C");
    c04.addAttribute("N", "BACK_TYPE");//??12 
    c04.addText(type);

    Element c05 = r.addElement("C");
    c05.addAttribute("N", "BACK_MSG");//
    if (map.get("remarks") != null && !"".equals(map.get("remarks"))) {
        c05.addText(map.get("remarks"));
    }

    Element c06 = r.addElement("C");
    c06.addAttribute("N", "JOBORDER_DNAME");//???(??)
    if (map.get("orgCode") != null && !"".equals(map.get("orgCode"))) {
        c06.addText(map.get("orgCode"));
    }

    Element c07 = r.addElement("C");
    c07.addAttribute("N", "JOBORDER_OUT");//?
    if (map.get("ifout") != null && !"".equals(map.get("ifout"))) {
        c07.addText(map.get("ifout"));
    }

    Element c08 = r.addElement("C");
    c08.addAttribute("N", "REPAIRER_NAME");//??
    if (map.get("userName") != null && !"".equals(map.get("userName"))) {
        c08.addText(map.get("userName"));
    }

    Element c09 = r.addElement("C");
    c09.addAttribute("N", "REPAIRER_CONTACT");//??
    if (map.get("mobils") != null && !"".equals(map.get("mobils"))) {
        c09.addText(map.get("mobils"));
    }

    Element c10 = r.addElement("C");
    c10.addAttribute("N", "VEHICLE_FLAG");//VEHICLE_FLAG ??10?
    if (map.get("ifVehicle") != null && !"".equals(map.get("ifVehicle"))) {
        c10.addText(map.get("ifVehicle"));
    }

    Element c11 = r.addElement("C");
    c11.addAttribute("N", "VEHICLE_NUMBER");//?
    if (map.get("licensePlate") != null && !"".equals(map.get("licensePlate"))) {
        c11.addText(map.get("licensePlate"));
    }

    Element c12 = r.addElement("C");
    c12.addAttribute("N", "JOBORDER_DTIME");//(YYYY-MM-DD HH24:MI:SS)
    if (map.get("dtime") != null && !"".equals(map.get("dtime"))) {
        c12.addText(Pub.getCurrentDate().toLocaleString());
    } else {
        c12.addText(Pub.getCurrentDate().toLocaleString());
    }

    Element c13 = r.addElement("C");
    c13.addAttribute("N", "REMARK");//
    if (map.get("remarks") != null && !"".equals(map.get("remarks"))) {
        c13.addText(map.get("remarks"));
    }
    return domresult.getRootElement().asXML();
}

From source file:com.ostrichemulators.semtool.ui.components.playsheets.BrowserPlaySheet2.java

License:Open Source License

protected BufferedImage getExportImageFromSVGBlock() throws IOException {
    log.debug("Using SVG block to save image.");
    DOMReader rdr = new DOMReader();
    Document doc = rdr.read(engine.getDocument());
    Document svgdoc = null;/*from   w  w w. ja  v a 2  s. c  o  m*/
    File svgfile = null;
    try {
        Map<String, String> namespaceUris = new HashMap<>();
        namespaceUris.put("svg", "http://www.w3.org/2000/svg");
        namespaceUris.put("xhtml", "http://www.w3.org/1999/xhtml");

        XPath xp = DocumentHelper.createXPath("//svg:svg");
        xp.setNamespaceURIs(namespaceUris);
        // don't forget about the styles
        XPath stylexp = DocumentHelper.createXPath("//xhtml:style");
        stylexp.setNamespaceURIs(namespaceUris);

        svgdoc = DocumentHelper.createDocument();
        Element svg = null;
        List<?> theSVGElements = xp.selectNodes(doc);
        if (theSVGElements.size() == 1) {
            svg = Element.class.cast(theSVGElements.get(0)).createCopy();
        } else {
            int currentTop = 0;
            int biggestSize = 0;
            for (int i = 0; i < theSVGElements.size(); i++) {
                Element thisElement = Element.class.cast(theSVGElements.get(i)).createCopy();
                int thisSize = thisElement.asXML().length();
                if (thisSize > biggestSize) {
                    currentTop = i;
                    biggestSize = thisSize;
                }
            }
            svg = Element.class.cast(theSVGElements.get(currentTop)).createCopy();
        }

        svgdoc.setRootElement(svg);

        Element oldstyle = Element.class.cast(stylexp.selectSingleNode(doc));
        if (null != oldstyle) {
            Element defs = svg.addElement("defs");
            Element style = defs.addElement("style");
            style.addAttribute("type", "text/css");
            String styledata = oldstyle.getTextTrim();
            style.addCDATA(styledata);
            // put the stylesheet definitions first
            List l = svg.elements();
            l.remove(defs);
            l.add(0, defs);
        }

        // clean up the SVG a little...
        // d3 comes up with coords like
        // M360,27475.063247863247C450,27475.063247863247 450,27269.907692307694 540,27269.907692307694
        XPath cleanxp1 = DocumentHelper.createXPath("//svg:path");
        Pattern pat = Pattern.compile(",([0-9]+)\\.([0-9]{1,2})[0-9]+");
        cleanxp1.setNamespaceURIs(namespaceUris);
        List<?> cleanups = cleanxp1.selectNodes(svgdoc);
        for (Object n : cleanups) {
            Element e = Element.class.cast(n);
            String dstr = e.attributeValue("d");
            Matcher m = pat.matcher(dstr);
            dstr = m.replaceAll(",$1.$2 ");
            e.addAttribute("d", dstr.replaceAll("([0-9])C([0-9])", "$1 C$2").trim());
        }
        XPath cleanxp2 = DocumentHelper.createXPath("//svg:g[@class='node']");
        cleanxp2.setNamespaceURIs(namespaceUris);
        cleanups = cleanxp2.selectNodes(svgdoc);
        for (Object n : cleanups) {
            Element e = Element.class.cast(n);
            String dstr = e.attributeValue("transform");
            Matcher m = pat.matcher(dstr);
            dstr = m.replaceAll(",$1.$2");
            e.addAttribute("transform", dstr.trim());
        }

        svgfile = File.createTempFile("graphviz-", ".svg");
        try (Writer svgw = new BufferedWriter(new FileWriter(svgfile))) {
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter xmlw = new XMLWriter(svgw, format);
            xmlw.write(svgdoc);
            xmlw.close();

            if (log.isDebugEnabled()) {
                FileUtils.copyFile(svgfile, new File(FileUtils.getTempDirectory(), "graphvisualization.svg"));
            }
        }

        try (Reader svgr = new BufferedReader(new FileReader(svgfile))) {
            TranscoderInput inputSvg = new TranscoderInput(svgr);

            ByteArrayOutputStream baos = new ByteArrayOutputStream((int) svgfile.length());
            TranscoderOutput outputPng = new TranscoderOutput(baos);

            try {
                PNGTranscoder transcoder = new PNGTranscoder();
                transcoder.addTranscodingHint(PNGTranscoder.KEY_INDEXED, 256);
                transcoder.addTranscodingHint(ImageTranscoder.KEY_BACKGROUND_COLOR, Color.WHITE);
                transcoder.transcode(inputSvg, outputPng);
            } catch (Throwable t) {
                log.error(t, t);
            }
            baos.flush();
            baos.close();

            return ImageIO.read(new ByteArrayInputStream(baos.toByteArray()));
        }
    } catch (InvalidXPathException e) {
        log.error(e);
        String msg = "Problem creating image";
        if (null != svgdoc) {
            try {
                File errsvg = new File(FileUtils.getTempDirectory(), "graphvisualization.svg");
                FileUtils.write(errsvg, svgdoc.asXML(), Charset.defaultCharset());
                msg = "Could not create the image. SVG data store here: " + errsvg.getAbsolutePath();
            } catch (IOException ex) {
                // don't care
            }
        }
        throw new IOException(msg, e);
    } finally {
        if (null != svgfile) {
            FileUtils.deleteQuietly(svgfile);
        }
    }
}

From source file:com.pactera.edg.am.metamanager.extractor.util.Dom4jWriter.java

License:Open Source License

public static void addAttribute(Element element, String key, String value) {
    element.addAttribute(key, value);
}

From source file:com.petpet.c3po.analysis.ProfileGenerator.java

License:Apache License

private void genereateFilterElement(Element partition, Filter filter) {
    Element elmntFilter = partition.addElement("filter");
    // TODO get rid of id
    elmntFilter.addAttribute("id", UUID.randomUUID().toString());
    Element parameters = elmntFilter.addElement("parameters");

    for (FilterCondition fc : filter.getConditions()) {
        Element parameter = parameters.addElement("parameter");
        parameter.addElement("name").addText(fc.getField());
        parameter.addElement("value").addText(fc.getValue().toString());
    }//from   w  ww  .j ava2  s .  co m

}

From source file:com.petpet.c3po.analysis.ProfileGenerator.java

License:Apache License

private void createSamples(final Filter filter, final Element partition, int sampleSize) {
    final Element samples = partition.addElement("samples");
    samples.addAttribute("type", this.sampleSelector.getType());
    this.sampleSelector.setFilter(filter);
    final List<String> output = this.sampleSelector.execute(sampleSize);

    LOG.debug("Found {} representatives", output.size());
    for (String s : output) {
        LOG.debug("Processing sample {}", s);
        createSampleElement(samples, s);
    }//ww  w .  ja  va  2  s. com
}

From source file:com.petpet.c3po.analysis.ProfileGenerator.java

License:Apache License

private void processNumericProperty(final Filter filter, final Element prop, final Property p) {

    List<String> properties = new ArrayList<String>();
    properties.add(p.getKey());/* ww w.  j  a  va  2s .co  m*/
    Map<String, Map<String, Long>> histograms = this.persistence.getHistograms(properties, filter, null);
    Map<String, Long> histogram = histograms.get(p.getKey());

    prop.addAttribute("count", histogram.get("count") + "");
    prop.addAttribute("sum", histogram.get("sum") + "");
    prop.addAttribute("min", histogram.get("min") + "");
    prop.addAttribute("max", histogram.get("max") + "");
    prop.addAttribute("avg", histogram.get("avg") + "");
    prop.addAttribute("var", histogram.get("var") + "");
    prop.addAttribute("sd", histogram.get("std") + "");
}

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  o m
    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.processpuzzle.application.domain.ApplicationRepository.java

License:Open Source License

private void createApplicationElement(Application application) {
    Element root = xmlDocument.getRootElement();

    Element applicationElement = root.addElement(APPLICATION_ELEMENT);
    applicationElement.addAttribute(NAME_ATTRIBUTE, application.getApplicationName());

    Element versionElement = applicationElement.addElement(VERSION_ELEMENT);
    versionElement.setText(application.getApplicationVersion());

    Element descriptionElement = applicationElement.addElement(DESCRIPTION_ELEMENT);
    descriptionElement.setText(application.getDescription());

    Element classNameElement = applicationElement.addElement(CLASS_NAME_ELEMENT);
    classNameElement.setText(application.getClass().getName());

    Element executionStatusElement = applicationElement.addElement(EXECUTION_STATUS_ELEMENT);
    executionStatusElement.setText(application.getExecutionStatus().name());

    Element installationStatusElement = applicationElement.addElement(INSTALLATION_STATUS_ELEMENT);
    installationStatusElement.setText(application.getInstallationStatus().name());

    Element configurationDescriptorPath = applicationElement.addElement(CONFIGURATION_PATH_ELEMENT);
    configurationDescriptorPath.setText(application.getConfigurationDescriptorPath());
}