Example usage for org.w3c.dom Element getNodeName

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

Introduction

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

Prototype

public String getNodeName();

Source Link

Document

The name of this node, depending on its type; see the table above.

Usage

From source file:org.chiba.tools.schemabuilder.AbstractSchemaFormBuilder.java

/**
 * Add a repeat section if maxOccurs > 1.
 *///from w  w  w . j  a v  a  2  s. c o  m
private Element addRepeatIfNecessary(Document xForm, Element modelSection, Element formSection,
        XSTypeDefinition controlType, int minOccurs, int maxOccurs, String pathToRoot) {
    Element repeatSection = formSection;

    // add xforms:repeat section if this element re-occurs
    //
    if (maxOccurs != 1) {

        if (LOGGER.isDebugEnabled())
            LOGGER.debug("DEBUG: AddRepeatIfNecessary for multiple element for type " + controlType.getName()
                    + ", maxOccurs=" + maxOccurs);

        //repeatSection = (Element) formSection.appendChild(xForm.createElementNS(XFORMS_NS,getXFormsNSPrefix() + "repeat"));
        repeatSection = xForm.createElementNS(XFORMS_NS, getXFormsNSPrefix() + "repeat");

        //bind instead of repeat
        //repeatSection.setAttributeNS(XFORMS_NS,getXFormsNSPrefix() + "nodeset",pathToRoot);
        // bind -> last element in the modelSection
        Element bind = DOMUtil.getLastChildElement(modelSection);
        String bindId = null;

        if ((bind != null) && (bind.getLocalName() != null) && bind.getLocalName().equals("bind")) {
            bindId = bind.getAttributeNS(SchemaFormBuilder.XFORMS_NS, "id");
        } else {
            LOGGER.warn("addRepeatIfNecessary: bind not found: " + bind + " (model selection name="
                    + modelSection.getNodeName() + ")");

            //if no bind is found -> modelSection is already a bind, get its parent last child
            bind = DOMUtil.getLastChildElement(modelSection.getParentNode());

            if ((bind != null) && (bind.getLocalName() != null) && bind.getLocalName().equals("bind")) {
                bindId = bind.getAttributeNS(SchemaFormBuilder.XFORMS_NS, "id");
            } else {
                LOGGER.warn("addRepeatIfNecessary: bind really not found");
            }
        }

        repeatSection.setAttributeNS(XFORMS_NS, getXFormsNSPrefix() + "bind", bindId);
        this.setXFormsId(repeatSection);

        //appearance=full is more user friendly
        repeatSection.setAttributeNS(XFORMS_NS, getXFormsNSPrefix() + "appearance", "full");

        //triggers
        this.addTriggersForRepeat(xForm, formSection, repeatSection, minOccurs, maxOccurs, bindId);

        Element controlWrapper = _wrapper.createControlsWrapper(repeatSection);
        formSection.appendChild(controlWrapper);

        //add a group inside the repeat?
        Element group = xForm.createElementNS(XFORMS_NS, this.getXFormsNSPrefix() + "group");
        this.setXFormsId(group);
        repeatSection.appendChild(group);
        repeatSection = group;
    }

    return repeatSection;
}

From source file:org.chiba.xml.xforms.connector.serializer.FormDataSerializer.java

protected void serializeElement(PrintWriter writer, Element element, String boundary, String charset)
        throws Exception {
    /* The specs http://www.w3.org/TR/2003/REC-xforms-20031014/slice11.html#serialize-form-data
     *//w  w  w . j  a v a  2s. c o m
     *     Each element node is visited in document order.
     *
     *     Each element that has exactly one text node child is selected 
     *     for inclusion.
     *
     *     Element nodes selected for inclusion are as encoded as 
     *     Content-Disposition: form-data MIME parts as defined in 
     *     [RFC 2387], with the name parameter being the element local name.
     *
     *     Element nodes of any datatype populated by upload are serialized 
     *     as the specified content and additionally have a 
     *     Content-Disposition filename parameter, if available.
     *
     *     The Content-Type must be text/plain except for xsd:base64Binary, 
     *     xsd:hexBinary, and derived types, in which case the header 
     *     represents the media type of the attachment if known, otherwise 
     *     application/octet-stream. If a character set is applicable, the 
     *     Content-Type may have a charset parameter.
     *
     */
    String nodeValue = null;
    boolean isCDATASection = false;
    boolean includeTextNode = true;

    NodeList list = element.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node n = list.item(i);
        switch (n.getNodeType()) {

        /* CDATA sections are not mentioned ... ignore for now
        case Node.CDATA_SECTION_NODE:
            isCDATASection = true;
         */

        case Node.TEXT_NODE:
            if (includeTextNode == true) {
                if (nodeValue != null) {
                    /* only one text node allowed by specs */
                    includeTextNode = false;
                } else {
                    nodeValue = n.getNodeValue();
                }
            }
            break;

        /* Real ambiguity in specs, what if there's one text node and
         * n elements ? Let's assume if there is an element, ignore the
         * text nodes
         */
        case Node.ELEMENT_NODE:
            includeTextNode = false;
            serializeElement(writer, (Element) n, boundary, charset);
            break;

        default:
            // ignore comments and other nodes...
        }
    }

    if (nodeValue != null && includeTextNode) {

        Object object = ((ElementImpl) element).getUserData();
        if (!(object instanceof ModelItem)) {
            throw new XFormsException("Unknown instance data format.");
        }
        ModelItem item = (ModelItem) object;

        writer.print("\r\n--" + boundary);

        String name = element.getLocalName();
        if (name == null) {
            name = element.getNodeName();
        }

        // mediatype tells about file upload
        if (item.getMediatype() != null) {
            writer.print("\r\nContent-Disposition: form-data; name=\"" + name + "\";");
            if (item.getFilename() != null) {
                File file = new File(item.getFilename());
                writer.print(" filename=\"" + file.getName() + "\";");
            }
            writer.print("\r\nContent-Type: " + item.getMediatype());

        } else {
            writer.print("\r\nContent-Disposition: form-data; name=\"" + name + "\";");
            writer.print("\r\nContent-Type: text/plain; charset=\"" + charset + "\";");
        }

        String encoding = "8bit";
        if ("base64Binary".equalsIgnoreCase(item.getDatatype())) {
            encoding = "base64";
        } else if ("hexBinary".equalsIgnoreCase(item.getDatatype())) {
            // recode to base64 because of MIME
            nodeValue = new String(Base64.encodeBase64(Hex.decodeHex(nodeValue.toCharArray()), true));
            encoding = "base64";
        }
        writer.print("\r\nContent-Transfer-Encoding: " + encoding);
        writer.print("\r\n\r\n" + nodeValue);
    }

    writer.flush();
}

From source file:org.chiba.xml.xforms.Model.java

private void loadInlineSchemas(List list) throws XFormsException {
    String schemaId = null;//from w  w w  .  ja v a 2 s  . c o  m
    try {
        NodeList children = this.element.getChildNodes();

        for (int index = 0; index < children.getLength(); index++) {
            Node child = children.item(index);

            if (Node.ELEMENT_NODE == child.getNodeType()
                    && NamespaceCtx.XMLSCHEMA_NS.equals(child.getNamespaceURI())) {
                Element element = (Element) child;
                schemaId = element.hasAttributeNS(null, "id") ? element.getAttributeNS(null, "id")
                        : element.getNodeName();

                XSModel schema = loadSchema(element);

                if (schema == null) {
                    throw new NullPointerException("resource not found");
                }
                list.add(schema);
            }
        }
    } catch (Exception e) {
        throw new XFormsLinkException("could not load inline schema", this.target, schemaId);
    }
}

From source file:org.commonjava.maven.galley.maven.parse.XMLInfrastructure.java

private String getChildText(final String name, final Element parent) {
    final NodeList nl = parent.getElementsByTagName(name);
    if (nl == null || nl.getLength() < 1) {
        logger.debug("No element: {} in: {}", name, parent.getNodeName());
        return null;
    }//from  w  w w.  j  a  v  a 2s. com

    Element elem = null;
    for (int i = 0; i < nl.getLength(); i++) {
        final Element e = (Element) nl.item(i);
        if (e.getParentNode() == parent) {
            elem = e;
            break;
        }
    }

    return elem == null ? null : elem.getTextContent().trim();
}

From source file:org.dbpedia.spotlight.string.XmlParser.java

public static Element parse(String xmlText) throws IOException, SAXException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = dbf.newDocumentBuilder();
    //LOG.debug("parsing"+xmlText);
    InputStream in = new ByteArrayInputStream(xmlText.getBytes("UTF-8"));
    Document doc = docBuilder.parse(in);

    Element rootTag = doc.getDocumentElement();
    LOG.debug(rootTag.getNodeName());
    return rootTag;
}

From source file:org.deegree.portal.standard.wfs.control.DigitizeListener.java

/**
 * Handle the WFS transaction response./* w ww  .jav  a 2 s .  c o  m*/
 *
 * A positive response may be overwritten in method handlePositiveResponse(). A negative
 * response may be overwritten in method handleNegativeResponse().
 *
 * @param response
 * @return any objects that are returned from handlePositiveResponse() and that might be needed
 *         in derived listeners. Here, it returns null.
 * @throws WFSClientException
 *             if the transaction response does not contain a root element of
 *             "wfs:TransactionResponse"
 */
protected Object[] handleResponse(XMLFragment response) throws WFSClientException {

    Object[] obj = null;

    Element rootElem = response.getRootElement();
    String root = rootElem.getNodeName();

    if (LOG.getLevel() == ILogger.LOG_DEBUG) {
        String msg = StringTools.concat(100, "Response root name: " + root);
        LOG.logDebug(msg);
    }
    // root.contains("TransactionResponse")
    if ("wfs:TransactionResponse".equals(root)) {
        obj = handlePositiveResponse(response);
    } else if ("ServiceExceptionReport".equals(root)) {
        handleNegativeResponse(response);
    } else {
        throw new WFSClientException(Messages.getMessage("IGEO_STD_WFS_UNKNOWN_TRANSACTION_RESPONSE", root));
    }

    return obj;
}

From source file:org.directwebremoting.spring.DwrNamespaceHandler.java

/**
 * @param converterConfig/*ww  w . j av  a2 s .c  o  m*/
 * @param parent
 */
protected void parseConverterSettings(ConverterConfig converterConfig, Element parent) {
    NodeList children = parent.getChildNodes();

    // check to see if there are any nested elements here
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);

        if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.COMMENT_NODE) {
            continue;
        }

        Element child = (Element) node;
        if ("dwr:include".equals(child.getNodeName())) {
            converterConfig.addInclude(child.getAttribute("method"));
        } else if ("dwr:exclude".equals(child.getNodeName())) {
            converterConfig.addExclude(child.getAttribute("method"));
        }
        /* TODO Why is this only a property of ObjectConverter?
         else if (child.getNodeName().equals("dwr:force"))
         {
         converterConfig.setForce(Boolean.parseBoolean(child.getAttribute("value")));
         }
         */
        else {
            throw new RuntimeException("an unknown dwr:remote sub node was found: " + node.getNodeName());
        }
    }

}

From source file:org.dita.dost.writer.ConrefPushParser.java

/**
 * //from  ww w  . ja  va 2  s .c o m
 * @param type pushtype
 * @param elem element
 * @return string
 */
private void replaceSubElementName(final String type, final Element elem) {
    final DitaClass classValue = DitaClass.getInstance(elem);
    if (elem.getAttributeNode(ATTRIBUTE_NAME_CONREF) != null) {
        hasConref = true;
    }
    if (elem.getAttributeNode(ATTRIBUTE_NAME_KEYREF) != null) {
        hasKeyref = true;
    }
    String generalizedElemName = elem.getNodeName();
    if (classValue != null) {
        if (classValue.toString().contains(type) && !type.equals(STRING_BLANK)) {
            generalizedElemName = classValue.toString()
                    .substring(classValue.toString().indexOf("/") + 1,
                            classValue.toString().indexOf(STRING_BLANK, classValue.toString().indexOf("/")))
                    .trim();
        }
    }
    elem.getOwnerDocument().renameNode(elem, elem.getNamespaceURI(), generalizedElemName);
    final NodeList nodeList = elem.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        final Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            replaceSubElementName(type, (Element) node);
        }
    }
}

From source file:org.dita.dost.writer.ConrefPushParser.java

private void writeNode(final Node node) throws SAXException {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_FRAGMENT_NODE: {
        final NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            writeNode(children.item(i));
        }/*from www  .jav a2 s . c o m*/
        break;
    }
    case Node.ELEMENT_NODE:
        final Element e = (Element) node;
        final AttributesBuilder b = new AttributesBuilder();
        final NamedNodeMap atts = e.getAttributes();
        for (int i = 0; i < atts.getLength(); i++) {
            b.add((Attr) atts.item(i));
        }
        final String ns = e.getNamespaceURI() != null ? e.getNamespaceURI() : NULL_NS_URI;
        getContentHandler().startElement(ns, e.getTagName(), e.getNodeName(), b.build());
        final NodeList children = e.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            writeNode(children.item(i));
        }
        getContentHandler().endElement(ns, e.getTagName(), e.getNodeName());
        break;
    case Node.TEXT_NODE:
        final char[] data = node.getNodeValue().toCharArray();
        getContentHandler().characters(data, 0, data.length);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        getContentHandler().processingInstruction(node.getNodeName(), node.getNodeValue());
        break;
    default:
        throw new UnsupportedOperationException();
    }
}

From source file:org.dita.dost.writer.TestConrefPushParser.java

private String nodeToString(final Element elem) {
    final StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(Constants.LESS_THAN).append(elem.getNodeName());
    final NamedNodeMap namedNodeMap = elem.getAttributes();
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        stringBuffer.append(Constants.STRING_BLANK).append(namedNodeMap.item(i).getNodeName())
                .append(Constants.EQUAL)
                .append(Constants.QUOTATION + namedNodeMap.item(i).getNodeValue() + Constants.QUOTATION);
    }/*  ww  w  . j  ava 2 s  .c om*/
    stringBuffer.append(Constants.GREATER_THAN);
    final NodeList nodeList = elem.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        final Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            // If the type of current node is ELEMENT_NODE, process it
            stringBuffer.append(nodeToString((Element) node));
        }
        if (node.getNodeType() == Node.TEXT_NODE) {
            stringBuffer.append(node.getNodeValue());
        }
    }
    stringBuffer.append("</").append(elem.getNodeName()).append(Constants.GREATER_THAN);
    return stringBuffer.toString();
}