Example usage for org.xml.sax.helpers AttributesImpl getIndex

List of usage examples for org.xml.sax.helpers AttributesImpl getIndex

Introduction

In this page you can find the example usage for org.xml.sax.helpers AttributesImpl getIndex.

Prototype

public int getIndex(String qName) 

Source Link

Document

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

Usage

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLInputElement.java

/**
 * Sets the value of the attribute {@code type}.
 * Note: this replace the DOM node with a new one.
 * @param newType the new type to set/*from www  .  j av  a  2 s . co m*/
 */
@JsxSetter
public void setType(String newType) {
    HtmlInput input = getDomNodeOrDie();

    final String currentType = input.getAttribute("type");

    if (!currentType.equalsIgnoreCase(newType)) {
        if (newType != null && getBrowserVersion().hasFeature(JS_INPUT_SET_TYPE_LOWERCASE)) {
            newType = newType.toLowerCase(Locale.ROOT);
        }
        final AttributesImpl attributes = readAttributes(input);
        final int index = attributes.getIndex("type");
        if (index > -1) {
            attributes.setValue(index, newType);
        } else {
            attributes.addAttribute(null, "type", "type", null, newType);
        }

        // create a new one only if we have a new type
        if (ATTRIBUTE_NOT_DEFINED != currentType || !"text".equalsIgnoreCase(newType)) {
            final HtmlInput newInput = (HtmlInput) InputElementFactory.instance.createElement(input.getPage(),
                    "input", attributes);

            if (input.wasCreatedByJavascript()) {
                newInput.markAsCreatedByJavascript();
            }

            if (input.getParentNode() == null) {
                // the input hasn't yet been inserted into the DOM tree (likely has been
                // created via document.createElement()), so simply replace it with the
                // new Input instance created in the code above
                input = newInput;
            } else {
                input.getParentNode().replaceChild(newInput, input);
            }

            input.setScriptableObject(null);
            setDomNode(newInput, true);
        } else {
            super.setAttribute("type", newType);
        }
    }
}

From source file:com.netspective.axiom.schema.column.BasicColumn.java

public void addSchemaRecordEditorDialogTemplates(TemplateElement dialogTemplate, Map jexlVars) {
    jexlVars.put("column", this);

    ForeignKey fKey = getForeignKey();/*from w  w  w  .  j  a v  a 2 s  . com*/
    if (fKey != null && fKey.getReferencedColumns().getFirst().getTable() instanceof EnumerationTable) {
        addEnumerationSchemaRecordEditorDialogTemplates(dialogTemplate, jexlVars);
        return;
    }

    TemplateProducer columnPresentationTemplates = getPresentation();
    if (columnPresentationTemplates.getInstances().size() > 0) {
        // get only the last template because if there was inheritace of a data-type we want the "final" one
        Template columnPresentationTemplate = (Template) columnPresentationTemplates.getInstances()
                .get(columnPresentationTemplates.getInstances().size() - 1);
        List copyColumnPresTmplChildren = columnPresentationTemplate.getChildren();
        for (int i = 0; i < copyColumnPresTmplChildren.size(); i++) {
            TemplateNode colTmplChildNode = (TemplateNode) copyColumnPresTmplChildren.get(i);
            if (colTmplChildNode instanceof TemplateElement) {
                TemplateElement elem = dialogTemplate.addCopyOfChildAndReplaceExpressions(
                        (TemplateElement) colTmplChildNode, jexlVars, true);
                if (elem.getElementName().equals("field")) {
                    boolean changedAttrs = false;
                    AttributesImpl attrs = new AttributesImpl(elem.getAttributes());
                    if (isPrimaryKey()
                            && (attrs.getIndex("primary-key") == -1 && attrs.getIndex("primarykey") == -1
                                    && attrs.getIndex("primary-key-generated") == -1
                                    && attrs.getIndex("primarykeygenerated") == -1)) {
                        if (this instanceof GeneratedValueColumn)
                            attrs.addAttribute(null, null, "primary-key-generated", "CDATA", "yes");
                        else
                            attrs.addAttribute(null, null, "primary-key", "CDATA", "yes");
                        if (attrs.getIndex("required") == -1) // unless required is being overidden, make the primary key field required
                            attrs.addAttribute(null, null, "required", "CDATA", "yes");
                        changedAttrs = true;
                    }

                    if (isRequiredByApp() && attrs.getIndex("required") == -1) {
                        attrs.addAttribute(null, null, "required", "CDATA", "yes");
                        changedAttrs = true;
                    }
                    if (changedAttrs)
                        elem.setAttributes(attrs);
                }
            } else if (colTmplChildNode instanceof TemplateText)
                dialogTemplate.addChild(
                        new TemplateText(dialogTemplate, ((TemplateText) colTmplChildNode).getText()));
            else
                throw new RuntimeException("This should never happen.");
        }
    }
}

From source file:org.apache.axis.message.MessageElement.java

/**
 * remove a named attribute./*from  w  ww  . ja  v  a 2  s .co m*/
 * @see org.w3c.dom.Element#removeAttribute(String)
 * @param attrName name of the attributes
 * @throws DOMException
 */
public void removeAttribute(String attrName) throws DOMException {
    AttributesImpl impl = (AttributesImpl) attributes;
    int index = impl.getIndex(attrName);
    if (index >= 0) {
        AttributesImpl newAttrs = new AttributesImpl();
        // copy except the removed attribute
        for (int i = 0; i < impl.getLength(); i++) { // shift after removal
            if (i != index) {
                String uri = impl.getURI(i);
                String local = impl.getLocalName(i);
                String qname = impl.getQName(i);
                String type = impl.getType(i);
                String value = impl.getValue(i);
                newAttrs.addAttribute(uri, local, qname, type, value);
            }
        }
        // replace it
        attributes = newAttrs;
    }
}

From source file:org.apache.axis.message.MessageElement.java

/**
 * set or update an attribute./*from   w  w w  . ja v a2 s.c om*/
 * @see org.w3c.dom.Element#setAttribute(String, String)
 * @param name attribute name
 * @param value attribute value
 * @throws DOMException
 */
public void setAttribute(String name, String value) throws DOMException {
    AttributesImpl impl = makeAttributesEditable();
    int index = impl.getIndex(name);
    if (index < 0) { // not found
        String uri = "";
        String localname = name;
        String qname = name;
        String type = "CDDATA";
        impl.addAttribute(uri, localname, qname, type, value);
    } else { // found
        impl.setLocalName(index, value);
    }
}

From source file:org.apache.cocoon.forms.transformation.EffectWidgetReplacingPipe.java

private Attributes translateAttributes(Attributes attributes, String[] names) {
    AttributesImpl newAtts = new AttributesImpl(attributes);
    if (names != null) {
        for (int i = 0; i < names.length; i++) {
            String name = names[i];
            int position = newAtts.getIndex(name);
            String newValue = pipeContext.translateText(newAtts.getValue(position));
            if (position > -1)
                newAtts.setValue(position, newValue);
            else/* w w  w  .  j a va2 s  .com*/
                throw new RuntimeException("Attribute \"" + name + "\" not present!");
        }
    }
    return newAtts;
}

From source file:org.apache.cocoon.transformation.JPathTransformer.java

/**
 * Helper method to check for the existance of an attribute named
 * jpath:action. If existing the string 'id' is replaced with the
 * continuation id.//from   www. ja v  a  2s  . c  o  m
 *
 * @param a an {@link AttributesImpl} instance
 */
private void checkJPathAction(final AttributesImpl a) {

    // check for jpath:action attribute
    int idx = a.getIndex(JPATH_ACTION);

    if (idx != -1 && JPATH_NAMESPACE_URI.equals(a.getURI(idx))) {
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("found jpath:action, adjusting");
        }

        String value = a.getValue(idx);

        // REVISIT(MC): support for continuation level
        String id = m_kont.getContinuation(0).getId();

        a.removeAttribute(idx);
        a.addAttribute("", "action", "action", "CDATA", m_re.subst(value, id));
    }
}

From source file:org.apache.cocoon.transformation.SimpleFormTransformer.java

/**
 * Handle input elements that may have a "checked" attributes,
 * i.e. checkbox and radio./*from  ww  w.  j ava2 s  . co m*/
 */
protected void startCheckableElement(String aName, String uri, String name, String raw,
        AttributesImpl attributes) throws SAXException {

    // @fixed and this.fixed already considered in startInputElement
    this.values = this.getValues(aName);
    String checked = attributes.getValue("checked");
    String value = attributes.getValue("value");
    boolean found = false;

    if (getLogger().isDebugEnabled())
        getLogger().debug("startCheckableElement " + name + " attributes " + this.printAttributes(attributes));
    if (this.values != null) {
        if (getLogger().isDebugEnabled())
            getLogger().debug("replacing");
        for (int i = 0; i < this.values.length; i++) {
            if (this.values[i].equals(value)) {
                found = true;
                if (checked == null) {
                    attributes.addAttribute("", "checked", "checked", "CDATA", "");
                }
                break;
            }
        }
        if (!found && checked != null) {
            attributes.removeAttribute(attributes.getIndex("checked"));
        }
    }
    this.relayStartElement(uri, name, raw, attributes);
}

From source file:org.apache.cocoon.transformation.SimpleFormTransformer.java

/**
 * Handle input elements that may don't have a "checked"
 * attributes, e.g. text, password, button.
 *//*from ww  w.  j av  a  2 s.c o m*/
protected void startNonCheckableElement(String aName, String uri, String name, String raw,
        AttributesImpl attributes) throws SAXException {

    // @fixed and this.fixed already considered in startInputElement
    Object fValue = this.getNextValue(aName);
    String value = attributes.getValue("value");
    if (getLogger().isDebugEnabled())
        getLogger()
                .debug("startNonCheckableElement " + name + " attributes " + this.printAttributes(attributes));
    if (fValue != null) {
        if (getLogger().isDebugEnabled())
            getLogger().debug("replacing");
        if (value != null) {
            attributes.setValue(attributes.getIndex("value"), String.valueOf(fValue));
        } else {
            attributes.addAttribute("", "value", "value", "CDATA", String.valueOf(fValue));
        }
    }
    this.relayStartElement(uri, name, raw, attributes);
}

From source file:org.apache.cocoon.transformation.SimpleFormTransformer.java

/**
 * Handle option elements. Uses instance variables set up by
 * startSelectElement. Relies on option having a "value"
 * attribute, i.e. does not check following characters if "value"
 * is not present./* w  ww  .  j a v a 2s.com*/
 */
protected void startOptionElement(String uri, String name, String raw, Attributes attr) throws SAXException {

    // add @selected if @value in request.getParameterValues(@name)
    if (getLogger().isDebugEnabled())
        getLogger().debug("startOptionElement " + name + " attributes " + this.printAttributes(attr));
    if (this.values == null || this.fixed) {
        this.relayStartElement(uri, name, raw, attr);
    } else {
        if (getLogger().isDebugEnabled())
            getLogger().debug("replacing");
        AttributesImpl attributes = null;
        if (attr instanceof AttributesImpl) {
            attributes = (AttributesImpl) attr;
        } else {
            attributes = new AttributesImpl(attr);
        }
        String selected = attributes.getValue("selected");
        String value = attributes.getValue("value");
        boolean found = false;

        for (int i = 0; i < this.values.length; i++) {
            if (this.values[i].equals(value)) {
                found = true;
                if (selected == null) {
                    attributes.addAttribute("", "selected", "selected", "CDATA", "");
                }
                break;
            }
        }
        if (!found && selected != null) {
            attributes.removeAttribute(attributes.getIndex("selected"));
        }

        this.relayStartElement(uri, name, raw, attributes);
    }
}

From source file:org.apache.cocoon.transformation.SimpleFormTransformer.java

/**
 * Handle error elements. If validation results are available,
 * compares validation result for parameter with the same name as
 * the "name" attribute with the result names is "when" and
 * "when-ge". Drops element and all nested events when error
 * condition is not met.//from   w  w w .  j a  va  2  s.  c om
 */
protected void startErrorElement(String uri, String name, String raw, Attributes attr) throws SAXException {

    if (getLogger().isDebugEnabled())
        getLogger().debug("startErrorElement " + name + " attributes " + this.printAttributes(attr));
    if (this.ignoreValidation) {
        this.relayStartElement(uri, name, raw, attr);
    } else if (this.validationResults == null || this.fixed) {
        this.relayStartElement(true, false, uri, name, raw, attr);
    } else {
        String aName = attr.getValue("name");
        if (aName == null) {
            this.relayStartElement(uri, name, raw, attr);
        } else {
            ValidatorActionResult validation = FormValidatorHelper.getParamResult(this.objectModel, aName);
            String when = attr.getValue("when");
            String when_ge = attr.getValue("when-ge");

            if ((when != null && when.equals(validatorResults.get(validation)))
                    || (when_ge != null && validation.ge((ValidatorActionResult) validatorResultLabel
                            .get(when_ge, ValidatorActionResult.MAXERROR)))) {
                AttributesImpl attributes = null;
                if (attr instanceof AttributesImpl) {
                    attributes = (AttributesImpl) attr;
                } else {
                    attributes = new AttributesImpl(attr);
                }
                // remove attributes not meant for client
                attributes.removeAttribute(attributes.getIndex("name"));
                if (when != null)
                    attributes.removeAttribute(attributes.getIndex("when"));
                if (when_ge != null)
                    attributes.removeAttribute(attributes.getIndex("when-ge"));
                this.relayStartElement(uri, name, raw, this.normalizeAttributes(attributes));
            } else {
                this.relayStartElement(true, true, uri, name, raw, attr);
            }
        }
    }
}