Java Code Examples for org.xml.sax.helpers.AttributesImpl#setValue()

The following examples show how to use org.xml.sax.helpers.AttributesImpl#setValue() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Tomcat8-Source-Read   File: Digester.java    License: MIT License 6 votes vote down vote up
/**
 * Returns an attributes list which contains all the attributes
 * passed in, with any text of form "${xxx}" in an attribute value
 * replaced by the appropriate value from the system property.
 */
private Attributes updateAttributes(Attributes list) {

    if (list.getLength() == 0) {
        return list;
    }

    AttributesImpl newAttrs = new AttributesImpl(list);
    int nAttributes = newAttrs.getLength();
    for (int i = 0; i < nAttributes; ++i) {
        String value = newAttrs.getValue(i);
        try {
            newAttrs.setValue(i, IntrospectionUtils.replaceProperties(value, null, source, getClassLoader()).intern());
        } catch (Exception e) {
            log.warn(sm.getString("digester.failedToUpdateAttributes", newAttrs.getLocalName(i), value), e);
        }
    }

    return newAttrs;
}
 
Example 2
Source Project: Tomcat7.0.67   File: Digester.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns an attributes list which contains all the attributes
 * passed in, with any text of form "${xxx}" in an attribute value
 * replaced by the appropriate value from the system property.
 */
private Attributes updateAttributes(Attributes list) {

    if (list.getLength() == 0) {
        return list;
    }
    
    AttributesImpl newAttrs = new AttributesImpl(list);
    int nAttributes = newAttrs.getLength();
    for (int i = 0; i < nAttributes; ++i) {
        String value = newAttrs.getValue(i);
        try {
            String newValue = 
                IntrospectionUtils.replaceProperties(value, null, source);
            if (value != newValue) {
                newAttrs.setValue(i, newValue);
            }
        }
        catch (Exception e) {
            // ignore - let the attribute have its original value
        }
    }

    return newAttrs;

}
 
Example 3
/**
 * Basic test for setLocalName(int, String), setQName(int, String),
 * setType(int, String), setValue(int, String) and setURI(int, String).
 */
@Test
public void testcase07() {
    AttributesImpl attr = new AttributesImpl();
    attr.addAttribute(CAR_URI, CAR_LOCALNAME, CAR_QNAME, CAR_TYPE, CAR_VALUE);
    attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE,
            JEEP_VALUE);
    attr.setLocalName(1, "speclead");
    attr.setQName(1, "megi");
    attr.setType(1, "sax");
    attr.setValue(1, "SAX01");
    attr.setURI(1, "www.megginson.com/sax/sax01");

    assertEquals(attr.getLocalName(1), "speclead");
    assertEquals(attr.getQName(1), "megi");
    assertEquals(attr.getType(1), "sax");
    assertEquals(attr.getType("megi"), "sax");
    assertEquals(attr.getURI(1), "www.megginson.com/sax/sax01");
}
 
Example 4
Source Project: tomcatsrc   File: Digester.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns an attributes list which contains all the attributes
 * passed in, with any text of form "${xxx}" in an attribute value
 * replaced by the appropriate value from the system property.
 */
private Attributes updateAttributes(Attributes list) {

    if (list.getLength() == 0) {
        return list;
    }
    
    AttributesImpl newAttrs = new AttributesImpl(list);
    int nAttributes = newAttrs.getLength();
    for (int i = 0; i < nAttributes; ++i) {
        String value = newAttrs.getValue(i);
        try {
            String newValue = 
                IntrospectionUtils.replaceProperties(value, null, source);
            if (value != newValue) {
                newAttrs.setValue(i, newValue);
            }
        }
        catch (Exception e) {
            // ignore - let the attribute have its original value
        }
    }

    return newAttrs;

}
 
Example 5
public 
 void startElement(String namespaceURI, String localName, String qName,
	    Attributes atts) throws org.xml.sax.SAXException {
   cat.debug("startElement("+namespaceURI+", "+localName+", "+qName+")called");

   if("log4j:event".equals(qName)) {
     cat.debug("-------------");      
     if(atts instanceof org.xml.sax.helpers.AttributesImpl) {
AttributesImpl ai = (AttributesImpl) atts;
int i = atts.getIndex("timestamp");
ai.setValue(i, "hello");
     }
     String ts = atts.getValue("timestamp");
     cat.debug("New timestamp is " + ts);
   }
   chandler.startElement(namespaceURI, localName, qName, atts);
 }
 
Example 6
static
protected Attributes setAttribute(Attributes attributes, String localName, String value){
	int index = attributes.getIndex("", localName);

	AttributesImpl result = new AttributesImpl(attributes);

	if(index < 0){
		result.addAttribute("", localName, "", "CDATA", value); // XXX
	} else

	{
		result.setValue(index, value);
	}

	return result;
}
 
Example 7
/**
 * {@inheritDoc}
 *
 * @see org.xml.sax.helpers.XMLFilterImpl#startElement(java.lang.String,
 * java.lang.String, java.lang.String, org.xml.sax.Attributes)
 */
@Override
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
    AttributesImpl attributes = (attrs instanceof AttributesImpl) ? (AttributesImpl) attrs : new AttributesImpl(attrs);

    int length = attributes.getLength();
    for (int i = 0; i < length; ++i) {
        attributes.setValue(i, this.replace(attributes.getValue(i)));
    }

    super.startElement(uri, localName, qName, attributes);
}
 
Example 8
Source Project: jlibs   File: XMLCrawler.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void startElement(String namespaceURI, String localName, String qualifiedName, Attributes atts) throws SAXException{
    if(depth==0){
        Element elem = current.findChild(namespaceURI, localName);
        if(elem==null)
            depth = 1;
        else{
            current = elem;
            try{
                if(file==null && current.extension!=null)
                    setFile(listener.toFile(url, current.extension));
                if(current.locationAttribute!=null){
                    String linkNamespace = null;
                    if(current.namespaceAttribute!=null)
                        linkNamespace = atts.getValue(current.namespaceAttribute.getNamespaceURI(), current.namespaceAttribute.getLocalPart());
                    String location = atts.getValue(current.locationAttribute.getNamespaceURI(), current.locationAttribute.getLocalPart());
                    if(location!=null){
                        location = resolveLink(linkNamespace, location);
                        URL targetURL = URLUtil.toURL(location);
                        File targetFile = null;
                        if(crawled!=null)
                            targetFile = crawled.get(targetURL);
                        if(targetFile==null && listener.doCrawl(targetURL))
                            targetFile = new XMLCrawler(XMLCrawler.this).crawl(new InputSource(location), listener, null);
                        String href = targetFile==null ? location : FileNavigator.INSTANCE.getRelativePath(file.getParentFile(), targetFile);
                        AttributesImpl newAtts = new AttributesImpl(atts);
                        int index = atts.getIndex(current.locationAttribute.getNamespaceURI(), current.locationAttribute.getLocalPart());
                        newAtts.setValue(index, href);
                        atts = newAtts;
                    }
                }
            }catch(IOException ex){
                throw new SAXException(ex);
            }
        }
    }else
        depth++;

    super.startElement(namespaceURI, localName, qualifiedName, atts);
}
 
Example 9
Source Project: htmlunit   File: HTMLInputElement.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 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
 */
@JsxSetter
public void setType(String newType) {
    HtmlInput input = getDomNodeOrDie();

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

    final BrowserVersion browser = getBrowserVersion();
    if (!currentType.equalsIgnoreCase(newType)) {
        if (newType != null && browser.hasFeature(JS_INPUT_SET_TYPE_LOWERCASE)) {
            newType = newType.toLowerCase(Locale.ROOT);
        }

        if (!browser.hasFeature(HTMLINPUT_TYPE_DATETIME_SUPPORTED)
                && ("week".equals(newType)
                        || "month".equals(newType)
                        || "date".equals(newType)
                        || "datetime-local".equals(newType)
                        || "time".equals(newType))) {
            throw Context.reportRuntimeError("Invalid argument '" + newType + "' for setting property type.");
        }

        if (browser.hasFeature(HTMLINPUT_TYPE_COLOR_NOT_SUPPORTED)
                && "color".equals(newType)) {
            throw Context.reportRuntimeError("Invalid argument '" + newType + "' for setting property type.");
        }

        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 SgmlPage page = input.getPage();
            final HtmlInput newInput = (HtmlInput) page.getWebClient().getPageCreator().getHtmlParser()
                    .getFactory(HtmlInput.TAG_NAME)
                    .createElement(page, HtmlInput.TAG_NAME, 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);
        }
    }
}
 
Example 10
/**
 * 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
 */
@JsxSetter
public void setType(String newType) {
    HtmlInput input = getDomNodeOrDie();

    final String currentType = input.getAttributeDirect("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);
        }
    }
}