Example usage for org.w3c.dom Document createAttribute

List of usage examples for org.w3c.dom Document createAttribute

Introduction

In this page you can find the example usage for org.w3c.dom Document createAttribute.

Prototype

public Attr createAttribute(String name) throws DOMException;

Source Link

Document

Creates an Attr of the given name.

Usage

From source file:clus.statistic.ClassificationStat.java

@Override
public Element getPredictElement(Document doc) {
    NumberFormat fr = ClusFormat.SIX_AFTER_DOT;
    Element stats = doc.createElement("ClassificationStat");
    Attr examples = doc.createAttribute("examples");
    examples.setValue(fr.format(m_SumWeight));
    stats.setAttributeNode(examples);/*w  w  w .  j  a  v a2  s.c  o m*/
    Element majorities = doc.createElement("MajorityClasses");
    stats.appendChild(majorities);
    if (m_MajorityClasses != null) {
        for (int i = 0; i < m_NbTarget; i++) {
            Element majority = doc.createElement("Target");
            majorities.appendChild(majority);

            majority.setTextContent(m_Attrs[i].getValue(m_MajorityClasses[i]));

            Attr name = doc.createAttribute("name");
            name.setValue(m_Attrs[i].getName());
            majority.setAttributeNode(name);
        }
    } else {
        Element majority = doc.createElement("Attribute");
        majorities.appendChild(majority);
    }
    Element distribution = doc.createElement("Distributions");
    stats.appendChild(distribution);
    for (int j = 0; j < m_NbTarget; j++) {
        Element target = doc.createElement("Target");
        distribution.appendChild(target);
        Attr name = doc.createAttribute("name");
        name.setValue(m_Attrs[j] + "");
        target.setAttributeNode(name);
        for (int i = 0; i < m_ClassCounts[j].length; i++) {
            Element value = doc.createElement("Value");
            target.appendChild(value);
            name = doc.createAttribute("name");
            name.setValue(m_Attrs[j].getValue(i));
            value.setAttributeNode(name);
            value.setTextContent(m_ClassCounts[j][i] + "");
        }
    }
    return stats;
}

From source file:Interface.MainJFrame.java

private void btnXMLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXMLActionPerformed
    // TODO add your handling code here:
    try {//from   ww  w. ja va  2 s . c o m

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        // root elements
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("Employees");
        doc.appendChild(rootElement);

        // staff elements
        String empTitle = txtEmployeeType.getText().trim();
        empTitle = empTitle.replace(" ", "");
        Element staff = doc.createElement(empTitle);
        rootElement.appendChild(staff);

        // set attribute to staff element
        Attr attr = doc.createAttribute("ID");
        attr.setValue(txtEmployeeID.getText().trim());
        staff.setAttributeNode(attr);

        // shorten way
        // staff.setAttribute("id", "1");

        // FullName elements
        Element FulllName = doc.createElement("FulllName");
        FulllName.appendChild(doc.createTextNode(txtFullName.getText().trim()));
        staff.appendChild(FulllName);

        // Phone elements
        Element Phone = doc.createElement("PhoneNumber");
        Phone.appendChild(doc.createTextNode(txtPhoneNumber.getText().trim()));
        staff.appendChild(Phone);

        // Address elements
        Element Address = doc.createElement("Address");
        Address.appendChild(doc.createTextNode(txtAddress.getText().trim()));
        staff.appendChild(Address);

        // Title elements
        Element Title = doc.createElement("Tile");
        Title.appendChild(doc.createTextNode(txtEmployeeType.getText().trim()));
        staff.appendChild(Title);

        // PayCategory elements
        Element PayCategory = doc.createElement("PayCategory");
        PayCategory.appendChild(doc.createTextNode(txtPayCategory.getText().trim()));
        staff.appendChild(PayCategory);

        // Salary elements
        Element Salary = doc.createElement("Salary");
        Salary.appendChild(doc.createTextNode(txtSalary.getText().trim()));
        staff.appendChild(Salary);

        // Hours elements

        String hours = txtHours.getText().trim();
        if (txtHours.getText().equalsIgnoreCase("") || hours == null) {
            hours = "null";
        }
        Element Hours = doc.createElement("Hours");
        Hours.appendChild(doc.createTextNode(hours));
        staff.appendChild(Hours);

        // Bonus elements
        Element Bonus = doc.createElement("Bonus");
        Bonus.appendChild(doc.createTextNode(txtBonuses.getText().trim()));
        staff.appendChild(Bonus);

        // Total elements
        Element Total = doc.createElement("Total");
        Total.appendChild(doc.createTextNode(txtTotal.getText().trim()));
        staff.appendChild(Total);

        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("XMLOutput"));

        // Output to console for testing
        // StreamResult result = new StreamResult(System.out);

        transformer.transform(source, result);

    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    } catch (TransformerException tfe) {
        tfe.printStackTrace();
    }

}

From source file:com.legstar.jaxb.gen.CobolJAXBCustomizer.java

/**
 * JAXB needs to know the LegStar extension prefix used. Here we lookup the
 * extension attribute and version if they are found, we add to them
 * otherwise we create new attributes.// w ww.j  a  v a 2  s .  c o  m
 * <p/>
 * 
 * @param xsd the XML Schema
 * @param jaxbPrefix the JAXB namespace prefix
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public void injectJaxbExtensionAttributes(final XmlSchema xsd, final String jaxbPrefix) {

    // Lookup the LegStar namespace prefix
    String coxbPrefix = COXB_DEFAULT_NAMESPACE_PREFIX;
    NamespacePrefixList nsList = xsd.getNamespaceContext();
    for (String prefix : nsList.getDeclaredPrefixes()) {
        if (nsList.getNamespaceURI(prefix).equals(CobolMarkup.NS)) {
            coxbPrefix = prefix;
            break;
        }
    }

    // Retrieve extension attributes if any
    Map metaInfoMap = xsd.getMetaInfoMap();
    Map<QName, Attr> extensionMap = null;
    if (metaInfoMap != null) {
        extensionMap = (Map<QName, Attr>) metaInfoMap.get(Constants.MetaDataConstants.EXTERNAL_ATTRIBUTES);
    } else {
        metaInfoMap = new LinkedHashMap();
        xsd.setMetaInfoMap(metaInfoMap);
    }
    if (extensionMap == null) {
        extensionMap = new HashMap<QName, Attr>();
    }

    // Extension attributes are DOM attributes
    Document doc = _db.newDocument();

    // Make sure the JAXB version extension is added
    QName versionQName = new QName(JAXB_NAMESPACE, JAXB_VERSION_ATTR_NAME);
    Attr attrib = doc.createAttribute(jaxbPrefix + ':' + JAXB_VERSION_ATTR_NAME);
    attrib.setValue(JAXB_VERSION_ATTR_VALUE);
    extensionMap.put(versionQName, attrib);

    /*
     * JAXB extension prefixes might already be present in which case we
     * make sure the legstar extension is there too. Extension prefixes are
     * specified as a whitespace-separated list of namespace prefixes.
     */

    QName extpfxQName = new QName(JAXB_NAMESPACE, JAXB_EXTENSION_BINDING_PREFIXES_ATTR_NAME);
    attrib = extensionMap.get(extpfxQName);
    if (attrib == null) {
        attrib = doc.createAttribute(jaxbPrefix + ':' + JAXB_EXTENSION_BINDING_PREFIXES_ATTR_NAME);
    }
    String extpfx = attrib.getValue();
    if (extpfx == null || extpfx.length() == 0) {
        extpfx = coxbPrefix;
    } else {
        boolean hasCoxbPrefix = false;
        StringTokenizer tokenizer = new StringTokenizer(extpfx, " ");
        while (tokenizer.hasMoreTokens()) {
            if (tokenizer.nextToken().equals(coxbPrefix)) {
                hasCoxbPrefix = true;
                break;
            }
        }
        if (!hasCoxbPrefix) {
            extpfx = extpfx + " " + coxbPrefix;
        }
    }
    attrib.setValue(extpfx);
    extensionMap.put(extpfxQName, attrib);

    metaInfoMap.put(Constants.MetaDataConstants.EXTERNAL_ATTRIBUTES, extensionMap);
}

From source file:ar.edu.uns.cs.vyglab.arq.rockar.gui.JFrameControlPanel.java

public void saveTable() {
    File currentDir = new File(System.getProperty("user.dir"));
    JFileChooser saveDialog = new JFileChooser(currentDir);
    FileNameExtensionFilter filter = new FileNameExtensionFilter("MTF File", "mtf", "mtf");
    saveDialog.setFileFilter(filter);/* w ww .ja va  2s .co  m*/
    int response = saveDialog.showSaveDialog(this);
    if (response == saveDialog.APPROVE_OPTION) {
        File file = saveDialog.getSelectedFile();
        if (file.getName().lastIndexOf(".") == -1) {
            file = new File(file.getName() + ".mtf");
        }
        DataCenter.fileMineralList = file;
        try {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            // root elements
            Document doc = docBuilder.newDocument();
            Element rootElement = doc.createElement("table");
            doc.appendChild(rootElement);

            // para cada mineral en la tabla de minerales, agrego un Element
            for (int i = 0; i < this.jTableMineralsModel.getRowCount(); i++) {
                Element mineral = doc.createElement("mineral");

                // set attribute id to mineral element
                Attr attr = doc.createAttribute("key");
                //attr.setValue(this.jTableMinerales.getModel().getValueAt(i, 0).toString());
                attr.setValue(this.jTableMineralsModel.getValueAt(i, 0).toString());
                mineral.setAttributeNode(attr);

                // set attribute name to mineral element
                attr = doc.createAttribute("name");
                attr.setValue(this.jTableMineralsModel.getValueAt(i, 1).toString());
                mineral.setAttributeNode(attr);

                // set attribute color to mineral element
                attr = doc.createAttribute("color");
                attr.setValue(String.valueOf(((Color) this.jTableMineralsModel.getValueAt(i, 2)).getRGB()));
                mineral.setAttributeNode(attr);

                // agrego el mineral a los minerales
                rootElement.appendChild(mineral);
            }

            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(DataCenter.fileMineralList);

            transformer.transform(source, result);

        } catch (Exception e) {
        }
    }
}

From source file:com.aurel.track.admin.customize.category.report.execute.ReportBeansToLaTeXConverter.java

/**
 * Convert the result set into an XML structure.
 *
 * @param items/*from   w w w  .j a v a  2s.c o  m*/
 * @param withHistory
 * @param locale
 * @param personBean
 * @param filterName
 * @param filterExpression
 * @param useProjectSpecificID
 * @param outfileName
 * @return
 */
public Document convertToDOM(List<ReportBean> items, boolean withHistory, Locale locale, TPersonBean personBean,
        String filterName, String filterExpression, boolean useProjectSpecificID, String outfileName) {
    Document doc = null;

    try {

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        // root elements
        doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("pdfFile");
        doc.appendChild(rootElement);

        // set attribute to staff element
        Attr attr = doc.createAttribute("file");
        outfileName = outfileName.replace(".tex", ".pdf");
        attr.setValue(latexTmpDir + File.separator + outfileName);
        rootElement.setAttributeNode(attr);

    } catch (FactoryConfigurationError e) {
        LOGGER.error("Creating the DOM document failed with FactoryConfigurationError:" + e.getMessage());
        LOGGER.debug(ExceptionUtils.getStackTrace(e));
        return null;
    } catch (ParserConfigurationException e) {
        LOGGER.error("Creating the DOM document failed with ParserConfigurationException: " + e.getMessage());
        LOGGER.debug(ExceptionUtils.getStackTrace(e));
        return null;
    }

    return doc;
}

From source file:org.shareok.data.sagedata.SageJournalDataProcessorAbstract.java

@Override
/** /*from  w  w w  .j a v a 2  s  .c  om*/
 * Convert the article data to dublin core xml metadata and save the the file
 * 
 * @param String fileName : the root folder contains all the uploading article data
 */
public void exportXmlByJournalData(String fileName) {

    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("dublin_core");
        doc.appendChild(rootElement);

        // Add the type node:
        Element element = doc.createElement("dcvalue");
        element.appendChild(doc.createTextNode("Research Article"));
        rootElement.appendChild(element);

        Attr attr = doc.createAttribute("element");
        attr.setValue("type");
        element.setAttributeNode(attr);

        attr = doc.createAttribute("language");
        attr.setValue("en_US");
        element.setAttributeNode(attr);

        attr = doc.createAttribute("qualifier");
        attr.setValue("none");
        element.setAttributeNode(attr);

        // Add the abstract node:
        String abs = journalData.getAbstractText();
        if (null != abs) {
            Element elementAbs = doc.createElement("dcvalue");
            elementAbs.appendChild(doc.createTextNode(abs));
            rootElement.appendChild(elementAbs);

            attr = doc.createAttribute("element");
            attr.setValue("description");
            elementAbs.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementAbs.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("abstract");
            elementAbs.setAttributeNode(attr);
        }

        // Add the language node:
        String lang = journalData.getLanguage();
        if (null != lang) {
            Element elementLang = doc.createElement("dcvalue");
            elementLang.appendChild(doc.createTextNode(lang));
            rootElement.appendChild(elementLang);

            attr = doc.createAttribute("element");
            attr.setValue("language");
            elementLang.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementLang.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("iso");
            elementLang.setAttributeNode(attr);
        }

        // Add the title node:
        String tit = journalData.getTitle();
        if (null != tit) {
            Element elementTitle = doc.createElement("dcvalue");
            elementTitle.appendChild(doc.createTextNode(tit));
            rootElement.appendChild(elementTitle);

            attr = doc.createAttribute("element");
            attr.setValue("title");
            elementTitle.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementTitle.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("none");
            elementTitle.setAttributeNode(attr);
        }

        // Add the available date node:
        //            Element elementAvailable = doc.createElement("dcvalue");
        //            elementAvailable.appendChild(doc.createTextNode(getDateAvailable().toString()));
        //            rootElement.appendChild(elementAvailable);
        //            
        //            attr = doc.createAttribute("element");
        //            attr.setValue("date");
        //            elementAvailable.setAttributeNode(attr);
        //            
        //            attr = doc.createAttribute("qualifier");
        //            attr.setValue("available");
        //            elementAvailable.setAttributeNode(attr);

        // Add the issued date node:
        Date issueDate = journalData.getDateIssued();
        if (null != issueDate) {
            SimpleDateFormat format_issuedDate = new SimpleDateFormat("yyyy-MM-dd");
            Element elementIssued = doc.createElement("dcvalue");
            elementIssued.appendChild(doc.createTextNode(format_issuedDate.format(issueDate)));
            rootElement.appendChild(elementIssued);

            attr = doc.createAttribute("element");
            attr.setValue("date");
            elementIssued.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("issued");
            elementIssued.setAttributeNode(attr);
        }

        // Add the author nodes:
        String[] authorSet = journalData.getAuthors();
        if (null != authorSet && authorSet.length > 0) {
            for (String author : authorSet) {
                Element elementAuthor = doc.createElement("dcvalue");
                elementAuthor.appendChild(doc.createTextNode(author));
                rootElement.appendChild(elementAuthor);

                attr = doc.createAttribute("element");
                attr.setValue("contributor");
                elementAuthor.setAttributeNode(attr);

                attr = doc.createAttribute("qualifier");
                attr.setValue("author");
                elementAuthor.setAttributeNode(attr);
            }
        }

        // Add the acknowledgements node:
        String ack = journalData.getAcknowledgements();
        if (null != ack) {
            Element elementAck = doc.createElement("dcvalue");
            elementAck.appendChild(doc.createTextNode(ack));
            rootElement.appendChild(elementAck);

            attr = doc.createAttribute("element");
            attr.setValue("description");
            elementAck.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementAck.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("none");
            elementAck.setAttributeNode(attr);
        }

        // Add the author contributions node:
        String contrib = journalData.getAuthorContributions();
        if (null != contrib) {
            Element elementContribution = doc.createElement("dcvalue");
            elementContribution.appendChild(doc.createTextNode(contrib));
            rootElement.appendChild(elementContribution);

            attr = doc.createAttribute("element");
            attr.setValue("description");
            elementContribution.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementContribution.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("none");
            elementContribution.setAttributeNode(attr);
        }

        // Add the publisher node:
        String puber = journalData.getPublisher();
        if (null != puber) {
            Element elementPublisher = doc.createElement("dcvalue");
            elementPublisher.appendChild(doc.createTextNode(puber));
            rootElement.appendChild(elementPublisher);

            attr = doc.createAttribute("element");
            attr.setValue("publisher");
            elementPublisher.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("none");
            elementPublisher.setAttributeNode(attr);
        }

        // Add the citation node:
        String cit = journalData.getCitation();
        if (null != cit) {
            Element elementCitation = doc.createElement("dcvalue");
            elementCitation.appendChild(doc.createTextNode(cit));
            rootElement.appendChild(elementCitation);

            attr = doc.createAttribute("element");
            attr.setValue("identifier");
            elementCitation.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementCitation.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("citation");
            elementCitation.setAttributeNode(attr);
        }

        // Add the rights node:
        String rit = journalData.getRights();
        if (null != rit) {
            Element elementRights = doc.createElement("dcvalue");
            elementRights.appendChild(doc.createTextNode(rit));
            rootElement.appendChild(elementRights);

            attr = doc.createAttribute("element");
            attr.setValue("rights");
            elementRights.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("none");
            elementRights.setAttributeNode(attr);
        }

        // Add the rights URI node:
        String ritUri = journalData.getRightsUri();
        if (null != ritUri) {
            Element elementRightsUri = doc.createElement("dcvalue");
            elementRightsUri.appendChild(doc.createTextNode(ritUri));
            rootElement.appendChild(elementRightsUri);

            attr = doc.createAttribute("element");
            attr.setValue("rights");
            elementRightsUri.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("uri");
            elementRightsUri.setAttributeNode(attr);
        }

        // Add the rights requestable node:
        Element elementRightsRequestable = doc.createElement("dcvalue");
        elementRightsRequestable
                .appendChild(doc.createTextNode(Boolean.toString(journalData.isRightsRequestable())));
        rootElement.appendChild(elementRightsRequestable);

        attr = doc.createAttribute("element");
        attr.setValue("rights");
        elementRightsRequestable.setAttributeNode(attr);

        attr = doc.createAttribute("language");
        attr.setValue("en_US");
        elementRightsRequestable.setAttributeNode(attr);

        attr = doc.createAttribute("qualifier");
        attr.setValue("requestable");
        elementRightsRequestable.setAttributeNode(attr);

        // Add the is part of node:
        String partOf = journalData.getIsPartOfSeries();
        if (null != partOf) {
            Element elementIsPartOf = doc.createElement("dcvalue");
            elementIsPartOf.appendChild(doc.createTextNode(partOf));
            rootElement.appendChild(elementIsPartOf);

            attr = doc.createAttribute("element");
            attr.setValue("relation");
            elementIsPartOf.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("ispartofseries");
            elementIsPartOf.setAttributeNode(attr);
        }

        // Add the relation uri node:
        String reUri = journalData.getRelationUri();
        if (null != reUri) {
            Element elementRelationUri = doc.createElement("dcvalue");
            elementRelationUri.appendChild(doc.createTextNode(reUri));
            rootElement.appendChild(elementRelationUri);

            attr = doc.createAttribute("element");
            attr.setValue("relation");
            elementRelationUri.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("uri");
            elementRelationUri.setAttributeNode(attr);
        }

        // Add the subject nodes:
        String[] subjectSet = journalData.getSubjects();
        if (null != subjectSet && subjectSet.length > 0) {
            for (String subject : subjectSet) {
                Element elementSubject = doc.createElement("dcvalue");
                elementSubject.appendChild(doc.createTextNode(subject));
                rootElement.appendChild(elementSubject);

                attr = doc.createAttribute("element");
                attr.setValue("subject");
                elementSubject.setAttributeNode(attr);

                attr = doc.createAttribute("language");
                attr.setValue("en_US");
                elementSubject.setAttributeNode(attr);

                attr = doc.createAttribute("qualifier");
                attr.setValue("none");
                elementSubject.setAttributeNode(attr);
            }
        }

        // Add the peerReview node:
        String review = journalData.getPeerReview();
        if (null != review) {
            Element elementPeerReview = doc.createElement("dcvalue");
            elementPeerReview.appendChild(doc.createTextNode(review));
            rootElement.appendChild(elementPeerReview);

            attr = doc.createAttribute("element");
            attr.setValue("description");
            elementPeerReview.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementPeerReview.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("peerreview");
            elementPeerReview.setAttributeNode(attr);
        }

        // Add the peer review notes node:
        String peer = journalData.getPeerReviewNotes();
        if (null != peer) {
            Element elementPeerReviewNotes = doc.createElement("dcvalue");
            elementPeerReviewNotes.appendChild(doc.createTextNode(peer));
            rootElement.appendChild(elementPeerReviewNotes);

            attr = doc.createAttribute("element");
            attr.setValue("description");
            elementPeerReviewNotes.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementPeerReviewNotes.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("peerreviewnotes");
            elementPeerReviewNotes.setAttributeNode(attr);
        }

        // Add the doi node:
        String doi = journalData.getDoi();
        if (null != doi) {
            Element elementDoi = doc.createElement("dcvalue");
            elementDoi.appendChild(doc.createTextNode(doi));
            rootElement.appendChild(elementDoi);

            attr = doc.createAttribute("element");
            attr.setValue("identifier");
            elementDoi.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementDoi.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("doi");
            elementDoi.setAttributeNode(attr);
        }

        String folderPath = setOutputPath(fileName);
        String filePath = folderPath + "/dublin_core.xml";
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(filePath));

        transformer.transform(source, result);

    } catch (ParserConfigurationException | TransformerException pce) {
        pce.printStackTrace();
    } catch (DOMException | BeansException e) {
        e.printStackTrace();
    }
}

From source file:org.shareok.data.plosdata.PlosData.java

/**
 * Generate the metadata xml file//from w  w  w  . jav a  2 s  . co  m
 * @param fileName 
 */
public void exportXmlByDoiData(String fileName) {

    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("dublin_core");
        doc.appendChild(rootElement);

        // Add the type node:
        Element element = doc.createElement("dcvalue");
        element.appendChild(doc.createTextNode("Research Article"));
        rootElement.appendChild(element);

        Attr attr = doc.createAttribute("element");
        attr.setValue("type");
        element.setAttributeNode(attr);

        attr = doc.createAttribute("language");
        attr.setValue("en_US");
        element.setAttributeNode(attr);

        attr = doc.createAttribute("qualifier");
        attr.setValue("none");
        element.setAttributeNode(attr);

        // Add the abstract node:
        String abs = getAbstractText();
        if (null != abs) {
            Element elementAbs = doc.createElement("dcvalue");
            elementAbs.appendChild(doc.createTextNode(abs));
            rootElement.appendChild(elementAbs);

            attr = doc.createAttribute("element");
            attr.setValue("description");
            elementAbs.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementAbs.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("abstract");
            elementAbs.setAttributeNode(attr);
        }

        // Add the language node:
        String lang = getLanguage();
        if (null != lang) {
            Element elementLang = doc.createElement("dcvalue");
            elementLang.appendChild(doc.createTextNode(lang));
            rootElement.appendChild(elementLang);

            attr = doc.createAttribute("element");
            attr.setValue("language");
            elementLang.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementLang.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("iso");
            elementLang.setAttributeNode(attr);
        }

        // Add the title node:
        String tit = getTitle();
        if (null != tit) {
            Element elementTitle = doc.createElement("dcvalue");
            elementTitle.appendChild(doc.createTextNode(tit));
            rootElement.appendChild(elementTitle);

            attr = doc.createAttribute("element");
            attr.setValue("title");
            elementTitle.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementTitle.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("none");
            elementTitle.setAttributeNode(attr);
        }

        // Add the available date node:
        //            Element elementAvailable = doc.createElement("dcvalue");
        //            elementAvailable.appendChild(doc.createTextNode(getDateAvailable().toString()));
        //            rootElement.appendChild(elementAvailable);
        //            
        //            attr = doc.createAttribute("element");
        //            attr.setValue("date");
        //            elementAvailable.setAttributeNode(attr);
        //            
        //            attr = doc.createAttribute("qualifier");
        //            attr.setValue("available");
        //            elementAvailable.setAttributeNode(attr);

        // Add the issued date node:
        Date issueDate = getDateIssued();
        if (null != issueDate) {
            SimpleDateFormat format_issuedDate = new SimpleDateFormat("yyyy-MM-dd");
            Element elementIssued = doc.createElement("dcvalue");
            elementIssued.appendChild(doc.createTextNode(format_issuedDate.format(issueDate)));
            rootElement.appendChild(elementIssued);

            attr = doc.createAttribute("element");
            attr.setValue("date");
            elementIssued.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("issued");
            elementIssued.setAttributeNode(attr);
        }

        // Add the author nodes:
        String[] authorSet = getAuthors();
        if (null != authorSet && authorSet.length > 0) {
            for (String author : authorSet) {
                Element elementAuthor = doc.createElement("dcvalue");
                elementAuthor.appendChild(doc.createTextNode(author));
                rootElement.appendChild(elementAuthor);

                attr = doc.createAttribute("element");
                attr.setValue("contributor");
                elementAuthor.setAttributeNode(attr);

                attr = doc.createAttribute("qualifier");
                attr.setValue("author");
                elementAuthor.setAttributeNode(attr);
            }
        }

        // Add the acknowledgements node:
        String ack = getAcknowledgements();
        if (null != ack) {
            Element elementAck = doc.createElement("dcvalue");
            elementAck.appendChild(doc.createTextNode(ack));
            rootElement.appendChild(elementAck);

            attr = doc.createAttribute("element");
            attr.setValue("description");
            elementAck.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementAck.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("none");
            elementAck.setAttributeNode(attr);
        }

        // Add the author contributions node:
        String contrib = getAuthorContributions();
        if (null != contrib) {
            Element elementContribution = doc.createElement("dcvalue");
            elementContribution.appendChild(doc.createTextNode(contrib));
            rootElement.appendChild(elementContribution);

            attr = doc.createAttribute("element");
            attr.setValue("description");
            elementContribution.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementContribution.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("none");
            elementContribution.setAttributeNode(attr);
        }

        // Add the publisher node:
        String puber = getPublisher();
        if (null != puber) {
            Element elementPublisher = doc.createElement("dcvalue");
            elementPublisher.appendChild(doc.createTextNode(puber));
            rootElement.appendChild(elementPublisher);

            attr = doc.createAttribute("element");
            attr.setValue("publisher");
            elementPublisher.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("none");
            elementPublisher.setAttributeNode(attr);
        }

        // Add the citation node:
        String cit = getCitation();
        if (null != cit) {
            Element elementCitation = doc.createElement("dcvalue");
            elementCitation.appendChild(doc.createTextNode(cit));
            rootElement.appendChild(elementCitation);

            attr = doc.createAttribute("element");
            attr.setValue("identifier");
            elementCitation.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementCitation.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("citation");
            elementCitation.setAttributeNode(attr);
        }

        // Add the rights node:
        String rit = getRights();
        if (null != rit) {
            Element elementRights = doc.createElement("dcvalue");
            elementRights.appendChild(doc.createTextNode(rit));
            rootElement.appendChild(elementRights);

            attr = doc.createAttribute("element");
            attr.setValue("rights");
            elementRights.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("none");
            elementRights.setAttributeNode(attr);
        }

        // Add the rights URI node:
        String ritUri = getRightsUri();
        if (null != ritUri) {
            Element elementRightsUri = doc.createElement("dcvalue");
            elementRightsUri.appendChild(doc.createTextNode(ritUri));
            rootElement.appendChild(elementRightsUri);

            attr = doc.createAttribute("element");
            attr.setValue("rights");
            elementRightsUri.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("uri");
            elementRightsUri.setAttributeNode(attr);
        }

        // Add the rights requestable node:
        Element elementRightsRequestable = doc.createElement("dcvalue");
        elementRightsRequestable.appendChild(doc.createTextNode(Boolean.toString(isRightsRequestable())));
        rootElement.appendChild(elementRightsRequestable);

        attr = doc.createAttribute("element");
        attr.setValue("rights");
        elementRightsRequestable.setAttributeNode(attr);

        attr = doc.createAttribute("language");
        attr.setValue("en_US");
        elementRightsRequestable.setAttributeNode(attr);

        attr = doc.createAttribute("qualifier");
        attr.setValue("requestable");
        elementRightsRequestable.setAttributeNode(attr);

        // Add the is part of node:
        String partOf = getIsPartOfSeries();
        if (null != partOf) {
            Element elementIsPartOf = doc.createElement("dcvalue");
            elementIsPartOf.appendChild(doc.createTextNode(partOf));
            rootElement.appendChild(elementIsPartOf);

            attr = doc.createAttribute("element");
            attr.setValue("relation");
            elementIsPartOf.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("ispartofseries");
            elementIsPartOf.setAttributeNode(attr);
        }

        // Add the relation uri node:
        String reUri = getRelationUri();
        if (null != reUri) {
            Element elementRelationUri = doc.createElement("dcvalue");
            elementRelationUri.appendChild(doc.createTextNode(reUri));
            rootElement.appendChild(elementRelationUri);

            attr = doc.createAttribute("element");
            attr.setValue("relation");
            elementRelationUri.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("uri");
            elementRelationUri.setAttributeNode(attr);
        }

        // Add the subject nodes:
        String[] subjectSet = getSubjects();
        if (null != subjectSet && subjectSet.length > 0) {
            for (String subject : subjectSet) {
                Element elementSubject = doc.createElement("dcvalue");
                elementSubject.appendChild(doc.createTextNode(subject));
                rootElement.appendChild(elementSubject);

                attr = doc.createAttribute("element");
                attr.setValue("subject");
                elementSubject.setAttributeNode(attr);

                attr = doc.createAttribute("language");
                attr.setValue("en_US");
                elementSubject.setAttributeNode(attr);

                attr = doc.createAttribute("qualifier");
                attr.setValue("none");
                elementSubject.setAttributeNode(attr);
            }
        }

        // Add the peerReview node:
        String review = getPeerReview();
        if (null != review) {
            Element elementPeerReview = doc.createElement("dcvalue");
            elementPeerReview.appendChild(doc.createTextNode(review));
            rootElement.appendChild(elementPeerReview);

            attr = doc.createAttribute("element");
            attr.setValue("description");
            elementPeerReview.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementPeerReview.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("peerreview");
            elementPeerReview.setAttributeNode(attr);
        }

        // Add the peer review notes node:
        String peer = getPeerReviewNotes();
        if (null != peer) {
            Element elementPeerReviewNotes = doc.createElement("dcvalue");
            elementPeerReviewNotes.appendChild(doc.createTextNode(peer));
            rootElement.appendChild(elementPeerReviewNotes);

            attr = doc.createAttribute("element");
            attr.setValue("description");
            elementPeerReviewNotes.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementPeerReviewNotes.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("peerreviewnotes");
            elementPeerReviewNotes.setAttributeNode(attr);
        }

        // Add the doi node:
        String doi = getDoi();
        if (null != doi) {
            Element elementDoi = doc.createElement("dcvalue");
            elementDoi.appendChild(doc.createTextNode(doi));
            rootElement.appendChild(elementDoi);

            attr = doc.createAttribute("element");
            attr.setValue("identifier");
            elementDoi.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementDoi.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("doi");
            elementDoi.setAttributeNode(attr);
        }

        // Generate the xml file:
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(fileName));

        transformer.transform(source, result);
    } catch (ParserConfigurationException | TransformerException pce) {
        pce.printStackTrace();
        System.exit(0);
    } catch (DOMException | BeansException e) {
        e.printStackTrace();
        System.exit(0);
    }
}

From source file:com.dinochiesa.edgecallouts.EditXmlNode.java

private void execute0(Document document, MessageContext msgCtxt) throws Exception {
    String xpath = getXpath(msgCtxt);
    XPathEvaluator xpe = getXpe(msgCtxt);
    NodeList nodes = (NodeList) xpe.evaluate(xpath, document, XPathConstants.NODESET);
    validate(nodes);//from ww w.j a  v  a 2 s . com
    EditAction action = getAction(msgCtxt);
    if (action == EditAction.Remove) {
        remove(nodes);
        return;
    }

    short newNodeType = getNewNodeType(msgCtxt);
    String text = getNewNodeText(msgCtxt);
    Node newNode = null;
    switch (newNodeType) {
    case Node.ELEMENT_NODE:
        // Create a duplicate node and transfer ownership of the
        // new node into the destination document.
        Document temp = XmlUtils.parseXml(text);
        newNode = document.importNode(temp.getDocumentElement(), true);
        break;
    case Node.ATTRIBUTE_NODE:
        if (text.indexOf("=") < 1) {
            throw new IllegalStateException("attribute spec must be name=value");
        }
        String[] parts = text.split("=", 2);
        if (parts.length != 2)
            throw new IllegalStateException("attribute spec must be name=value");
        Attr attr = document.createAttribute(parts[0]);
        attr.setValue(parts[1]);
        newNode = attr;
        break;
    case Node.TEXT_NODE:
        newNode = document.createTextNode(text);
        break;
    }
    switch (action) {
    case InsertBefore:
        insertBefore(nodes, newNode, newNodeType);
        break;
    case Append:
        append(nodes, newNode, newNodeType);
        break;
    case Replace:
        replace(nodes, newNode, newNodeType);
        break;
    }
}

From source file:edu.lternet.pasta.datapackagemanager.LevelOneEMLFactory.java

private void modifyAccessElementAttributes(Document emlDocument) throws TransformerException {
    CachedXPathAPI xpathapi = new CachedXPathAPI();

    // Parse the access elements
    NodeList accessNodeList = xpathapi.selectNodeList(emlDocument, ACCESS_PATH);
    if (accessNodeList != null) {
        for (int i = 0; i < accessNodeList.getLength(); i++) {
            boolean hasSystemAttribute = false;
            Element accessElement = (Element) accessNodeList.item(i);
            NamedNodeMap accessAttributesList = accessElement.getAttributes();

            for (int j = 0; j < accessAttributesList.getLength(); j++) {
                Node attributeNode = accessAttributesList.item(j);
                String nodeName = attributeNode.getNodeName();
                String nodeValue = attributeNode.getNodeValue();
                if (nodeName.equals("authSystem")) {
                    attributeNode.setNodeValue(LEVEL_ONE_AUTH_SYSTEM_ATTRIBUTE);
                } else if (nodeName.equals("system")) {
                    attributeNode.setNodeValue(LEVEL_ONE_SYSTEM_ATTRIBUTE);
                    hasSystemAttribute = true;
                }/*from ww w.  j  a  v  a  2s  .c o m*/
            }

            /*
             * No @system attribute was found in the access element, so we
             * need to add one.
             */
            if (!hasSystemAttribute) {
                Attr systemAttribute = emlDocument.createAttribute("system");
                systemAttribute.setTextContent(LEVEL_ONE_SYSTEM_ATTRIBUTE);
                accessElement.setAttributeNode(systemAttribute);
            }
        }
    }
}

From source file:org.sakaiproject.lessonbuildertool.service.LessonBuilderEntityProducer.java

protected void addAttr(Document doc, Element element, String name, String value) {
    if (value == null)
        return;/*  ww w . j a v a 2 s .c o  m*/
    Attr attr = doc.createAttribute(name);
    attr.setValue(value);
    element.setAttributeNode(attr);
}