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:com.amazonaws.services.s3.model.transform.XmlResponsesSaxParser.java

private static String findAttributeValue(String qnameToFind, Attributes attrs) {

    for (int i = 0; i < attrs.getLength(); i++) {
        String qname = attrs.getQName(i);
        if (qname.trim().equalsIgnoreCase(qnameToFind.trim())) {
            return attrs.getValue(i);
        }//from  w  w  w.  j  a  v  a2s  .  c om
    }

    return null;
}

From source file:it.wami.map.mongodeploy.OsmSaxHandler.java

/**
 * /*w w  w . ja va2 s .com*/
 * @param atts the Attributes
 */
private void processMember(Attributes atts) {
    BasicDBObject[] members = (BasicDBObject[]) entry.get(Relation.MEMBERS);

    if (members == null) {
        members = new BasicDBObject[1];
    } else {
        BasicDBObject[] tmp = new BasicDBObject[members.length + 1];
        System.arraycopy(members, 0, tmp, 0, members.length);
        members = tmp;
    }

    BasicDBObject member = new BasicDBObject();

    for (int i = 0; i < atts.getLength(); i++) {
        String key = atts.getQName(i);
        String value = atts.getValue(i);
        member.append(key, value);
    }

    members[members.length - 1] = member;
    entry.append(Relation.MEMBERS, members);
}

From source file:com.jkoolcloud.jesl.simulator.TNT4JSimulatorParserHandler.java

private void pauseSimulator(Attributes attributes) throws SAXException {
    long usec = 0;
    int i;//from ww w.  j  a  v a2  s.co m

    for (i = 0; i < attributes.getLength(); i++) {
        String attName = attributes.getQName(i);
        String attValue = expandEnvVars(attributes.getValue(i));

        if (attName.equals(SIM_XML_ATTR_MSEC))
            usec = Long.parseLong(attValue) * 1000L;
        else if (attName.equals(SIM_XML_ATTR_USEC))
            usec = Long.parseLong(attValue);
        else
            throw new SAXParseException("Unknown <" + SIM_XML_SLEEP + "> attribute '" + attName + "'",
                    saxLocator);
    }

    if (usec > 0) {
        simCurrTime.add(0L, (long) TNT4JSimulator.varyValue(usec));
        TNT4JSimulator.trace(simCurrTime, "Executed sleep, usec=" + usec);
    }
}

From source file:Writer.java

/** Returns a sorted list of attributes. */
protected Attributes sortAttributes(Attributes attrs) {

    AttributesImpl attributes = new AttributesImpl();

    int len = (attrs != null) ? attrs.getLength() : 0;
    for (int i = 0; i < len; i++) {
        String name = attrs.getQName(i);
        int count = attributes.getLength();
        int j = 0;
        while (j < count) {
            if (name.compareTo(attributes.getQName(j)) < 0) {
                break;
            }//from w ww . j  a v a2s.  c o  m
            j++;
        }
        attributes.insertAttributeAt(j, name, attrs.getType(i), attrs.getValue(i));
    }

    return attributes;

}

From source file:com.jkoolcloud.jesl.simulator.TNT4JSimulatorParserHandler.java

private void defineOption(Attributes attributes) throws SAXException {
    String name = null;//from   www  .  j a  v  a 2s .co  m
    String value = null;

    try {
        for (int i = 0; i < attributes.getLength(); i++) {
            String attName = attributes.getQName(i);
            String attValue = expandEnvVars(attributes.getValue(i));

            if (attName.equals(SIM_XML_ATTR_NAME))
                name = attValue;
            else if (attName.equals(SIM_XML_ATTR_VALUE)) {
                value = attValue;
                String[] args = value.split(",");
                TNT4JSimulator.processArgs(this, args);
            } else {
                throw new SAXParseException("Unknown <" + SIM_XML_PROP + "> attribute '" + attName + "'",
                        saxLocator);
            }
        }

        if (StringUtils.isEmpty(name))
            throw new SAXParseException("<" + SIM_XML_VAR + ">: must specify '" + SIM_XML_ATTR_NAME + "'",
                    saxLocator);
        TNT4JSimulator.trace(simCurrTime, "Defining option: '" + name + "=" + value + "'");
    } catch (Exception e) {
        if (e instanceof SAXException)
            throw (SAXException) e;
        throw new SAXException("Failed processing definition for option '" + name + "': " + e, e);
    }
}

From source file:com.jkoolcloud.jesl.simulator.TNT4JSimulatorParserHandler.java

private void defineVar(Attributes attributes) throws SAXException {
    String name = null;/*w w w.  j a v a2s . co  m*/
    String value = null;

    try {
        for (int i = 0; i < attributes.getLength(); i++) {
            String attName = attributes.getQName(i);
            String attValue = expandEnvVars(attributes.getValue(i));

            if (attName.equals(SIM_XML_ATTR_NAME))
                name = attValue;
            else if (attName.equals(SIM_XML_ATTR_VALUE)) {
                value = processVarValue(attValue);
            } else {
                throw new SAXParseException("Unknown <" + SIM_XML_PROP + "> attribute '" + attName + "'",
                        saxLocator);
            }
        }

        if (StringUtils.isEmpty(name))
            throw new SAXParseException("<" + SIM_XML_VAR + ">: must specify '" + SIM_XML_ATTR_NAME + "'",
                    saxLocator);

        if (value.equalsIgnoreCase("=?")) {
            // requires input if not defined
            String oVal = vars.get(name);
            if (oVal == null) {
                value = processVarValue(TNT4JSimulator.readFromConsole("\nDefine variable [" + name + "]:"));
            } else {
                TNT4JSimulator.trace(simCurrTime, "Skipping duplicate variable: '" + name + "=" + value
                        + "', existing.value='" + oVal + "'");
            }
        }

        String eVal = vars.putIfAbsent(name, value);
        if (eVal != null) {
            TNT4JSimulator.trace(simCurrTime,
                    "Skipping duplicate variable: '" + name + "=" + value + "', existing.value='" + eVal + "'");
        }
        TNT4JSimulator.trace(simCurrTime, "Defining variable: '" + name + "=" + value + "'");
    } catch (Exception e) {
        if (e instanceof SAXException)
            throw (SAXException) e;
        throw new SAXException("Failed processing definition for variable '" + name + "': " + e, e);
    }
}

From source file:com.swtxml.tinydom.TinyDomSaxHandler.java

private Map<INamespaceDefinition, Map<IAttributeDefinition, String>> processAttributes(
        INamespaceDefinition tagNamespace, ITagDefinition tagDefinition, Attributes attributes) {

    Map<INamespaceDefinition, Map<IAttributeDefinition, String>> attributeNsMap = new HashMap<INamespaceDefinition, Map<IAttributeDefinition, String>>();
    for (int i = 0; i < attributes.getLength(); i++) {
        String uri = attributes.getURI(i);
        INamespaceDefinition attributeNamespace = !StringUtils.isEmpty(uri) ? getNamespace(uri) : tagNamespace;
        Map<IAttributeDefinition, String> attributeMap = attributeNsMap.get(attributeNamespace);
        if (attributeMap == null) {
            attributeMap = new HashMap<IAttributeDefinition, String>();
            attributeNsMap.put(attributeNamespace, attributeMap);
        }/*from   ww w .ja  v a  2  s  .c  o  m*/
        String name = attributes.getLocalName(i);
        String value = attributes.getValue(i);
        IAttributeDefinition attributeDefinition;
        if (attributeNamespace.equals(tagNamespace)) {
            attributeDefinition = tagDefinition.getAttribute(name);
        } else {
            attributeDefinition = attributeNamespace.getForeignAttribute(name);
            if (attributeDefinition instanceof ITagScope
                    && !((ITagScope) attributeDefinition).isAllowedIn(tagDefinition)) {
                throw new ParseException("Attribute " + attributes.getQName(i) + " is not allowed for tag \""
                        + tagDefinition.getName() + "\"");
            }
        }

        if (attributeDefinition == null) {
            throw new ParseException("Unknown attribute \"" + attributes.getQName(i) + "\" for tag \""
                    + tagDefinition.getName() + "\" (available are: "
                    + CollectionUtils.sortedToString(tagDefinition.getAttributeNames()) + ")");
        }
        attributeMap.put(attributeDefinition, value);

    }
    if (attributeNsMap.isEmpty()) {
        return Collections.emptyMap();
    }
    return attributeNsMap;
}

From source file:ee.ria.xroad.common.message.SaxSoapParserImpl.java

@SneakyThrows
protected void writeStartElementXml(String prefix, QName element, Attributes attributes, Writer writer) {
    writer.append('<');
    String localName = element.getLocalPart();
    String tag = StringUtils.isEmpty(prefix) ? localName : prefix + ":" + localName;
    writer.append(tag);/*from   ww  w.  j a  va 2 s  .  c om*/
    for (int i = 0; i < attributes.getLength(); i++) {
        String escapedAttrValue = StringEscapeUtils.escapeXml11(attributes.getValue(i));
        writer.append(String.format(" %s=\"%s\"", attributes.getQName(i), escapedAttrValue));
    }
    writer.append('>');
}

From source file:XMLWriter.java

/**
 * Write out an attribute list, escaping values.
 *
 * The names will have prefixes added to them.
 *
 * @param atts The attribute list to write.
 * @exception org.xml.SAXException If there is an error writing
 *            the attribute list, this method will throw an
 *            IOException wrapped in a SAXException.
 *///from  ww  w . j  av a  2 s.  c o  m
private void writeAttributes(Attributes atts) throws SAXException {
    int len = atts.getLength();
    for (int i = 0; i < len; i++) {
        char ch[] = atts.getValue(i).toCharArray();
        write(' ');
        writeName(atts.getURI(i), atts.getLocalName(i), atts.getQName(i), false);
        if (htmlMode && booleanAttribute(atts.getLocalName(i), atts.getQName(i), atts.getValue(i)))
            break;
        write("=\"");
        writeEsc(ch, 0, ch.length, true);
        write('"');
    }
}

From source file:MapTranslater.java

public void startElement(String namespaceURI, String sName, // simple name
        String qName, // qualified name
        Attributes attrs) throws SAXException {
    echoText();/*www .jav  a 2s.co m*/

    String eName = sName; // element name

    if ("".equals(eName)) {
        eName = qName; // not namespaceAware
    }

    if (isMapElem(eName)) {
        // New map elem starts. Now we have to look for its names... 
        jaName = null;
        enName = null;
        enNameOnly = null;
        deName = null;
    } else if (eName.equals("tag")) {
        String key = attrs.getValue("k");
        if (key.equals("name:en") || (key.equals("name:ja_rm") && enName == null))
            enName = attrs.getValue("v");
        else if (key.equals("name:de"))
            deName = attrs.getValue("v");
        else if (key.equals("name") || key.equals("name:ja"))
            jaName = attrs.getValue("v");
        if (key.equals("name:en"))
            enNameOnly = attrs.getValue("v");
    }

    emit("<" + eName);

    if (attrs != null) {
        for (int i = 0; i < attrs.getLength(); i++) {
            String aName = attrs.getLocalName(i); // Attr name 

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

            emit(" ");
            emit(aName + "=\"" + StringEscapeUtils.escapeXml(attrs.getValue(i)) + "\"");
        }
    }

    emit(">");
}