Example usage for org.w3c.dom Element setAttributeNS

List of usage examples for org.w3c.dom Element setAttributeNS

Introduction

In this page you can find the example usage for org.w3c.dom Element setAttributeNS.

Prototype

public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException;

Source Link

Document

Adds a new attribute.

Usage

From source file:de.betterform.xml.xforms.ui.Repeat.java

private void initializePrototype(Node parent, Node prototype) {
    Node copy = prototype.cloneNode(false);
    if (copy.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) copy;
        if (element.getAttributeNS(null, "id").length() == 0 && XFormsElementFactory.isUIElement(element)) {
            element.setAttributeNS(null, "id", this.container.generateId());
        }/*from   w w  w. j ava2 s. c o  m*/

        NodeList children = prototype.getChildNodes();
        for (int index = 0; index < children.getLength(); index++) {
            initializePrototype(element, children.item(index));
        }
    }

    parent.appendChild(copy);
}

From source file:fr.inria.atlanmod.collaboro.ui.views.NotationView.java

/**
 * Creates a splash image with the message "No item selected"
 * //  w ww .j a va 2s.c  o  m
 * @return
 */
protected SVGDocument createTestImage() {
    DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
    String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
    SVGDocument doc = (SVGDocument) impl.createDocument(svgNS, "svg", null);

    Element root = doc.getDocumentElement();
    root.setAttributeNS(null, "width", "500px");
    root.setAttributeNS(null, "height", "500px");

    Element text = doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "text");
    text.setAttributeNS(null, "x", "10");
    text.setAttributeNS(null, "y", "20");
    text.setAttributeNS(null, "font-size", DEFAULT_FONT_SIZE);
    text.setAttributeNS(null, "font-family", DEFAULT_FONT_FAMILY);
    text.setAttributeNS(null, "font-weight", "bold");
    text.setAttributeNS(null, "fill", "red");
    text.setAttributeNS(null, "stroke", "none");
    text.setTextContent("No item selected");
    root.appendChild(text);

    return doc;
}

From source file:fr.inria.atlanmod.collaboro.ui.views.NotationView.java

/**
 * Creates a splash image with the message "No syntax defined"
 * /*from   ww  w.  j  ava 2  s  .c om*/
 * @return
 */
protected SVGDocument createNoSyntaxImage() {
    DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
    String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
    SVGDocument doc = (SVGDocument) impl.createDocument(svgNS, "svg", null);

    Element root = doc.getDocumentElement();
    root.setAttributeNS(null, "width", "500px");
    root.setAttributeNS(null, "height", "500px");

    Element text = doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "text");
    text.setAttributeNS(null, "x", "10");
    text.setAttributeNS(null, "y", "20");
    text.setAttributeNS(null, "font-size", DEFAULT_FONT_SIZE);
    text.setAttributeNS(null, "font-family", DEFAULT_FONT_FAMILY);
    text.setAttributeNS(null, "font-weight", "bold");
    text.setAttributeNS(null, "fill", "red");
    text.setAttributeNS(null, "stroke", "none");
    text.setTextContent("No syntax defined");
    root.appendChild(text);

    return doc;
}

From source file:com.centeractive.ws.builder.soap.XmlUtils.java

public static void setXsiType(Element elm, QName name) {
    String prefix = findPrefixForNamespace(elm, name.getNamespaceURI());
    if (prefix == null) {
        prefix = generatePrefixForNamespace(name.getNamespaceURI());
        while (findNamespaceForPrefix(elm, prefix) != null) {
            prefix = generatePrefixForNamespace(name.getNamespaceURI());
        }//from   ww w. java 2 s. c  o m

        elm.setAttribute("xmlns:" + prefix, name.getNamespaceURI());
    }

    elm.setAttributeNS(Constants.XSI_NS, "type", prefix + ":" + name.getLocalPart());
}

From source file:gov.nij.bundles.intermediaries.ers.EntityResolutionMessageHandler.java

/**
 * This method takes the ER response and converts the Java objects to the Merge Response XML.
 * /*ww  w. ja  va  2s  . c  om*/
 * @param entityContainerNode
 * @param results
 * @param recordLimit
 * @param attributeParametersNode
 * @return
 * @throws ParserConfigurationException
 * @throws XPathExpressionException
 * @throws TransformerException
 */
private Document createResponseMessage(Node entityContainerNode, EntityResolutionResults results,
        Node attributeParametersNode, int recordLimit) throws Exception {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);

    Document resultDocument = dbf.newDocumentBuilder().newDocument();

    Element entityMergeResultMessageElement = resultDocument.createElementNS(
            EntityResolutionNamespaceContext.MERGE_RESULT_NAMESPACE, "EntityMergeResultMessage");
    resultDocument.appendChild(entityMergeResultMessageElement);

    Element entityContainerElement = resultDocument
            .createElementNS(EntityResolutionNamespaceContext.MERGE_RESULT_NAMESPACE, "EntityContainer");
    entityMergeResultMessageElement.appendChild(entityContainerElement);

    NodeList inputEntityNodes = (NodeList) xpath.evaluate("er-ext:Entity", entityContainerNode,
            XPathConstants.NODESET);
    Collection<Element> inputEntityElements = null;
    if (attributeParametersNode == null) {
        inputEntityElements = new ArrayList<Element>();
    } else {
        inputEntityElements = TreeMultiset
                .create(new EntityElementComparator((Element) attributeParametersNode));
        //inputEntityElements = new ArrayList<Element>();
    }

    for (int i = 0; i < inputEntityNodes.getLength(); i++) {
        inputEntityElements.add((Element) inputEntityNodes.item(i));
    }

    if (attributeParametersNode == null) {
        LOG.warn("Attribute Parameters element was null, so records will not be sorted");
    }
    //Collections.sort((List<Element>) inputEntityElements, new EntityElementComparator((Element) attributeParametersNode));

    if (inputEntityElements.size() != inputEntityNodes.getLength()) {
        LOG.error("Lost elements in ER output sorting.  Input count=" + inputEntityNodes.getLength()
                + ", output count=" + inputEntityElements.size());
    }

    for (Element e : inputEntityElements) {
        Node clone = resultDocument.adoptNode(e.cloneNode(true));
        resultDocument.renameNode(clone, EntityResolutionNamespaceContext.MERGE_RESULT_EXT_NAMESPACE,
                e.getLocalName());
        entityContainerElement.appendChild(clone);
    }

    Element mergedRecordsElement = resultDocument
            .createElementNS(EntityResolutionNamespaceContext.MERGE_RESULT_NAMESPACE, "MergedRecords");
    entityMergeResultMessageElement.appendChild(mergedRecordsElement);

    if (results != null) {

        List<RecordWrapper> records = results.getRecords();

        // Loop through RecordWrappers to extract info to create merged records
        for (RecordWrapper record : records) {
            LOG.debug("  !#!#!#!# Record 1, id=" + record.getExternalId() + ", externals="
                    + record.getRelatedIds());

            // Create Merged Record Container
            Element mergedRecordElement = resultDocument.createElementNS(
                    EntityResolutionNamespaceContext.MERGE_RESULT_EXT_NAMESPACE, "MergedRecord");
            mergedRecordsElement.appendChild(mergedRecordElement);

            // Create Original Record Reference for 'first record'
            Element originalRecordRefElement = resultDocument.createElementNS(
                    EntityResolutionNamespaceContext.MERGE_RESULT_EXT_NAMESPACE, "OriginalRecordReference");
            originalRecordRefElement.setAttributeNS(EntityResolutionNamespaceContext.STRUCTURES_NAMESPACE,
                    "ref", record.getExternalId());
            mergedRecordElement.appendChild(originalRecordRefElement);

            // Loop through and add any related records
            for (String relatedRecordId : record.getRelatedIds()) {
                originalRecordRefElement = resultDocument.createElementNS(
                        EntityResolutionNamespaceContext.MERGE_RESULT_EXT_NAMESPACE, "OriginalRecordReference");
                originalRecordRefElement.setAttributeNS(EntityResolutionNamespaceContext.STRUCTURES_NAMESPACE,
                        "ref", relatedRecordId);
                mergedRecordElement.appendChild(originalRecordRefElement);
            }

            // Create Merge Quality Element
            Element mergeQualityElement = resultDocument.createElementNS(
                    EntityResolutionNamespaceContext.MERGE_RESULT_EXT_NAMESPACE, "MergeQuality");
            mergedRecordElement.appendChild(mergeQualityElement);
            Set<AttributeStatistics> stats = results.getStatisticsForRecord(record.getExternalId());
            for (AttributeStatistics stat : stats) {
                Element stringDistanceStatsElement = resultDocument.createElementNS(
                        EntityResolutionNamespaceContext.MERGE_RESULT_EXT_NAMESPACE,
                        "StringDistanceStatistics");
                mergeQualityElement.appendChild(stringDistanceStatsElement);
                Element xpathElement = resultDocument.createElementNS(
                        EntityResolutionNamespaceContext.MERGE_RESULT_EXT_NAMESPACE, "AttributeXPath");
                stringDistanceStatsElement.appendChild(xpathElement);
                Node contentNode = resultDocument.createTextNode(stat.getAttributeName());
                xpathElement.appendChild(contentNode);
                Element meanElement = resultDocument.createElementNS(
                        EntityResolutionNamespaceContext.MERGE_RESULT_EXT_NAMESPACE,
                        "StringDistanceMeanInRecord");
                stringDistanceStatsElement.appendChild(meanElement);
                contentNode = resultDocument.createTextNode(String.valueOf(stat.getAverageStringDistance()));
                meanElement.appendChild(contentNode);
                Element sdElement = resultDocument.createElementNS(
                        EntityResolutionNamespaceContext.MERGE_RESULT_EXT_NAMESPACE,
                        "StringDistanceStandardDeviationInRecord");
                stringDistanceStatsElement.appendChild(sdElement);
                contentNode = resultDocument
                        .createTextNode(String.valueOf(stat.getStandardDeviationStringDistance()));
                sdElement.appendChild(contentNode);

            }
        }

    } else {

        for (Element e : inputEntityElements) {

            String id = e.getAttributeNS(EntityResolutionNamespaceContext.STRUCTURES_NAMESPACE, "id");

            Element mergedRecordElement = resultDocument.createElementNS(
                    EntityResolutionNamespaceContext.MERGE_RESULT_EXT_NAMESPACE, "MergedRecord");
            mergedRecordsElement.appendChild(mergedRecordElement);

            Element originalRecordRefElement = resultDocument.createElementNS(
                    EntityResolutionNamespaceContext.MERGE_RESULT_EXT_NAMESPACE, "OriginalRecordReference");
            originalRecordRefElement.setAttributeNS(EntityResolutionNamespaceContext.STRUCTURES_NAMESPACE,
                    "ref", id);
            mergedRecordElement.appendChild(originalRecordRefElement);

            Element mergeQualityElement = resultDocument.createElementNS(
                    EntityResolutionNamespaceContext.MERGE_RESULT_EXT_NAMESPACE, "MergeQuality");
            mergedRecordElement.appendChild(mergeQualityElement);
            XPath xp = XPathFactory.newInstance().newXPath();
            xp.setNamespaceContext(new EntityResolutionNamespaceContext());
            NodeList attributeParameterNodes = (NodeList) xp.evaluate("er-ext:AttributeParameter",
                    attributeParametersNode, XPathConstants.NODESET);
            for (int i = 0; i < attributeParameterNodes.getLength(); i++) {
                String attributeName = xp.evaluate("er-ext:AttributeXPath", attributeParametersNode);
                Element stringDistanceStatsElement = resultDocument.createElementNS(
                        EntityResolutionNamespaceContext.MERGE_RESULT_EXT_NAMESPACE,
                        "StringDistanceStatistics");
                mergeQualityElement.appendChild(stringDistanceStatsElement);
                Element xpathElement = resultDocument.createElementNS(
                        EntityResolutionNamespaceContext.MERGE_RESULT_EXT_NAMESPACE, "AttributeXPath");
                stringDistanceStatsElement.appendChild(xpathElement);
                Node contentNode = resultDocument.createTextNode(attributeName);
                xpathElement.appendChild(contentNode);
                Element meanElement = resultDocument.createElementNS(
                        EntityResolutionNamespaceContext.MERGE_RESULT_EXT_NAMESPACE,
                        "StringDistanceMeanInRecord");
                stringDistanceStatsElement.appendChild(meanElement);
                contentNode = resultDocument.createTextNode("0.0");
                meanElement.appendChild(contentNode);
                Element sdElement = resultDocument.createElementNS(
                        EntityResolutionNamespaceContext.MERGE_RESULT_EXT_NAMESPACE,
                        "StringDistanceStandardDeviationInRecord");
                stringDistanceStatsElement.appendChild(sdElement);
                contentNode = resultDocument.createTextNode("0.0");
                sdElement.appendChild(contentNode);
            }

        }

    }

    Element recordLimitExceededElement = resultDocument.createElementNS(
            EntityResolutionNamespaceContext.MERGE_RESULT_NAMESPACE, "RecordLimitExceededIndicator");
    recordLimitExceededElement.setTextContent(new Boolean(results == null).toString());
    entityMergeResultMessageElement.appendChild(recordLimitExceededElement);

    return resultDocument;

}

From source file:fr.inria.atlanmod.collaboro.ui.views.NotationView.java

/**
 * Builds the SVG for the corresponding notationElement. The representation is for
 * the abstract syntax element./*  w  ww  .j  av  a 2 s  . c om*/
 * 
 * @param notationElement
 * @param doc
 * @param x
 * @param y
 */
private Box buildSVG(NotationElement notationElement, SVGDocument doc, int x, int y) {
    Element svgRoot = doc.getDocumentElement();

    Box result = new Box(0, 0, x, y);
    if (notationElement instanceof Composite) {
        Composite composite = (Composite) notationElement;
        result.setX(x);
        int oldX = x;
        for (NotationElement subElement : composite.getSubElements()) {
            if (subElement instanceof Composite) {
                x = oldX + TAB;
                y = y + VERTICAL_SEP;
            }

            Box subBox = buildSVG(subElement, doc, x, y);

            if ((subElement instanceof Composite) || (subElement instanceof SyntaxOf)) {
                x = oldX;
                y = y + subBox.getHeight() - VERTICAL_SEP;
                if (result.getWidth() < subBox.getWidth())
                    result.setWidth(subBox.getWidth());
                result.setHeight(result.getHeight() + subBox.getHeight());
            } else {
                x = x + subBox.getWidth();
                result.setWidth(result.getWidth() + subBox.getWidth());
                if (result.getHeight() < subBox.getHeight())
                    result.setHeight(subBox.getHeight());
            }
        }
    } else if (notationElement instanceof TextualElement) {
        TextualElement textualElement = (TextualElement) notationElement;

        Element text = doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "text");
        text.setAttributeNS(null, "x", String.valueOf(x));
        text.setAttributeNS(null, "y", String.valueOf(y));
        text.setAttributeNS(null, "font-size", DEFAULT_FONT_SIZE);
        text.setAttributeNS(null, "font-family", DEFAULT_FONT_FAMILY);

        String value = "";
        if (textualElement instanceof Keyword) {
            Keyword keyword = (Keyword) notationElement;
            text.setAttributeNS(null, "font-weight", "bold");
            text.setAttributeNS(null, "fill", "green");
            text.setAttributeNS(null, "stroke", "none");
            value = keyword.getId();
        } else if (textualElement instanceof Token) {
            Token token = (Token) notationElement;
            text.setAttributeNS(null, "fill", "black");
            text.setAttributeNS(null, "stroke", "none");
            value = token.getId();
        } else if (textualElement instanceof AttributeValue) {
            AttributeValue attributeValue = (AttributeValue) notationElement;
            text.setAttributeNS(null, "fill", "orange");
            value = "<value of '" + attributeValue.getAttribute().getName() + "' attribute>";
        } else if (textualElement instanceof ReferenceValue) {
            ReferenceValue referenceValue = (ReferenceValue) notationElement;
            text.setAttributeNS(null, "fill", "orange");
            value = "<value of '" + referenceValue.getReference().getName() + "' reference>";
        }
        result.setWidth(value.length() * CHAR_SEP + CHAR_SEP);
        result.setHeight(VERTICAL_SEP);

        text.setTextContent(value);
        svgRoot.appendChild(text);
    } else if (notationElement instanceof SyntaxOf) {
        SyntaxOf syntaxOf = (SyntaxOf) notationElement;

        Element text = doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "text");
        text.setAttributeNS(null, "x", String.valueOf(x));
        text.setAttributeNS(null, "y", String.valueOf(y));
        text.setAttributeNS(null, "font-size", DEFAULT_FONT_SIZE);
        text.setAttributeNS(null, "font-family", DEFAULT_FONT_FAMILY);
        text.setAttributeNS(null, "fill", "blue");

        String value = "<syntax of '" + syntaxOf.getReference().getName() + "' reference>";
        text.setTextContent(value);

        result.setWidth(value.length() * CHAR_SEP + CHAR_SEP);
        result.setHeight(VERTICAL_SEP);
        svgRoot.appendChild(text);
    }

    return result;
}

From source file:fr.inria.atlanmod.collaboro.ui.views.NotationView.java

/**
 * Builds the SVG for the corresponding notationElement and particularizes the 
 * representation with the model element instance.
 * //from   w  w  w  .java  2s  .  co  m
 * @param eObject
 * @param notationElement
 * @param doc
 * @param x
 * @param y
 * @return
 */
public Box buildSVG(EObject eObject, NotationElement notationElement, SVGDocument doc, int x, int y) {
    Element svgRoot = doc.getDocumentElement();
    Box result = new Box(0, 0, x, y);

    if (notationElement instanceof Composite) {
        Composite composite = (Composite) notationElement;
        result.setX(x);
        int oldX = x;
        for (NotationElement subElement : composite.getSubElements()) {
            if (subElement instanceof Composite) {
                x = oldX + TAB;
                y = y + VERTICAL_SEP;
            }

            Box subBox = buildSVG(eObject, subElement, doc, x, y);

            if ((subElement instanceof Composite)) { // || (subElement instanceof SyntaxOf)) {
                x = oldX;
                y = y + subBox.getHeight() - VERTICAL_SEP;
                if (result.getWidth() < subBox.getWidth())
                    result.setWidth(subBox.getWidth());
                result.setHeight(result.getHeight() + subBox.getHeight());
            } else {
                x = x + subBox.getWidth();
                result.setWidth(result.getWidth() + subBox.getWidth());
                if (result.getHeight() < subBox.getHeight())
                    result.setHeight(subBox.getHeight());
            }
        }
    } else if (notationElement instanceof TextualElement) {
        TextualElement textualElement = (TextualElement) notationElement;

        Element text = doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "text");
        text.setAttributeNS(null, "x", String.valueOf(x));
        text.setAttributeNS(null, "y", String.valueOf(y));
        text.setAttributeNS(null, "font-size", DEFAULT_FONT_SIZE);
        text.setAttributeNS(null, "font-family", DEFAULT_FONT_FAMILY);

        String value = "";

        if (textualElement instanceof Keyword) {
            Keyword keyword = (Keyword) notationElement;
            text.setAttributeNS(null, "font-weight", "bold");
            text.setAttributeNS(null, "fill", keyword.getFill().getLiteral());
            text.setAttributeNS(null, "stroke", "none");
            value = keyword.getId();
        } else if (textualElement instanceof Token) {
            Token token = (Token) notationElement;
            text.setAttributeNS(null, "fill", token.getFill().getLiteral());
            text.setAttributeNS(null, "stroke", "none");
            value = token.getId();
        } else if (textualElement instanceof AttributeValue) {
            AttributeValue attributeValue = (AttributeValue) notationElement;
            text.setAttributeNS(null, "fill", attributeValue.getFill().getLiteral());

            EAttribute eAttribute = attributeValue.getAttribute();

            value = convert(eObject.eGet(eObject.eClass().getEStructuralFeature(eAttribute.getName())));
        } else if (textualElement instanceof ReferenceValue) {
            ReferenceValue referenceValue = (ReferenceValue) notationElement;
            text.setAttributeNS(null, "fill", referenceValue.getFill().getLiteral());

            EReference eReference = referenceValue.getReference();
            EAttribute eAttribute = referenceValue.getAttribute();
            String separator = referenceValue.getSeparator();

            Object referredObjs = eObject.eGet(eObject.eClass().getEStructuralFeature(eReference.getName()));
            if (referredObjs instanceof EList) {
                EList<EObject> eReferenceList = (EList<EObject>) referredObjs;
                for (EObject elementList : eReferenceList) {
                    Object attributeValue = elementList
                            .eGet(elementList.eClass().getEStructuralFeature(eAttribute.getName()));
                    value += convert(attributeValue);
                    if (eReferenceList.indexOf(elementList) != eReferenceList.size() - 1) {
                        value += separator;
                    }
                }
            } else if (referredObjs instanceof EObject) {
                EObject elementList = (EObject) referredObjs;
                Object attributeValue = elementList
                        .eGet(elementList.eClass().getEStructuralFeature(eAttribute.getName()));
                value += convert(attributeValue);
            }
        }

        if (value.length() > 0) {
            result.setWidth(value.length() * CHAR_SEP + CHAR_SEP);
            result.setHeight(VERTICAL_SEP);
            text.setTextContent(value);
            svgRoot.appendChild(text);
        }
    } else if (notationElement instanceof SyntaxOf) {
        SyntaxOf syntaxOf = (SyntaxOf) notationElement;

        EReference eReference = syntaxOf.getReference();

        Object referredObjs = eObject.eGet(eObject.eClass().getEStructuralFeature(eReference.getName()));
        if (referredObjs instanceof EList) {
            EList<EObject> eReferenceList = (EList<EObject>) referredObjs;
            if (eReferenceList.size() > 0) {
                for (EObject elementList : eReferenceList) {
                    NotationElement subNotationElement = Controller.INSTANCE.getNotation(elementList.eClass());
                    if (subNotationElement != null) {
                        Box subBox = buildSVG(elementList, subNotationElement, doc, x, y);
                        y = y + subBox.getHeight();

                        if (result.getWidth() < subBox.getWidth())
                            result.setWidth(subBox.getWidth());
                        result.setHeight(result.getHeight() + subBox.getHeight());
                    }
                }
            }
        }
    }
    return result;
}

From source file:gov.nij.bundles.intermediaries.ers.EntityResolutionMessageHandler.java

/**
 * This method takes the entity and attribute parameter nodes as arguments and converts the XML to Java Objects so Entity Resolution can be performed.
 * /*from   w w  w .ja v  a  2  s.c  o  m*/
 * @param entityContainerNode
 * @param attributeParametersNode
 * @return
 * @throws Exception
 */

List<RecordWrapper> createRecordsFromRequestMessage(NodeList entityNodeList, Node attributeParametersNode)
        throws Exception {

    List<RecordWrapper> records = new ArrayList<RecordWrapper>();

    Set<AttributeParametersXpathSupport> attributeParametersXpathSupport = getAttributeParameters(
            attributeParametersNode);

    for (int i = 0; i < entityNodeList.getLength(); i++) {
        Element entityElement = (Element) entityNodeList.item(i);

        // The following lines will first check for an ID, if none is found, one is generated
        String entityId = entityElement.getAttributeNS(EntityResolutionNamespaceContext.STRUCTURES_NAMESPACE,
                "id");

        if (StringUtils.isEmpty(entityId)) {
            entityId = "E" + UUID.randomUUID().toString();
            entityElement.setAttributeNS(EntityResolutionNamespaceContext.STRUCTURES_NAMESPACE, "s:id",
                    entityId);
        }

        entityElement = createOrphanElement(entityElement);

        Map<String, AttributeWrapper> attributeMap = new HashMap<String, AttributeWrapper>();

        for (AttributeParametersXpathSupport parameter : attributeParametersXpathSupport) {
            String attributeName = parameter.getAttributeName();
            AttributeWrapper attribute = new AttributeWrapper(attributeName);

            XPath attributeParameterXpath = parameter.getXpath();
            String value = attributeParameterXpath.evaluate(attributeName, entityElement);

            attribute.addValue(value);
            LOG.debug("Adding attribute to record with entityId=" + entityId + ", type=" + attributeName
                    + ", value=" + value);
            attributeMap.put(attribute.getType(), attribute);
        }

        RecordWrapper record = new RecordWrapper(attributeMap, entityId);
        records.add(record);
    }

    return records;

}

From source file:de.betterform.xml.dom.DOMUtil.java

/**
 * copies all attributes from one Element to another
 *
 * @param from   - the Element which the source attributes
 * @param to     - the target Element for the Attributes
 * @param filter - a NodeFilter to apply during copy
 *//*from   w  w w.j av a2 s .  co  m*/
public static void copyAttributes(Element from, Element to, NodeFilter filter) {
    if ((from != null) && (to != null)) {
        NamedNodeMap map = from.getAttributes();

        /* if filter is null use our own default filter, which accepts
           everything (this saves us from always check if filter is
           null */
        if (filter == null) {
            filter = new NodeFilter() {
                public short acceptNode(Node n) {
                    return NodeFilter.FILTER_ACCEPT;
                }
            };
        }

        if (map != null) {
            int len = map.getLength();

            for (int i = 0; i < len; i++) {
                Node attr = map.item(i);

                if (attr.getNodeType() == Node.ATTRIBUTE_NODE) {
                    if (filter.acceptNode(attr) == NodeFilter.FILTER_ACCEPT) {
                        to.setAttributeNS(attr.getNamespaceURI(), attr.getNodeName(), attr.getNodeValue());
                    }
                }
            }
        }
    }
}

From source file:dk.statsbiblioteket.doms.central.connectors.fedora.FedoraRest.java

@Override
public void addRelations(String pid, String subject, String predicate, List<String> objects, boolean literal,
        String comment)/*w ww.  ja  va  2s .  c  o  m*/
        throws BackendMethodFailedException, BackendInvalidCredsException, BackendInvalidResourceException {
    if (comment == null || comment.isEmpty()) {
        comment = "No message supplied";
    }
    if (literal) {//cant handle literal yet
        for (String object : objects) {
            addRelation(pid, subject, predicate, object, literal, comment);
        }
    }
    if (objects.size() == 1) {//more efficient if only adding one relation
        addRelation(pid, subject, predicate, objects.get(0), literal, comment);
    }

    XPathSelector xpath = DOM.createXPathSelector("rdf", Constants.NAMESPACE_RDF);

    String datastream;
    if (subject == null || subject.isEmpty() || subject.equals(pid) || subject.equals("info:fedora/" + pid)) {
        subject = "info:fedora/" + pid;
        datastream = "RELS-EXT";
    } else {
        datastream = "RELS-INT";
    }

    String rels = getXMLDatastreamContents(pid, datastream, null);
    Document relsDoc = DOM.stringToDOM(rels, true);

    Node rdfDescriptionNode = xpath.selectNode(relsDoc,
            "/rdf:RDF/rdf:Description[@rdf:about='" + subject + "']");

    predicate = getAbsoluteURIAsString(predicate);

    String[] splits = predicate.split("#");

    for (String object : objects) {
        if (!object.startsWith("info:fedora/")) {
            object = "info:fedora/" + object;
        }

        Element relationsShipElement = relsDoc.createElementNS(splits[0] + "#", splits[1]);
        relationsShipElement.setAttributeNS(Constants.NAMESPACE_RDF, "rdf:resource", object);
        rdfDescriptionNode.appendChild(relationsShipElement);
    }

    byte[] bytes;
    try {
        bytes = DOM.domToString(relsDoc).getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        //TODO Not really a backend exception
        throw new BackendMethodFailedException("Failed to transform RELS-EXT", e);
    } catch (TransformerException e) {
        //TODO Not really a backend exception
        throw new BackendMethodFailedException("Failed to transform RELS-EXT", e);
    }
    modifyDatastreamByValue(pid, datastream, ChecksumType.MD5, null, bytes, null, "application/rdf+xml",
            comment, null);
}