Example usage for org.w3c.dom Attr setValue

List of usage examples for org.w3c.dom Attr setValue

Introduction

In this page you can find the example usage for org.w3c.dom Attr setValue.

Prototype

public void setValue(String value) throws DOMException;

Source Link

Document

On retrieval, the value of the attribute is returned as a string.

Usage

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);/*from www  .  java 2 s.  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.amalto.core.storage.hibernate.MappingGenerator.java

private Element handleSimpleField(FieldMetadata field) {
    if (isDoingColumns) {
        Element column = document.createElement("column"); //$NON-NLS-1$
        Attr columnName = document.createAttribute("name"); //$NON-NLS-1$
        String columnNameValue = resolver.get(field, compositeKeyPrefix);
        columnName.setValue(columnNameValue);
        column.getAttributes().setNamedItem(columnName);
        // TMDM-9003: should add not-null="false" explicitly if not-null isn't "true" for many-to-many scenario 
        Attr notNull = document.createAttribute("not-null"); //$NON-NLS-1$
        if (generateConstrains && isColumnMandatory) {
            notNull.setValue(Boolean.TRUE.toString());
        } else {/*from  w w  w .  j  av a2 s  .  com*/
            notNull.setValue(Boolean.FALSE.toString());
        }
        column.getAttributes().setNamedItem(notNull);

        if (resolver.isIndexed(field)) { // Create indexes for fields that should be indexed.
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Creating index for field '" + field.getName() + "'."); //$NON-NLS-1$ //$NON-NLS-2$
            }
            Attr indexName = document.createAttribute("index"); //$NON-NLS-1$
            setIndexName(field, columnNameValue, indexName);
            column.getAttributes().setNamedItem(indexName);
        }
        parentElement.appendChild(column);
        return column;
    }
    if (field.isKey()) {
        Element idElement;
        if (!compositeId) {
            idElement = document.createElement("id"); //$NON-NLS-1$
            if (Types.UUID.equals(field.getType().getName())
                    && ScatteredMappingCreator.GENERATED_ID.equals(field.getName())) {
                // <generator class="org.hibernate.id.UUIDGenerator"/>
                Element generator = document.createElement("generator"); //$NON-NLS-1$
                Attr generatorClass = document.createAttribute("class"); //$NON-NLS-1$
                generatorClass.setValue("org.hibernate.id.UUIDGenerator"); //$NON-NLS-1$
                generator.getAttributes().setNamedItem(generatorClass);
                idElement.appendChild(generator);
            }
        } else {
            idElement = document.createElement("key-property"); //$NON-NLS-1$
        }
        Attr idName = document.createAttribute("name"); //$NON-NLS-1$
        idName.setValue(field.getName());
        Attr columnName = document.createAttribute("column"); //$NON-NLS-1$
        columnName.setValue(resolver.get(field));

        idElement.getAttributes().setNamedItem(idName);
        idElement.getAttributes().setNamedItem(columnName);
        if (field.getType().getData(MetadataRepository.DATA_MAX_LENGTH) != null) {
            Attr length = document.createAttribute("length"); //$NON-NLS-1$
            String value = field.getType().<String>getData(MetadataRepository.DATA_MAX_LENGTH);
            length.setValue(value);
            idElement.getAttributes().setNamedItem(length);
        }
        return idElement;
    } else {
        if (!field.isMany()) {
            Element propertyElement = document.createElement("property"); //$NON-NLS-1$
            Attr propertyName = document.createAttribute("name"); //$NON-NLS-1$
            propertyName.setValue(field.getName());
            Element columnElement = document.createElement("column"); //$NON-NLS-1$
            Attr columnName = document.createAttribute("name"); //$NON-NLS-1$
            columnName.setValue(resolver.get(field));

            if (resolver.isIndexed(field)) { // Create indexes for fields that should be indexed.
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Creating index for field '" + field.getName() + "'."); //$NON-NLS-1$ //$NON-NLS-2$
                }
                Attr indexName = document.createAttribute("index"); //$NON-NLS-1$
                setIndexName(field, field.getName(), indexName);
                propertyElement.getAttributes().setNamedItem(indexName);
            } else {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("*Not* creating index for field '" + field.getName() + "'."); //$NON-NLS-1$ //$NON-NLS-2$
                }
            }
            // Not null
            Attr notNull = document.createAttribute("not-null"); //$NON-NLS-1$
            if (generateConstrains && field.isMandatory()) {
                notNull.setValue(Boolean.TRUE.toString());
            } else {
                notNull.setValue(Boolean.FALSE.toString());
            }
            columnElement.getAttributes().setNamedItem(notNull);
            // default value
            addDefaultValueAttribute(field, columnElement);

            addFieldTypeAttribute(field, columnElement, propertyElement, dataSource.getDialectName());
            propertyElement.getAttributes().setNamedItem(propertyName);
            columnElement.getAttributes().setNamedItem(columnName);
            propertyElement.appendChild(columnElement);
            return propertyElement;
        } else {
            Element listElement = document.createElement("list"); //$NON-NLS-1$
            Attr name = document.createAttribute("name"); //$NON-NLS-1$
            name.setValue(field.getName());
            Attr tableName = document.createAttribute("table"); //$NON-NLS-1$
            tableName.setValue(resolver.getCollectionTable(field));
            listElement.getAttributes().setNamedItem(tableName);
            if (field.getContainingType().getKeyFields().size() == 1) {
                // lazy="extra"
                Attr lazyAttribute = document.createAttribute("lazy"); //$NON-NLS-1$
                lazyAttribute.setValue("extra"); //$NON-NLS-1$
                listElement.getAttributes().setNamedItem(lazyAttribute);
                // fetch="join"
                Attr fetchAttribute = document.createAttribute("fetch"); //$NON-NLS-1$
                fetchAttribute.setValue("join"); //$NON-NLS-1$
                listElement.getAttributes().setNamedItem(fetchAttribute);
                // inverse="true"
                Attr inverse = document.createAttribute("inverse"); //$NON-NLS-1$
                inverse.setValue("false"); //$NON-NLS-1$
                listElement.getAttributes().setNamedItem(inverse);
            } else {
                /*
                 * Hibernate does not handle correctly reverse collection when main entity owns multiple keys.
                 */
                // lazy="false"
                Attr lazyAttribute = document.createAttribute("lazy"); //$NON-NLS-1$
                lazyAttribute.setValue("false"); //$NON-NLS-1$
                listElement.getAttributes().setNamedItem(lazyAttribute);
                // In case containing type has > 1 key, switch to fetch="select" since Hibernate returns incorrect
                // results in case of fetch="join".
                Attr fetchAttribute = document.createAttribute("fetch"); //$NON-NLS-1$
                fetchAttribute.setValue("select"); //$NON-NLS-1$
                listElement.getAttributes().setNamedItem(fetchAttribute);
                // batch-size="20"
                Attr batchSize = document.createAttribute("batch-size"); //$NON-NLS-1$
                batchSize.setValue("20"); //$NON-NLS-1$
                listElement.getAttributes().setNamedItem(batchSize);
            }
            // cascade="delete"
            Attr cascade = document.createAttribute("cascade"); //$NON-NLS-1$
            cascade.setValue("lock, all-delete-orphan"); //$NON-NLS-1$
            listElement.getAttributes().setNamedItem(cascade);
            // Keys
            Element key = document.createElement("key"); //$NON-NLS-1$
            Collection<FieldMetadata> keyFields = field.getContainingType().getKeyFields();
            for (FieldMetadata keyField : keyFields) {
                Element column = document.createElement("column"); //$NON-NLS-1$
                Attr columnName = document.createAttribute("name"); //$NON-NLS-1$
                column.getAttributes().setNamedItem(columnName);
                columnName.setValue(resolver.get(keyField));
                key.appendChild(column);
            }
            // <element column="name" type="string"/>
            Element element = document.createElement("element"); //$NON-NLS-1$
            Element columnElement = document.createElement("column"); //$NON-NLS-1$
            Attr columnNameAttr = document.createAttribute("name"); //$NON-NLS-1$
            columnNameAttr.setValue("value"); //$NON-NLS-1$

            // default value
            addDefaultValueAttribute(field, columnElement);

            columnElement.getAttributes().setNamedItem(columnNameAttr);
            addFieldTypeAttribute(field, columnElement, element, dataSource.getDialectName());
            element.appendChild(columnElement);
            // Not null warning
            if (field.isMandatory()) {
                LOGGER.warn("Field '" + field.getName() //$NON-NLS-1$
                        + "' is mandatory and a collection. Constraint can not be expressed in database schema."); //$NON-NLS-1$
            }
            // <index column="pos" />
            Element index = document.createElement("list-index"); //$NON-NLS-1$
            Attr indexColumn = document.createAttribute("column"); //$NON-NLS-1$
            indexColumn.setValue("pos"); //$NON-NLS-1$
            index.getAttributes().setNamedItem(indexColumn);
            listElement.getAttributes().setNamedItem(name);
            listElement.appendChild(key);
            listElement.appendChild(index);
            listElement.appendChild(element);
            return listElement;
        }
    }
}

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 ww  .j  a  v a  2  s.  c om
 * @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 ww w . j a v a2  s. c  o m
 * 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//  w ww  .  jav a  2  s  . com
 * @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:org.shareok.data.sagedata.SageSourceDataHandlerImpl.java

/** 
 * Convert the article data to dublin core xml metadata and save the the file
 * /*from  w  w  w.j av a  2  s .  com*/
 * @param journalData : the SageJournalData
 * @param fileName : the root folder contains all the uploading article data
 */
public void exportXmlByJournalData(SageJournalData journalData, String outputPath) {

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

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

        // Add the type node:
        org.w3c.dom.Element element = doc.createElement("dcvalue");
        element.appendChild(doc.createTextNode(journalData.getType()));
        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) {
            org.w3c.dom.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) {
            org.w3c.dom.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) {
            org.w3c.dom.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");
            org.w3c.dom.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) {
                org.w3c.dom.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) {
            org.w3c.dom.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) {
            org.w3c.dom.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) {
            org.w3c.dom.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) {
            org.w3c.dom.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) {
            org.w3c.dom.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) {
            org.w3c.dom.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:
        org.w3c.dom.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) {
            org.w3c.dom.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) {
            org.w3c.dom.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) {
                org.w3c.dom.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) {
            org.w3c.dom.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) {
            org.w3c.dom.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) {
            org.w3c.dom.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);
        }

        File outputFolder = new File(outputPath + File.separator + journalData.getDoi().replaceAll("/", "."));
        if (!outputFolder.exists()) {
            outputFolder.mkdirs();
        }
        String filePath = outputFolder + File.separator + "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 | DOMException | BeansException pce) {
        pce.printStackTrace();
    } catch (TransformerException ex) {
        Logger.getLogger(SageSourceDataHandlerImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.evolveum.midpoint.util.DOMUtil.java

private static void setQNameAttribute(Element element, Attr attr, QName attributeQnameValue,
        Element definitionElement) {
    String attributeStringValue;//from   w  ww.  j  a va  2  s  .c  om

    if (attributeQnameValue == null) {
        attributeStringValue = "";
    } else if (XMLConstants.NULL_NS_URI.equals(attributeQnameValue.getNamespaceURI())) {
        if (QNameUtil.isPrefixUndeclared(attributeQnameValue.getPrefix())) {
            attributeStringValue = attributeQnameValue.getPrefix() + ":" + attributeQnameValue.getLocalPart(); // to give user a chance to see and fix this
        } else {
            attributeStringValue = attributeQnameValue.getLocalPart();
        }
    } else {
        String valuePrefix = lookupOrCreateNamespaceDeclaration(element, attributeQnameValue.getNamespaceURI(),
                attributeQnameValue.getPrefix(), definitionElement, false);
        assert StringUtils.isNotBlank(valuePrefix);
        attributeStringValue = valuePrefix + ":" + attributeQnameValue.getLocalPart();
    }

    NamedNodeMap attributes = element.getAttributes();
    checkValidXmlChars(attributeStringValue);
    attr.setValue(attributeStringValue);
    attributes.setNamedItem(attr);
}

From source file:com.microsoft.windowsazure.management.sql.DacOperationsImpl.java

/**
* Exports an Azure SQL Database into a DACPAC file in Azure Blob Storage.
*
* @param serverName Required. The name of the Azure SQL Database Server in
* which the database to export resides.//from   ww  w.  j  a v a 2 s .  com
* @param parameters Optional. The parameters needed to initiate the export
* request.
* @throws ParserConfigurationException Thrown if there was an error
* configuring the parser for the response body.
* @throws SAXException Thrown if there was an error parsing the response
* body.
* @throws TransformerException Thrown if there was an error creating the
* DOM transformer.
* @throws IOException Signals that an I/O exception of some sort has
* occurred. This class is the general class of exceptions produced by
* failed or interrupted I/O operations.
* @throws ServiceException Thrown if an unexpected response is found.
* @return Represents the response that the service returns once an import
* or export operation has been initiated.
*/
@Override
public DacImportExportResponse export(String serverName, DacExportParameters parameters)
        throws ParserConfigurationException, SAXException, TransformerException, IOException, ServiceException {
    // Validate
    if (serverName == null) {
        throw new NullPointerException("serverName");
    }
    if (parameters != null) {
        if (parameters.getBlobCredentials() != null) {
            if (parameters.getBlobCredentials().getStorageAccessKey() == null) {
                throw new NullPointerException("parameters.BlobCredentials.StorageAccessKey");
            }
            if (parameters.getBlobCredentials().getUri() == null) {
                throw new NullPointerException("parameters.BlobCredentials.Uri");
            }
        }
        if (parameters.getConnectionInfo() != null) {
            if (parameters.getConnectionInfo().getDatabaseName() == null) {
                throw new NullPointerException("parameters.ConnectionInfo.DatabaseName");
            }
            if (parameters.getConnectionInfo().getPassword() == null) {
                throw new NullPointerException("parameters.ConnectionInfo.Password");
            }
            if (parameters.getConnectionInfo().getServerName() == null) {
                throw new NullPointerException("parameters.ConnectionInfo.ServerName");
            }
            if (parameters.getConnectionInfo().getUserName() == null) {
                throw new NullPointerException("parameters.ConnectionInfo.UserName");
            }
        }
    }

    // Tracing
    boolean shouldTrace = CloudTracing.getIsEnabled();
    String invocationId = null;
    if (shouldTrace) {
        invocationId = Long.toString(CloudTracing.getNextInvocationId());
        HashMap<String, Object> tracingParameters = new HashMap<String, Object>();
        tracingParameters.put("serverName", serverName);
        tracingParameters.put("parameters", parameters);
        CloudTracing.enter(invocationId, this, "exportAsync", tracingParameters);
    }

    // Construct URL
    String url = "";
    url = url + "/";
    if (this.getClient().getCredentials().getSubscriptionId() != null) {
        url = url + URLEncoder.encode(this.getClient().getCredentials().getSubscriptionId(), "UTF-8");
    }
    url = url + "/services/sqlservers/servers/";
    url = url + URLEncoder.encode(serverName, "UTF-8");
    url = url + "/DacOperations/Export";
    String baseUrl = this.getClient().getBaseUri().toString();
    // Trim '/' character from the end of baseUrl and beginning of url.
    if (baseUrl.charAt(baseUrl.length() - 1) == '/') {
        baseUrl = baseUrl.substring(0, (baseUrl.length() - 1) + 0);
    }
    if (url.charAt(0) == '/') {
        url = url.substring(1);
    }
    url = baseUrl + "/" + url;
    url = url.replace(" ", "%20");

    // Create HTTP transport objects
    HttpPost httpRequest = new HttpPost(url);

    // Set Headers
    httpRequest.setHeader("Content-Type", "application/xml");
    httpRequest.setHeader("x-ms-version", "2012-03-01");

    // Serialize Request
    String requestContent = null;
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document requestDoc = documentBuilder.newDocument();

    if (parameters != null) {
        Element exportInputElement = requestDoc.createElementNS(
                "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes",
                "ExportInput");
        requestDoc.appendChild(exportInputElement);

        if (parameters.getBlobCredentials() != null) {
            Element blobCredentialsElement = requestDoc.createElementNS(
                    "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes",
                    "BlobCredentials");
            exportInputElement.appendChild(blobCredentialsElement);

            Attr typeAttribute = requestDoc.createAttributeNS("http://www.w3.org/2001/XMLSchema-instance",
                    "type");
            typeAttribute.setValue("BlobStorageAccessKeyCredentials");
            blobCredentialsElement.setAttributeNode(typeAttribute);

            Element uriElement = requestDoc.createElementNS(
                    "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes",
                    "Uri");
            uriElement.appendChild(
                    requestDoc.createTextNode(parameters.getBlobCredentials().getUri().toString()));
            blobCredentialsElement.appendChild(uriElement);

            Element storageAccessKeyElement = requestDoc.createElementNS(
                    "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes",
                    "StorageAccessKey");
            storageAccessKeyElement.appendChild(
                    requestDoc.createTextNode(parameters.getBlobCredentials().getStorageAccessKey()));
            blobCredentialsElement.appendChild(storageAccessKeyElement);
        }

        if (parameters.getConnectionInfo() != null) {
            Element connectionInfoElement = requestDoc.createElementNS(
                    "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes",
                    "ConnectionInfo");
            exportInputElement.appendChild(connectionInfoElement);

            Element databaseNameElement = requestDoc.createElementNS(
                    "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes",
                    "DatabaseName");
            databaseNameElement
                    .appendChild(requestDoc.createTextNode(parameters.getConnectionInfo().getDatabaseName()));
            connectionInfoElement.appendChild(databaseNameElement);

            Element passwordElement = requestDoc.createElementNS(
                    "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes",
                    "Password");
            passwordElement
                    .appendChild(requestDoc.createTextNode(parameters.getConnectionInfo().getPassword()));
            connectionInfoElement.appendChild(passwordElement);

            Element serverNameElement = requestDoc.createElementNS(
                    "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes",
                    "ServerName");
            serverNameElement
                    .appendChild(requestDoc.createTextNode(parameters.getConnectionInfo().getServerName()));
            connectionInfoElement.appendChild(serverNameElement);

            Element userNameElement = requestDoc.createElementNS(
                    "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes",
                    "UserName");
            userNameElement
                    .appendChild(requestDoc.createTextNode(parameters.getConnectionInfo().getUserName()));
            connectionInfoElement.appendChild(userNameElement);
        }
    }

    DOMSource domSource = new DOMSource(requestDoc);
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(domSource, streamResult);
    requestContent = stringWriter.toString();
    StringEntity entity = new StringEntity(requestContent);
    httpRequest.setEntity(entity);
    httpRequest.setHeader("Content-Type", "application/xml");

    // Send Request
    HttpResponse httpResponse = null;
    try {
        if (shouldTrace) {
            CloudTracing.sendRequest(invocationId, httpRequest);
        }
        httpResponse = this.getClient().getHttpClient().execute(httpRequest);
        if (shouldTrace) {
            CloudTracing.receiveResponse(invocationId, httpResponse);
        }
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            ServiceException ex = ServiceException.createFromXml(httpRequest, requestContent, httpResponse,
                    httpResponse.getEntity());
            if (shouldTrace) {
                CloudTracing.error(invocationId, ex);
            }
            throw ex;
        }

        // Create Result
        DacImportExportResponse result = null;
        // Deserialize Response
        if (statusCode == HttpStatus.SC_OK) {
            InputStream responseContent = httpResponse.getEntity().getContent();
            result = new DacImportExportResponse();
            DocumentBuilderFactory documentBuilderFactory2 = DocumentBuilderFactory.newInstance();
            documentBuilderFactory2.setNamespaceAware(true);
            DocumentBuilder documentBuilder2 = documentBuilderFactory2.newDocumentBuilder();
            Document responseDoc = documentBuilder2.parse(new BOMInputStream(responseContent));

            Element guidElement = XmlUtility.getElementByTagNameNS(responseDoc,
                    "http://schemas.microsoft.com/2003/10/Serialization/", "guid");
            if (guidElement != null) {
                result.setGuid(guidElement.getTextContent());
            }

        }
        result.setStatusCode(statusCode);
        if (httpResponse.getHeaders("x-ms-request-id").length > 0) {
            result.setRequestId(httpResponse.getFirstHeader("x-ms-request-id").getValue());
        }

        if (shouldTrace) {
            CloudTracing.exit(invocationId, result);
        }
        return result;
    } finally {
        if (httpResponse != null && httpResponse.getEntity() != null) {
            httpResponse.getEntity().getContent().close();
        }
    }
}

From source file:com.microsoft.windowsazure.management.sql.DacOperationsImpl.java

/**
* Initiates an Import of a DACPAC file from Azure Blob Storage into a Azure
* SQL Database./*  ww  w  .j  a v  a  2  s.c o m*/
*
* @param serverName Required. The name of the Azure SQL Database Server
* into which the database is being imported.
* @param parameters Optional. The parameters needed to initiated the Import
* request.
* @throws ParserConfigurationException Thrown if there was an error
* configuring the parser for the response body.
* @throws SAXException Thrown if there was an error parsing the response
* body.
* @throws TransformerException Thrown if there was an error creating the
* DOM transformer.
* @throws IOException Signals that an I/O exception of some sort has
* occurred. This class is the general class of exceptions produced by
* failed or interrupted I/O operations.
* @throws ServiceException Thrown if an unexpected response is found.
* @return Represents the response that the service returns once an import
* or export operation has been initiated.
*/
@Override
public DacImportExportResponse importMethod(String serverName, DacImportParameters parameters)
        throws ParserConfigurationException, SAXException, TransformerException, IOException, ServiceException {
    // Validate
    if (serverName == null) {
        throw new NullPointerException("serverName");
    }
    if (parameters != null) {
        if (parameters.getBlobCredentials() != null) {
            if (parameters.getBlobCredentials().getStorageAccessKey() == null) {
                throw new NullPointerException("parameters.BlobCredentials.StorageAccessKey");
            }
            if (parameters.getBlobCredentials().getUri() == null) {
                throw new NullPointerException("parameters.BlobCredentials.Uri");
            }
        }
        if (parameters.getConnectionInfo() != null) {
            if (parameters.getConnectionInfo().getDatabaseName() == null) {
                throw new NullPointerException("parameters.ConnectionInfo.DatabaseName");
            }
            if (parameters.getConnectionInfo().getPassword() == null) {
                throw new NullPointerException("parameters.ConnectionInfo.Password");
            }
            if (parameters.getConnectionInfo().getServerName() == null) {
                throw new NullPointerException("parameters.ConnectionInfo.ServerName");
            }
            if (parameters.getConnectionInfo().getUserName() == null) {
                throw new NullPointerException("parameters.ConnectionInfo.UserName");
            }
        }
    }

    // Tracing
    boolean shouldTrace = CloudTracing.getIsEnabled();
    String invocationId = null;
    if (shouldTrace) {
        invocationId = Long.toString(CloudTracing.getNextInvocationId());
        HashMap<String, Object> tracingParameters = new HashMap<String, Object>();
        tracingParameters.put("serverName", serverName);
        tracingParameters.put("parameters", parameters);
        CloudTracing.enter(invocationId, this, "importMethodAsync", tracingParameters);
    }

    // Construct URL
    String url = "";
    url = url + "/";
    if (this.getClient().getCredentials().getSubscriptionId() != null) {
        url = url + URLEncoder.encode(this.getClient().getCredentials().getSubscriptionId(), "UTF-8");
    }
    url = url + "/services/sqlservers/servers/";
    url = url + URLEncoder.encode(serverName, "UTF-8");
    url = url + "/DacOperations/Import";
    String baseUrl = this.getClient().getBaseUri().toString();
    // Trim '/' character from the end of baseUrl and beginning of url.
    if (baseUrl.charAt(baseUrl.length() - 1) == '/') {
        baseUrl = baseUrl.substring(0, (baseUrl.length() - 1) + 0);
    }
    if (url.charAt(0) == '/') {
        url = url.substring(1);
    }
    url = baseUrl + "/" + url;
    url = url.replace(" ", "%20");

    // Create HTTP transport objects
    HttpPost httpRequest = new HttpPost(url);

    // Set Headers
    httpRequest.setHeader("Content-Type", "application/xml");
    httpRequest.setHeader("x-ms-version", "2012-03-01");

    // Serialize Request
    String requestContent = null;
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document requestDoc = documentBuilder.newDocument();

    if (parameters != null) {
        Element importInputElement = requestDoc.createElementNS(
                "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes",
                "ImportInput");
        requestDoc.appendChild(importInputElement);

        if (parameters.getAzureEdition() != null) {
            Element azureEditionElement = requestDoc.createElementNS(
                    "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes",
                    "AzureEdition");
            azureEditionElement.appendChild(requestDoc.createTextNode(parameters.getAzureEdition()));
            importInputElement.appendChild(azureEditionElement);
        }

        if (parameters.getBlobCredentials() != null) {
            Element blobCredentialsElement = requestDoc.createElementNS(
                    "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes",
                    "BlobCredentials");
            importInputElement.appendChild(blobCredentialsElement);

            Attr typeAttribute = requestDoc.createAttributeNS("http://www.w3.org/2001/XMLSchema-instance",
                    "type");
            typeAttribute.setValue("BlobStorageAccessKeyCredentials");
            blobCredentialsElement.setAttributeNode(typeAttribute);

            Element uriElement = requestDoc.createElementNS(
                    "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes",
                    "Uri");
            uriElement.appendChild(
                    requestDoc.createTextNode(parameters.getBlobCredentials().getUri().toString()));
            blobCredentialsElement.appendChild(uriElement);

            Element storageAccessKeyElement = requestDoc.createElementNS(
                    "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes",
                    "StorageAccessKey");
            storageAccessKeyElement.appendChild(
                    requestDoc.createTextNode(parameters.getBlobCredentials().getStorageAccessKey()));
            blobCredentialsElement.appendChild(storageAccessKeyElement);
        }

        if (parameters.getConnectionInfo() != null) {
            Element connectionInfoElement = requestDoc.createElementNS(
                    "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes",
                    "ConnectionInfo");
            importInputElement.appendChild(connectionInfoElement);

            Element databaseNameElement = requestDoc.createElementNS(
                    "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes",
                    "DatabaseName");
            databaseNameElement
                    .appendChild(requestDoc.createTextNode(parameters.getConnectionInfo().getDatabaseName()));
            connectionInfoElement.appendChild(databaseNameElement);

            Element passwordElement = requestDoc.createElementNS(
                    "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes",
                    "Password");
            passwordElement
                    .appendChild(requestDoc.createTextNode(parameters.getConnectionInfo().getPassword()));
            connectionInfoElement.appendChild(passwordElement);

            Element serverNameElement = requestDoc.createElementNS(
                    "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes",
                    "ServerName");
            serverNameElement
                    .appendChild(requestDoc.createTextNode(parameters.getConnectionInfo().getServerName()));
            connectionInfoElement.appendChild(serverNameElement);

            Element userNameElement = requestDoc.createElementNS(
                    "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes",
                    "UserName");
            userNameElement
                    .appendChild(requestDoc.createTextNode(parameters.getConnectionInfo().getUserName()));
            connectionInfoElement.appendChild(userNameElement);
        }

        Element databaseSizeInGBElement = requestDoc.createElementNS(
                "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes",
                "DatabaseSizeInGB");
        databaseSizeInGBElement
                .appendChild(requestDoc.createTextNode(Integer.toString(parameters.getDatabaseSizeInGB())));
        importInputElement.appendChild(databaseSizeInGBElement);
    }

    DOMSource domSource = new DOMSource(requestDoc);
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(domSource, streamResult);
    requestContent = stringWriter.toString();
    StringEntity entity = new StringEntity(requestContent);
    httpRequest.setEntity(entity);
    httpRequest.setHeader("Content-Type", "application/xml");

    // Send Request
    HttpResponse httpResponse = null;
    try {
        if (shouldTrace) {
            CloudTracing.sendRequest(invocationId, httpRequest);
        }
        httpResponse = this.getClient().getHttpClient().execute(httpRequest);
        if (shouldTrace) {
            CloudTracing.receiveResponse(invocationId, httpResponse);
        }
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            ServiceException ex = ServiceException.createFromXml(httpRequest, requestContent, httpResponse,
                    httpResponse.getEntity());
            if (shouldTrace) {
                CloudTracing.error(invocationId, ex);
            }
            throw ex;
        }

        // Create Result
        DacImportExportResponse result = null;
        // Deserialize Response
        if (statusCode == HttpStatus.SC_OK) {
            InputStream responseContent = httpResponse.getEntity().getContent();
            result = new DacImportExportResponse();
            DocumentBuilderFactory documentBuilderFactory2 = DocumentBuilderFactory.newInstance();
            documentBuilderFactory2.setNamespaceAware(true);
            DocumentBuilder documentBuilder2 = documentBuilderFactory2.newDocumentBuilder();
            Document responseDoc = documentBuilder2.parse(new BOMInputStream(responseContent));

            Element guidElement = XmlUtility.getElementByTagNameNS(responseDoc,
                    "http://schemas.microsoft.com/2003/10/Serialization/", "guid");
            if (guidElement != null) {
                result.setGuid(guidElement.getTextContent());
            }

        }
        result.setStatusCode(statusCode);
        if (httpResponse.getHeaders("x-ms-request-id").length > 0) {
            result.setRequestId(httpResponse.getFirstHeader("x-ms-request-id").getValue());
        }

        if (shouldTrace) {
            CloudTracing.exit(invocationId, result);
        }
        return result;
    } finally {
        if (httpResponse != null && httpResponse.getEntity() != null) {
            httpResponse.getEntity().getContent().close();
        }
    }
}

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

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