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.apache.jasper.compiler.JspUtil.java

/**
 * Checks if all mandatory attributes are present and if all attributes
 * present have valid names.  Checks attributes specified as XML-style
 * attributes as well as attributes specified using the jsp:attribute
 * standard action. //from   ww  w  .  j av  a2 s  .  c  o m
 */
public static void checkAttributes(String typeOfTag, Node n, ValidAttribute[] validAttributes,
        ErrorDispatcher err) throws JasperException {
    Attributes attrs = n.getAttributes();
    Mark start = n.getStart();
    boolean valid = true;

    // AttributesImpl.removeAttribute is broken, so we do this...
    int tempLength = (attrs == null) ? 0 : attrs.getLength();
    Vector temp = new Vector(tempLength, 1);
    for (int i = 0; i < tempLength; i++) {
        String qName = attrs.getQName(i);
        if ((!qName.equals("xmlns")) && (!qName.startsWith("xmlns:")))
            temp.addElement(qName);
    }

    // Add names of attributes specified using jsp:attribute
    Node.Nodes tagBody = n.getBody();
    if (tagBody != null) {
        int numSubElements = tagBody.size();
        for (int i = 0; i < numSubElements; i++) {
            Node node = tagBody.getNode(i);
            if (node instanceof Node.NamedAttribute) {
                String attrName = node.getAttributeValue("name");
                temp.addElement(attrName);
                // Check if this value appear in the attribute of the node
                if (n.getAttributeValue(attrName) != null) {
                    err.jspError(n, "jsp.error.duplicate.name.jspattribute", attrName);
                }
            } else {
                // Nothing can come before jsp:attribute, and only
                // jsp:body can come after it.
                break;
            }
        }
    }

    /*
     * First check to see if all the mandatory attributes are present.
     * If so only then proceed to see if the other attributes are valid
     * for the particular tag.
     */
    String missingAttribute = null;

    for (int i = 0; i < validAttributes.length; i++) {
        int attrPos;
        if (validAttributes[i].mandatory) {
            attrPos = temp.indexOf(validAttributes[i].name);
            if (attrPos != -1) {
                temp.remove(attrPos);
                valid = true;
            } else {
                valid = false;
                missingAttribute = validAttributes[i].name;
                break;
            }
        }
    }

    // If mandatory attribute is missing then the exception is thrown
    if (!valid)
        err.jspError(start, "jsp.error.mandatory.attribute", typeOfTag, missingAttribute);

    // Check to see if there are any more attributes for the specified tag.
    int attrLeftLength = temp.size();
    if (attrLeftLength == 0)
        return;

    // Now check to see if the rest of the attributes are valid too.
    String attribute = null;

    for (int j = 0; j < attrLeftLength; j++) {
        valid = false;
        attribute = (String) temp.elementAt(j);
        for (int i = 0; i < validAttributes.length; i++) {
            if (attribute.equals(validAttributes[i].name)) {
                valid = true;
                break;
            }
        }
        if (!valid)
            err.jspError(start, "jsp.error.invalid.attribute", typeOfTag, attribute);
    }
    // XXX *could* move EL-syntax validation here... (sb)
}

From source file:org.apache.struts2.jasper.compiler.JspUtil.java

/**
 * Checks if all mandatory attributes are present and if all attributes
 * present have valid names.  Checks attributes specified as XML-style
 * attributes as well as attributes specified using the jsp:attribute
 * standard action./* w  ww.j  av  a2s. c o m*/
 *
 * @param typeOfTag       type of tag
 * @param n               node
 * @param validAttributes valid attributes
 * @param err             error dispatcher
 * @throws JasperException in case of Jasper errors
 */
public static void checkAttributes(String typeOfTag, Node n, ValidAttribute[] validAttributes,
        ErrorDispatcher err) throws JasperException {
    Attributes attrs = n.getAttributes();
    Mark start = n.getStart();
    boolean valid = true;

    // AttributesImpl.removeAttribute is broken, so we do this...
    int tempLength = (attrs == null) ? 0 : attrs.getLength();
    Vector temp = new Vector(tempLength, 1);
    for (int i = 0; i < tempLength; i++) {
        String qName = attrs.getQName(i);
        if ((!qName.equals("xmlns")) && (!qName.startsWith("xmlns:")))
            temp.addElement(qName);
    }

    // Add names of attributes specified using jsp:attribute
    Node.Nodes tagBody = n.getBody();
    if (tagBody != null) {
        int numSubElements = tagBody.size();
        for (int i = 0; i < numSubElements; i++) {
            Node node = tagBody.getNode(i);
            if (node instanceof Node.NamedAttribute) {
                String attrName = node.getAttributeValue("name");
                temp.addElement(attrName);
                // Check if this value appear in the attribute of the node
                if (n.getAttributeValue(attrName) != null) {
                    err.jspError(n, "jsp.error.duplicate.name.jspattribute", attrName);
                }
            } else {
                // Nothing can come before jsp:attribute, and only
                // jsp:body can come after it.
                break;
            }
        }
    }

    /*
     * First check to see if all the mandatory attributes are present.
     * If so only then proceed to see if the other attributes are valid
     * for the particular tag.
     */
    String missingAttribute = null;

    for (ValidAttribute validAttribute : validAttributes) {
        int attrPos;
        if (validAttribute.mandatory) {
            attrPos = temp.indexOf(validAttribute.name);
            if (attrPos != -1) {
                temp.remove(attrPos);
                valid = true;
            } else {
                valid = false;
                missingAttribute = validAttribute.name;
                break;
            }
        }
    }

    // If mandatory attribute is missing then the exception is thrown
    if (!valid)
        err.jspError(start, "jsp.error.mandatory.attribute", typeOfTag, missingAttribute);

    // Check to see if there are any more attributes for the specified tag.
    int attrLeftLength = temp.size();
    if (attrLeftLength == 0)
        return;

    // Now check to see if the rest of the attributes are valid too.
    String attribute = null;

    for (int j = 0; j < attrLeftLength; j++) {
        valid = false;
        attribute = (String) temp.elementAt(j);
        for (ValidAttribute validAttribute : validAttributes) {
            if (attribute.equals(validAttribute.name)) {
                valid = true;
                break;
            }
        }
        if (!valid)
            err.jspError(start, "jsp.error.invalid.attribute", typeOfTag, attribute);
    }
    // XXX *could* move EL-syntax validation here... (sb)
}

From source file:org.apache.syncope.core.persistence.jpa.content.ContentLoaderHandler.java

private Object[] getParameters(final String tableName, final Attributes attrs) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

    Map<String, Integer> colTypes = jdbcTemplate.query("SELECT * FROM " + tableName + " WHERE 0=1",
            new ResultSetExtractor<Map<String, Integer>>() {

                @Override//ww w .j  a v a 2  s  . com
                public Map<String, Integer> extractData(final ResultSet rs) throws SQLException {
                    Map<String, Integer> colTypes = new HashMap<>();
                    for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                        colTypes.put(rs.getMetaData().getColumnName(i).toUpperCase(),
                                rs.getMetaData().getColumnType(i));
                    }
                    return colTypes;
                }
            });

    Object[] parameters = new Object[attrs.getLength()];
    for (int i = 0; i < attrs.getLength(); i++) {
        Integer colType = colTypes.get(attrs.getQName(i).toUpperCase());
        if (colType == null) {
            LOG.warn("No column type found for {}", attrs.getQName(i).toUpperCase());
            colType = Types.VARCHAR;
        }

        switch (colType) {
        case Types.INTEGER:
        case Types.TINYINT:
        case Types.SMALLINT:
            try {
                parameters[i] = Integer.valueOf(attrs.getValue(i));
            } catch (NumberFormatException e) {
                LOG.error("Unparsable Integer '{}'", attrs.getValue(i));
                parameters[i] = attrs.getValue(i);
            }
            break;

        case Types.NUMERIC:
        case Types.DECIMAL:
        case Types.BIGINT:
            try {
                parameters[i] = Long.valueOf(attrs.getValue(i));
            } catch (NumberFormatException e) {
                LOG.error("Unparsable Long '{}'", attrs.getValue(i));
                parameters[i] = attrs.getValue(i);
            }
            break;

        case Types.DOUBLE:
            try {
                parameters[i] = Double.valueOf(attrs.getValue(i));
            } catch (NumberFormatException e) {
                LOG.error("Unparsable Double '{}'", attrs.getValue(i));
                parameters[i] = attrs.getValue(i);
            }
            break;

        case Types.REAL:
        case Types.FLOAT:
            try {
                parameters[i] = Float.valueOf(attrs.getValue(i));
            } catch (NumberFormatException e) {
                LOG.error("Unparsable Float '{}'", attrs.getValue(i));
                parameters[i] = attrs.getValue(i);
            }
            break;

        case Types.DATE:
        case Types.TIME:
        case Types.TIMESTAMP:
            try {
                parameters[i] = FormatUtils.parseDate(attrs.getValue(i));
            } catch (ParseException e) {
                LOG.error("Unparsable Date '{}'", attrs.getValue(i));
                parameters[i] = attrs.getValue(i);
            }
            break;

        case Types.BIT:
        case Types.BOOLEAN:
            parameters[i] = "1".equals(attrs.getValue(i)) ? Boolean.TRUE : Boolean.FALSE;
            break;

        case Types.BINARY:
        case Types.VARBINARY:
        case Types.LONGVARBINARY:
            try {
                parameters[i] = Hex.decodeHex(attrs.getValue(i).toCharArray());
            } catch (DecoderException | IllegalArgumentException e) {
                parameters[i] = attrs.getValue(i);
            }
            break;

        case Types.BLOB:
            try {
                parameters[i] = Hex.decodeHex(attrs.getValue(i).toCharArray());
            } catch (DecoderException | IllegalArgumentException e) {
                LOG.warn("Error decoding hex string to specify a blob parameter", e);
                parameters[i] = attrs.getValue(i);
            } catch (Exception e) {
                LOG.warn("Error creating a new blob parameter", e);
            }
            break;

        default:
            parameters[i] = attrs.getValue(i);
        }
    }

    return parameters;
}

From source file:org.apache.syncope.core.persistence.jpa.content.ContentLoaderHandler.java

@Override
public void startElement(final String uri, final String localName, final String qName, final Attributes atts)
        throws SAXException {

    // skip root element
    if (rootElement.equals(qName)) {
        return;/*from  ww w . j  a v  a  2  s.co  m*/
    }

    StringBuilder query = new StringBuilder("INSERT INTO ").append(qName).append('(');

    StringBuilder values = new StringBuilder();

    for (int i = 0; i < atts.getLength(); i++) {
        query.append(atts.getQName(i));
        values.append('?');
        if (i < atts.getLength() - 1) {
            query.append(',');
            values.append(',');
        }
    }
    query.append(") VALUES (").append(values).append(')');

    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    try {
        jdbcTemplate.update(query.toString(), getParameters(qName, atts));
    } catch (DataAccessException e) {
        LOG.error("While trying to perform {}", query, e);
        if (!continueOnError) {
            throw e;
        }
    }
}

From source file:org.apache.tapestry.parse.AbstractSpecificationRule.java

protected String getValue(Attributes attributes, String name) {
    int count = attributes.getLength();

    for (int i = 0; i < count; i++) {
        String attributeName = attributes.getLocalName(i);

        if (Tapestry.isBlank(attributeName))
            attributeName = attributes.getQName(i);

        if (attributeName.equals(name))
            return attributes.getValue(i);
    }/*  w  w  w . j a  v  a  2s.  c o m*/

    return null;
}

From source file:org.apache.tapestry.parse.SetLimitedPropertiesRule.java

public void begin(String namespace, String name, Attributes attributes) throws Exception {
    Object top = digester.peek();

    int count = attributes.getLength();

    for (int i = 0; i < count; i++) {
        String attributeName = attributes.getLocalName(i);

        if (Tapestry.isBlank(attributeName))
            attributeName = attributes.getQName(i);

        for (int x = 0; x < _attributeNames.length; x++) {
            if (_attributeNames[x].equals(attributeName)) {
                String value = attributes.getValue(i);
                String propertyName = _propertyNames[x];

                PropertyUtils.setProperty(top, propertyName, value);

                // Terminate inner loop when attribute name is found.

                break;
            }//from  www.java2  s. com
        }
    }
}

From source file:org.apache.torque.engine.database.transform.XmlToData.java

/**
 * Handles opening elements of the xml file.
 *//*from w ww . j  av a 2 s . c  o m*/
public void startElement(String uri, String localName, String rawName, Attributes attributes)
        throws SAXException {
    try {
        if (rawName.equals("dataset")) {
            //ignore <dataset> for now.
        } else {
            Table table = database.getTableByJavaName(rawName);

            if (table == null) {
                throw new SAXException("Table '" + rawName + "' unknown");
            }
            List columnValues = new ArrayList();
            for (int i = 0; i < attributes.getLength(); i++) {
                Column col = table.getColumnByJavaName(attributes.getQName(i));

                if (col == null) {
                    throw new SAXException(
                            "Column " + attributes.getQName(i) + " in table " + rawName + " unknown.");
                }

                String value = attributes.getValue(i);
                columnValues.add(new ColumnValue(col, value));
            }
            data.add(new DataRow(table, columnValues));
        }
    } catch (Exception e) {
        throw new SAXException(e);
    }
}

From source file:org.apereo.portal.groups.GroupServiceConfiguration.java

/**
 *
 *//*from   w w  w  . j  av a  2  s .c  om*/
protected void parseAttributes(Attributes atts) {
    String name, value;
    for (int i = 0; i < atts.getLength(); i++) {
        name = atts.getQName(i);
        value = atts.getValue(i);
        getAttributes().put(name, value);
    }
}

From source file:org.cee.highlighter.impl.HighlightWriter.java

public void writeStartTag(String qname, Attributes attributes, String[] attributeOverrides) {
    try {/*from   w ww.j  av  a2 s  .  c o  m*/
        output.append('<').append(qname);
        for (int i = 0; i < attributes.getLength(); i++) {
            final String attr = attributes.getQName(i);
            String value = null;
            if (attributeOverrides != null) {
                value = attributeOverrides[i];
            }
            if (value == null) {
                value = attributes.getValue(i);
            }
            output.append(' ').append(attr).append("=\"").append(value).append("\"");
        }
        output.append('>');
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

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

private void writeStartElement(final String qName, final Attributes atts) throws IOException {
    final int attsLen = atts.getLength();
    output.write(LESS_THAN + qName);/*from  w  ww .  jav a  2s  .  co  m*/
    for (int i = 0; i < attsLen; i++) {
        final String attQName = atts.getQName(i);
        final String attValue = escapeXML(atts.getValue(i));
        output.write(STRING_BLANK + attQName + EQUAL + QUOTATION + attValue + QUOTATION);
    }
    output.write(GREATER_THAN);
}