Example usage for org.xml.sax Attributes getQName

List of usage examples for org.xml.sax Attributes getQName

Introduction

In this page you can find the example usage for org.xml.sax Attributes getQName.

Prototype

public abstract String getQName(int index);

Source Link

Document

Look up an attribute's XML qualified (prefixed) name by index.

Usage

From source file:org.jbuiltDemo.managed.view.Xhtml2Jbuilt.java

@Override
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
    ElementNode node = new ElementNode();
    node.name = localName;//  ww  w . j a  v a  2  s .co  m
    node.mapping = uri;
    for (int i = 0; i < attributes.getLength(); i++) {
        String aln = attributes.getQName(i);
        String ava = attributes.getValue(i);
        node.attributes.put(aln, ava);
    }
    info.startNode(node, false, null, null);
    super.startElement(uri, localName, name, attributes);
}

From source file:org.jcurl.core.helpers.XmlSerializerBase.java

/**
 * Write an element start tag and it's attributes. Correctly handles
 * changing default namespaces./* w  w  w  .j a  v a 2 s.c o  m*/
 * 
 * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
 *      java.lang.String, java.lang.String, org.xml.sax.Attributes)
 */
public void startElement(final String namespaceURI, final String localName, final String qName,
        final Attributes atts) throws SAXException {
    if (indent) {
        this.writePlain(NEWLINE);
        for (int i = currentElement.size(); i > 0; i--)
            this.writePlain(TABULATOR);
    }
    this.writePlain("<");
    this.writePlain(qName);
    // handle namespace changes
    if (pendingPrefixStart != null || pendingUriStart != null) {
        this.writePlain(" xmlns");
        if (pendingPrefixStart != null) {
            this.writePlain(':');
            this.writePlain(pendingPrefixStart);
        }
        this.writePlain("=\"");
        if (pendingUriStart != null)
            this.writeEncoded(pendingUriStart);
        this.writePlain("\"");
        pendingPrefixStart = pendingUriStart = null;
    }
    // TODO keep track of changing namespace prefixes
    // TODO handle attribute namespaces right

    if (atts != null) {
        final int len = atts.getLength();
        for (int i = 0; i < len; i++) {
            this.writePlain(" ");
            this.writePlain(atts.getQName(i));
            this.writePlain("=\"");
            this.writeEncoded(atts.getValue(i));
            this.writePlain("\"");
        }
    }
    this.writePlain(">");
    currentElement.push(qName);
}

From source file:org.jcurl.core.helpers.XmlSimpleWriter.java

/**
 * Write an element start tag and it's attributes. Correctly handles
 * changing default namespaces.// www  .  j  ava  2 s  . c o  m
 * 
 * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
 *      java.lang.String, java.lang.String, org.xml.sax.Attributes)
 */
@Override
public void startElement(final String namespaceURI, final String localName, String qName, final Attributes atts)
        throws SAXException {
    if (qName == null && localName != null)
        qName = localName;
    checkQName(qName);
    writePlain('<', target);
    writePlain(qName, target);
    // handle namespace changes
    if (pendingPrefixStart != null || pendingUriStart != null) {
        writePlain(" xmlns", target);
        if (pendingPrefixStart != null) {
            writePlain(':', target);
            writePlain(pendingPrefixStart, target);
        }
        writePlain("=\"", target);
        if (pendingUriStart != null)
            writeEncoded(pendingUriStart, target);
        writePlain("\"", target);
        pendingPrefixStart = pendingUriStart = null;
    }
    // TODO keep track of changing namespace prefixes
    // TODO handle attribute namespaces right

    if (atts != null) {
        final int len = atts.getLength();
        for (int i = 0; i < len; i++) {
            final String val = atts.getValue(i);
            String name = atts.getQName(i);
            if (name == null && atts.getLocalName(i) != null)
                name = atts.getLocalName(i);
            checkAttName(name);
            if (val == null)
                log.warn("Attribute [" + getCurrentXPath(elemStack) + "/" + qName + "/@" + name
                        + "] is skipped because it has the value null");
            else {
                writePlain(' ', target);
                writePlain(name, target);
                writePlain("=\"", target);
                writeEncoded(val, target);
                writePlain("\"", target);
            }
        }
    }
    writePlain(">", target);
    elemStack.push(qName);
}

From source file:org.junitext.runners.parameters.factory.CreateValueObjectRule.java

/**
 * @see org.apache.commons.digester.Rule#begin(java.lang.String,
 *      java.lang.String, org.xml.sax.Attributes)
 *//*from w  w w.j a va  2 s .  co m*/
@Override
public void begin(String namespace, String elementName, Attributes attributes) throws Exception {

    // Look to see if the type of the value object has been defined
    String valueType = null;
    for (int j = 0; j < attributes.getLength(); j++) {
        String attrName = attributes.getLocalName(j);
        if (attrName.equals("")) {
            attrName = attributes.getQName(j);
        }
        if (attrName.equals(typeAttributeName)) {
            // We found the attribute that defines the
            // property to set
            valueType = attributes.getValue(j);
        }
    }

    if (valueType == null) {
        // The value type was not specified, so default to String
        valueType = String.class.getName();
    }

    // Push the value type onto the value type stack
    digester.push(VALUE_OBJECT_TYPE_STACK, valueType);

    //Also initialize the "isNull" flag to false
    digester.push(VALUE_OBJECT_IS_NULL, false);
}

From source file:org.junitext.runners.parameters.factory.SetPropertyWithParameterRule.java

/**
 * @see org.apache.commons.digester.Rule#begin(java.lang.String,
 *      java.lang.String, org.xml.sax.Attributes)
 *///  w  ww  . j av a  2 s . co m
@Override
public void begin(String namespace, String elementName, Attributes attributes) throws Exception {
    // get the name of the property to set on the bean
    String propertyName = "";
    for (int j = 0; j < attributes.getLength(); j++) {
        String attrName = attributes.getLocalName(j);
        if (attrName.equals("")) {
            attrName = attributes.getQName(j);
        }
        if (attrName.equals(propertyAttribute)) {
            // We found the attribute that defines the
            // property to set
            propertyName = attributes.getValue(j);
        }
    }

    // Push the name of the property we are going to set onto a named stack.
    // We use a stack because an instance variable would be reset on nested
    // elements
    digester.push(PROPERTY_NAME_STACK, propertyName);

    // Push a single-element array onto the parameter stack to capture
    // parameters
    Object[] parameters = new Object[1];
    digester.pushParams(parameters);
}

From source file:org.lobid.lodmill.XmlEntitySplitter.java

private void appendValuesToEntity(final String qName, final Attributes attributes) {
    this.builder.append("<" + qName);
    if (attributes.getLength() > 0) {
        for (int i = 0; i < attributes.getLength(); i++) {
            builder.append(" " + attributes.getQName(i) + "=\""
                    + StringEscapeUtils.escapeXml(attributes.getValue(i)) + "\"");
        }//from w  ww . j a va2  s .  c  o m
    }
    builder.append(">");
}

From source file:org.orbeon.oxf.xml.InspectingXMLReceiver.java

public void startElement(String uri, String localname, String qname, Attributes attributes)
        throws SAXException {
    namespaceContext.startElement();//  www. ja va  2s  .  c  o m
    final String error = checkInDocument();
    if (error != null)
        throw new ValidationException(error + ": element " + qname, new LocationData(locator));

    elementStack.push(new NameInfo(uri, localname, qname, new AttributesImpl(attributes)));

    // Check names
    checkElementName(uri, localname, qname);
    for (int i = 0; i < attributes.getLength(); i++)
        checkAttributeName(attributes.getURI(i), attributes.getLocalName(i), attributes.getQName(i));

    super.startElement(uri, localname, qname, attributes);
}

From source file:org.oscarehr.sharingcenter.util.EformParser.java

@Override
public void startElement(String namespaceURI, String sName, // simple name
        String qName, // qualified name
        Attributes attrs) throws SAXException {

    boolean isHTMLElement = false;
    boolean isFormElement = false;

    String eName = sName; // element name
    if ("".equals(eName)) {
        eName = qName; // not namespace-aware
    }/*  ww  w. j a v  a 2  s  .c o m*/
    echoText();
    emit("<" + eName);

    if (eName.equalsIgnoreCase("html")) {
        isHTMLElement = true;
    } else if (eName.equalsIgnoreCase("form")) {
        isFormElement = true;
    }

    if (attrs != null) {
        if (isHTMLElement) {
            emit(" xmlns=\"http://www.w3.org/1999/xhtml\"");
        }
        for (int i = 0; i < attrs.getLength(); i++) {
            String aName = attrs.getLocalName(i); // Attr name

            if ("".equals(aName)) {
                aName = attrs.getQName(i);
            }
            emit(" ");

            String attributeValue = StringUtils.replaceEach(attrs.getValue(i),
                    new String[] { "&", "\"", "<", ">" }, new String[] { "&amp;", "&quot;", "&lt;", "&gt;" });

            emit(aName + "=\"" + attributeValue + "\"");
        }

    }

    //Check if this element name is in the array of self-closing names array.
    if (isSelfClosing(eName)) {
        suppressNextClosingTag = true;
        emit(" />");
        return;
    } else {

        if (eName.equals("style") || eName.equals("script")) {
            emit("><![CDATA[");
        } else {
            emit(">");
        }
    }

    if (isFormElement) {
        nl();
        //emit("<input type=\"hidden\" name=\"pcsFormId\" value=\"" + formSerialNumber + "\" />");
        nl();
    }

}

From source file:org.pentaho.reporting.engine.classic.core.modules.parser.bundle.layout.elements.AbstractElementReadHandler.java

/**
 * Starts parsing./*from   w  w  w . j  a v a 2 s  . co m*/
 *
 * @param attrs
 *          the attributes.
 * @throws SAXException
 *           if there is a parsing error.
 */
protected void startParsing(final Attributes attrs) throws SAXException {
    final ReportElement element = getElement();
    if (element == null) {
        throw new IllegalStateException("Failed at " + getClass());
    }

    final int length = attrs.getLength();
    for (int i = 0; i < length; i++) {
        if ("xmlns".equals(attrs.getQName(i)) || attrs.getQName(i).startsWith("xmlns:")) {
            // workaround for buggy parsers
            continue;
        }
        final String name = attrs.getLocalName(i);
        if (name.indexOf(':') > -1) {
            // attribute with ':' are not valid and indicate a namespace definition or so
            continue;
        }
        final String namespace = attrs.getURI(i);
        final String attributeValue = attrs.getValue(i);

        setAttributeValue(element, namespace, name, attributeValue, ReportAttributeMap.EMPTY_MAP);
    }
}

From source file:org.regenstrief.util.XMLUtil.java

/**
 * Creates a node of an XML Document/*w ww  .ja v  a 2  s. c o  m*/
 * 
 * @param doc the XML Document
 * @param namespaceURI the namespace URI
 * @param tagName the tag name of the XML element
 * @param attrs the Attributes of the XML element
 * @return the new Element
 **/
public final static Element createNode(final Document doc, final String namespaceURI, final String tagName,
        final Attributes attrs) {
    if ((doc == null) || (tagName == null)) {
        return null;
    }

    final Element e = doc.createElementNS(namespaceURI, tagName);

    for (int i = 0, len = attrs.getLength(); i < len; i++) {
        final String uri = attrs.getURI(i);
        if (Util.isEmpty(uri)) {
            e.setAttribute(attrs.getQName(i), attrs.getValue(i));
        } else {
            e.setAttributeNS(uri, attrs.getQName(i), attrs.getValue(i));
        }
    }

    return e;
}