Example usage for org.xml.sax Attributes getLength

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

Introduction

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

Prototype

public abstract int getLength();

Source Link

Document

Return the number of attributes in the list.

Usage

From source file:SAXCopy.java

public void startElement(String namespaceURI, String localName, String qName, Attributes atts) {
    out.print("<" + qName);
    if (namespaceBegin) {
        out.print(" xmlns:" + currentNamespace + "=\"" + currentNamespaceUri + "\"");
        namespaceBegin = false;/*from   w w  w .  ja v  a  2 s  . c om*/
    }
    for (int i = 0; i < atts.getLength(); i++) {
        out.print(" " + atts.getQName(i) + "=\\" + atts.getValue(i) + "\"");
    }
    out.print(">");
}

From source file:com.redhat.rhn.common.util.AttributeCopyRule.java

/** {@inheritDoc} */
public void begin(String namespace, String name, Attributes attributes) throws Exception {
    Object param = digester.peek();
    Method me;//  w ww. j  a  v  a 2s.  c o m
    try {
        me = param.getClass().getMethod("put", new Class[] { Object.class, Object.class });
    } catch (NoSuchMethodException e) {
        return;
    }

    for (int i = 0; i < attributes.getLength(); i++) {
        me.invoke(param, attributes.getQName(i), attributes.getValue(i));
    }
}

From source file:FragmentContentHandler.java

@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
    Integer count = elementNameCount.get(qName);
    if (null == count) {
        count = 1;//from  www  . jav a2  s . c  o  m
    } else {
        count++;
    }
    elementNameCount.put(qName, count);
    String childXPath = xPath + "/" + qName + "[" + count + "]";

    int attsLength = atts.getLength();
    for (int x = 0; x < attsLength; x++) {
        System.out.println(childXPath + "[@" + atts.getQName(x) + "='" + atts.getValue(x) + ']');
    }

    FragmentContentHandler child = new FragmentContentHandler(childXPath, xmlReader, this);
    xmlReader.setContentHandler(child);
}

From source file:SAXSample.java

public void startElement(String uri, String localName, String qName, Attributes attributes) {
    if (qName.equalsIgnoreCase("books")) {
        books = new SAXBooks();
    } else if (qName.equalsIgnoreCase("book")) {
        SAXBook book = new SAXBook();
        for (int i = 0; i < attributes.getLength(); i++) {
            if (attributes.getQName(i).equalsIgnoreCase("category")) {
                book.setCategory(attributes.getValue(i));
            }/*from   w  w  w .ja  v a 2 s. c o  m*/
        }
        books.addBook(book);
    } else if (qName.equalsIgnoreCase("author")) {
        this.readingAuthor = true;
    } else if (qName.equalsIgnoreCase("title")) {
        this.readingTitle = true;
    } else if (qName.equalsIgnoreCase("price")) {
        this.readingPrice = true;
    } else {
        System.out.println("Unknown element: " + qName);
    }
}

From source file:com.prowidesoftware.swift.io.parser.MxNodeContentHandler.java

public void startElement(final String uri, final String localName, final String qName,
        final org.xml.sax.Attributes atts) throws org.xml.sax.SAXException {
    if (log.isLoggable(Level.FINEST)) {
        log.finest("uri: " + uri + "\nlocalName: " + localName + "\nqName: " + qName
                + (atts == null ? "" : "\natts(" + atts.getLength() + "): ..."));
    }//from ww w .j  a v a2 s  .c o m
    final MxNode node = new MxNode(currentNode, localName);
    if (atts != null) {
        for (int i = 0; i < atts.getLength(); i++) {
            node.addAttribute(atts.getLocalName(i), atts.getValue(i));
        }
    }
    /*
     * set uri as xmlns attribute for the first node in namespace
     */
    if (uri != null
            && (node.getParent() == null || !StringUtils.equals(node.getParent().getAttribute("xmlns"), uri))) {
        node.addAttribute("xmlns", uri);
    }
    currentNode = node;
}

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 w  w  w  .j  a va  2s. 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:com.threerings.miso.tile.tools.xml.FringeConfigurationParser.java

@Override
protected void addRules(Digester digest) {
    // configure top-level constraints
    String prefix = "fringe";
    digest.addRule(prefix, new SetPropertyFieldsRule());

    // create and configure fringe config instances
    prefix += "/base";
    digest.addObjectCreate(prefix, FringeRecord.class.getName());

    ValidatedSetNextRule.Validator val;
    val = new ValidatedSetNextRule.Validator() {
        public boolean isValid(Object target) {
            if (((FringeRecord) target).isValid()) {
                return true;
            } else {
                log.warning("A FringeRecord was not added because it was " + "improperly specified [rec="
                        + target + "].");
                return false;
            }/*from   ww  w  .j  a  v a2s  .  c  om*/
        }
    };
    ValidatedSetNextRule vrule;
    vrule = new ValidatedSetNextRule("addFringeRecord", val) {
        // parse the fringe record, converting tileset names to
        // tileset ids
        @Override
        public void begin(String namespace, String lname, Attributes attrs) throws Exception {
            FringeRecord frec = (FringeRecord) digester.peek();

            for (int ii = 0; ii < attrs.getLength(); ii++) {
                String name = attrs.getLocalName(ii);
                if (StringUtil.isBlank(name)) {
                    name = attrs.getQName(ii);
                }
                String value = attrs.getValue(ii);

                if ("name".equals(name)) {
                    if (_idBroker.tileSetMapped(value)) {
                        frec.base_tsid = _idBroker.getTileSetID(value);
                    } else {
                        log.warning("Skipping unknown base " + "tileset [name=" + value + "].");
                    }

                } else if ("priority".equals(name)) {
                    frec.priority = Integer.parseInt(value);
                } else {
                    log.warning("Skipping unknown attribute " + "[name=" + name + "].");
                }
            }
        }
    };
    digest.addRule(prefix, vrule);

    // create the tileset records in each fringe record
    prefix += "/tileset";
    digest.addObjectCreate(prefix, FringeTileSetRecord.class.getName());

    val = new ValidatedSetNextRule.Validator() {
        public boolean isValid(Object target) {
            if (((FringeTileSetRecord) target).isValid()) {
                return true;
            } else {
                log.warning("A FringeTileSetRecord was not added because " + "it was improperly specified "
                        + "[rec=" + target + "].");
                return false;
            }
        }
    };
    vrule = new ValidatedSetNextRule("addTileset", val) {
        // parse the fringe tilesetrecord, converting tileset names to ids
        @Override
        public void begin(String namespace, String lname, Attributes attrs) throws Exception {
            FringeTileSetRecord f = (FringeTileSetRecord) digester.peek();

            for (int ii = 0; ii < attrs.getLength(); ii++) {
                String name = attrs.getLocalName(ii);
                if (StringUtil.isBlank(name)) {
                    name = attrs.getQName(ii);
                }
                String value = attrs.getValue(ii);

                if ("name".equals(name)) {
                    if (_idBroker.tileSetMapped(value)) {
                        f.fringe_tsid = _idBroker.getTileSetID(value);
                    } else {
                        log.warning("Skipping unknown fringe " + "tileset [name=" + value + "].");
                    }

                } else if ("mask".equals(name)) {
                    f.mask = Boolean.valueOf(value).booleanValue();
                } else {
                    log.warning("Skipping unknown attribute " + "[name=" + name + "].");
                }
            }
        }
    };
    digest.addRule(prefix, vrule);
}

From source file:jp.gr.java_conf.petit_lycee.subsonico.MethodConstants.java

/***
 * ???????//from ww w  .  j  a va2s  .  c om
 * int,boolean,String,Date??
 * @param obj 
 * @param attrs attributes
 */
private void setValues(Object obj, org.xml.sax.Attributes attrs) {
    for (int i = 0; i < attrs.getLength(); i++) {
        try {
            Field field = getField(obj.getClass(), attrs.getLocalName(i));
            field.setAccessible(true);

            String attr_value = attrs.getValue(i);
            if (field.getType() == boolean.class) {
                field.setBoolean(obj, Boolean.valueOf(attr_value));
            } else if (field.getType() == int.class) {
                field.setInt(obj, Integer.parseInt(attr_value));
            } else if (field.getType() == String.class) {
                field.set(obj, attr_value);
            } else if (field.getType() == Date.class) {
                String format = "yyyy-MM-dd'T'HH:mm:ss";
                field.set(obj, StringUtil.toDate(attr_value, format));
            }
        } catch (Exception ignored) {
        }
    }
}

From source file:MyContentHandler.java

public void startElement(String namespaceURI, String localName, String qName, Attributes atts) {
    System.out.println("-" + locator.getLineNumber() + "---Opening tag of an element");
    System.out.println("       Namespace: " + namespaceURI);
    System.out.println("      Local name: " + localName);
    System.out.println("  Qualified name: " + qName);
    for (int i = 0; i < atts.getLength(); i++) {
        System.out.println("       Attribute: " + atts.getQName(i) + "=\"" + atts.getValue(i) + "\"");
    }/*from  ww w  .j  a  v a  2  s .  c om*/
}

From source file:com.icesoft.faces.webapp.parser.ELSetPropertiesRule.java

public void begin(Attributes attributes) throws Exception {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    HashMap values = new HashMap();
    Object top = digester.peek();

    for (int i = 0; i < attributes.getLength(); i++) {
        String name = attributes.getLocalName(i);
        if ("".equals(name)) {
            name = attributes.getQName(i);
        }/*from  ww  w  .  j ava2 s .c  o  m*/
        String value = attributes.getValue(i);

        //take a guess at the types of the JSF 1.2 tag members
        //we are probably better off doing this reflectively
        if (name != null) {
            values.put(name, value);
            if (("id".equals(name)) || ("name".equals(name)) || ("var".equals(name))) {
                values.put(name, value);
            } else if (top instanceof UIComponentTag) {
                //must be a JSF 1.1 tag
                values.put(name, value);
            } else if ("action".equals(name)) {
                values.put(name, getMethodExpression(facesContext, name, value, null));
            } else if ("validator".equals(name)) {
                values.put(name, getMethodExpression(facesContext, name, value, null));
            } else if ("actionListener".equals(name)) {
                values.put(name, getMethodExpression(facesContext, name, value, ActionEvent.class));
            } else if ("valueChangeListener".equals(name)) {
                values.put(name, getMethodExpression(facesContext, name, value, ValueChangeEvent.class));
            } else {
                values.put(name, getValueExpression(facesContext, name, value));
            }
            if (top instanceof javax.faces.webapp.UIComponentELTag) {
                //special case for 
                //com.sun.faces.taglib.jsf_core.ParameterTag
                //and potentially others
                if ("name".equals(name)) {
                    values.put(name, getValueExpression(facesContext, name, value));
                } else if ("locale".equals(name)) {
                    values.put(name, getValueExpression(facesContext, name, value));
                }
            } else {
                //reflection based code as mentioned above.  More likely
                //to be correct, but performance may not be as good,
                //so only applying it in a specific case
                if ("name".equals(name)) {
                    Method setNameMethod = null;
                    try {
                        setNameMethod = top.getClass().getMethod("setName",
                                new Class[] { ValueExpression.class });
                    } catch (Exception e) {
                    }
                    if (null != setNameMethod) {
                        values.put(name, getValueExpression(facesContext, name, value));
                    }
                }

            }

        }
    }

    BeanUtils.populate(top, values);
}