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:com.granule.json.utils.internal.JSONSAXHandler.java

/**
 * This function parses an IFix top level element and all its children.
 *///w  w  w.  jav  a2s. c  o  m
public void startElement(String namespaceURI, String localName, String qName, Attributes attrs)
        throws SAXException {
    if (logger.isLoggable(Level.FINER))
        logger.exiting(className, "startElement(String,String,String,org.xml.sax.Attributes)");

    Properties props = new Properties();
    int attrLength = attrs.getLength();
    for (int i = 0; i < attrLength; i++) {
        props.put(attrs.getQName(i), attrs.getValue(i));
    }

    JSONObject obj = new JSONObject(localName, props);
    if (this.head == null) {
        this.head = obj;
        this.current = head;
    } else {
        if (current != null) {
            this.previousObjects.push(current);
            this.current.addJSONObject(obj);
        }
        this.current = obj;
    }

    if (logger.isLoggable(Level.FINER))
        logger.exiting(className, "startElement(String,String,String,org.xml.sax.Attributes)");
}

From source file:com.ibm.jaql.lang.expr.xml.TypedXmlToJsonFn.java

/**
 * Create a JSON record for new node. The node attributes are processed.
 *//*ww  w.  jav  a 2s . c om*/
@Override
public void startElement(String uri, String localName, String name, Attributes attrs) throws SAXException {
    BufferedJsonRecord node = new BufferedJsonRecord();
    stack.push(node);
    lengths.add(text.length());
    int n = attrs.getLength();
    BufferedJsonRecord container;
    for (int i = 0; i < n; i++) {
        uri = attrs.getURI(i);

        // If attribute has a uri
        if (uri != null && uri.length() > 0) {
            JsonString jUri = new JsonString(uri);
            BufferedJsonRecord ur = (BufferedJsonRecord) node.get(jUri);
            if (ur == null) {
                ur = new BufferedJsonRecord();
                node.add(jUri, ur);
            }
            container = ur;
        } else {
            container = node;
        }

        String attrName = "@" + attrs.getLocalName(i);
        JsonString jName = new JsonString(attrName);
        String v = attrs.getValue(i);
        if (container.containsKey(jName)) {
            throw new RuntimeException("duplicate attribute name: " + attrName);
        }
        container.add(jName, cast(v));
    }
}

From source file:net.sourceforge.msscodefactory.cfbamcustom.v2_7.CFBamXmlLoader.CFBamXmlLoaderTableAddendumHandler.java

public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
    final String S_ProcName = "startElement";

    // TableRelations attributes
    String attrFromTable = null;//ww  w  .j  a v a 2  s . c o  m

    // Attribute Extraction
    String attrLocalName;
    int numAttrs;
    int idxAttr;

    assert qName.equals("TableAddendum");

    numAttrs = attrs.getLength();
    for (idxAttr = 0; idxAttr < numAttrs; idxAttr++) {
        attrLocalName = attrs.getLocalName(idxAttr);
        if (attrLocalName.equals("FromTable")) {
            if (attrFromTable != null) {
                throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                        S_ProcName, attrLocalName);
            }
            attrFromTable = attrs.getValue(idxAttr);
        } else if (attrLocalName.equals("schemaLocation")) {
            // ignored
        } else {
            throw CFLib.getDefaultExceptionFactory().newUnrecognizedAttributeException(getClass(), S_ProcName,
                    "LocalName", attrLocalName);
        }
    }

    if ((attrFromTable == null) || (attrFromTable.length() <= 0)) {
        throw CFLib.getDefaultExceptionFactory().newEmptyArgumentException(getClass(), S_ProcName, 0,
                "FromTable");
    }

    CFBamXmlLoader saxLoader = (CFBamXmlLoader) getParser();
    if (saxLoader == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                "getParser()");
    }

    CFLibXmlCoreContext curContext = saxLoader.getCurContext();
    if (curContext == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                "getParser().getCurContext()");
    }

    ICFBamSchemaObj schemaObj = saxLoader.getSchemaObj();
    if (schemaObj == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                "getParser().getSchemaObj()");
    }

    if (!CFBamXmlLoader.getProcessSchema(curContext)) {
        return;
    }

    CFLibXmlCoreContext prevContext = curContext.getPrevContext();
    ICFBamSchemaDefObj schemaDef = (ICFBamSchemaDefObj) (prevContext.getNamedValue("Object"));
    ICFBamTableObj fromTable = schemaObj.getTableTableObj().readTableByUNameIdx(schemaDef.getRequiredTenantId(),
            schemaDef.getRequiredId(), attrFromTable);
    if (fromTable == null) {
        throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                " Could not resolve FromTable \"" + attrFromTable + "\" in Schema \""
                        + schemaDef.getObjFullName() + "\"");
    }
    curContext.putNamedValue("FromTable", fromTable);

    // No scope to add here, just resolving a table context
    curContext.putNamedValue("Object", fromTable);
}

From source file:TestModelBuilder.java

public void startElement(String namespace, String localname, String qname, Attributes atts)
        throws SAXException {
    SimpleElement element = null;//from   w w w  . j  a  v a  2s. c  o m
    try {
        element = (SimpleElement) Class.forName(qname).newInstance();
    } catch (Exception e) {
    }
    if (element == null)
        element = new SimpleElement();
    for (int i = 0; i < atts.getLength(); i++)
        element.setAttributeValue(atts.getQName(i), atts.getValue(i));
    stack.push(element);
}

From source file:com.ibm.jaql.lang.expr.xml.XmlToJsonFn.java

/**
 * Create a JSON record for new node. The node attributes are processed.
 *//*from   w w  w .j a va 2 s .c o  m*/
@Override
public void startElement(String uri, String localName, String name, Attributes attrs) throws SAXException {
    BufferedJsonRecord node = new BufferedJsonRecord();
    stack.push(node);
    lengths.add(text.length());
    int n = attrs.getLength();
    BufferedJsonRecord container;
    for (int i = 0; i < n; i++) {
        uri = attrs.getURI(i);

        // If attribute has a uri
        if (uri != null && uri.length() > 0) {
            JsonString jUri = new JsonString(uri);
            BufferedJsonRecord ur = (BufferedJsonRecord) node.get(jUri);
            if (ur == null) {
                ur = new BufferedJsonRecord();
                node.add(jUri, ur);
            }
            container = ur;
        } else {
            container = node;
        }

        String attrName = "@" + attrs.getLocalName(i);
        JsonString jName = new JsonString(attrName);
        String v = attrs.getValue(i);
        if (container.containsKey(jName)) {
            throw new RuntimeException("duplicate attribute name: " + attrName);
        }
        container.add(jName, new JsonString(v));
    }
}

From source file:com.miragedev.mononara.core.io.KanjiHandlerImpl.java

public void startElement(String uri, String localName, String qName, Attributes attributes)
        throws SAXException {
    super.startElement(uri, localName, qName, attributes); //To change body of overridden methods use File | Settings | File Templates.
    //System.out.println("Parsing: uri("+uri+") localName("+localName+") qName("+qName+")");
    if (qName.equalsIgnoreCase("kanjis")) {
        if (attributes.getLength() > 0) {
            numberOfEntry = Integer.parseInt(attributes.getValue("size"));
        }//  www . j a  v a  2s  .c  o  m
    } else if (qName.equalsIgnoreCase("kanji")) {
        parentId = Integer.parseInt(attributes.getValue("id"));
        Kanji kanji = kanjiDao.findById(parentId);
        isNew = false;
        currentTagList = new Vector<String>();
        if (kanji == null) {
            kanji = new Kanji();
            kanji.setId(parentId);
            kanjiDao.save(kanji);
            isNew = true;
        }
    } else if (qName.equalsIgnoreCase("name")) {
        inName = true;
        inTag = false;
    } else if (qName.equalsIgnoreCase("tag")) {
        inTag = true;
        inName = false;
    }
}

From source file:de.l3s.boilerpipe.sax.BoilerpipeHTMLContentHandler.java

private List<Pair<String, String>> convertAttributes(Attributes atts) {
    List<Pair<String, String>> attrs = new ArrayList<>();

    for (int i = 0; i < atts.getLength(); i++) {
        String qName = atts.getQName(i);
        attrs.add(Pair.of(qName, atts.getValue(qName)));
    }/* w w w  .  j ava 2s  .  co m*/
    return attrs;
}

From source file:com.adobe.acs.commons.rewriter.impl.StaticReferenceRewriteTransformerFactory.java

private Attributes rebuildAttributes(final String elementName, final Attributes attrs) {
    if (attributes.containsKey(elementName)) {
        final String[] modifyableAttributes = attributes.get(elementName);

        // first - check for the nostatic class
        boolean rewriteStatic = true;
        for (int i = 0; i < attrs.getLength(); i++) {
            final String attrName = attrs.getLocalName(i);
            if (ATTR_CLASS.equals(attrName)) {
                String attrValue = attrs.getValue(i);
                if (attrValue.contains(CLASS_NOSTATIC)) {
                    rewriteStatic = false;
                }/*from   w  ww.  j  av a2 s .  c o  m*/
            }
        }

        if (rewriteStatic) {
            return rebuildAttributes(elementName, attrs, modifyableAttributes);
        }
    }

    return attrs;
}

From source file:com.miragedev.mononara.core.io.DictionnaryHandlerImpl.java

public void startElement(String uri, String localName, String qName, Attributes attributes)
        throws SAXException {
    super.startElement(uri, localName, qName, attributes); //To change body of overridden methods use File | Settings | File Templates.
    //System.out.println("Parsing: uri("+uri+") localName("+localName+") qName("+qName+")");
    if (qName.equalsIgnoreCase("dictionnary")) {
        if (attributes.getLength() > 0) {
            numberOfEntry = Integer.parseInt(attributes.getValue("size"));
        }/* www.  j  ava 2 s .c o  m*/
    } else if (qName.equalsIgnoreCase("entry")) {
        parentId = Integer.parseInt(attributes.getValue("id"));
        DictionaryEntry dictionnaryEntry = dictionaryEntryDao.findById(parentId);
        if (dictionnaryEntry != null) {
            dictionaryEntryDao.delete(dictionnaryEntry);
        }
        //isNew = false;
        //if (dictionnaryEntry == null) {
        dictionnaryEntry = new DictionaryEntry();
        dictionnaryEntry.setId(parentId);
        dictionaryEntryDao.save(dictionnaryEntry);
        isNew = true;
        //}

    } else if (qName.equalsIgnoreCase("kanji")) {
        inKanji = true;
        inKana = false;
        inRomaji = false;
        inDescription = false;
        inTag = false;
    } else if (qName.equalsIgnoreCase("kana")) {
        inKanji = false;
        inKana = true;
        inRomaji = false;
        inDescription = false;
        inTag = false;
    } else if (qName.equalsIgnoreCase("romaji")) {
        inKanji = false;
        inKana = false;
        inRomaji = true;
        inDescription = false;
        inTag = false;
    } else if (qName.equalsIgnoreCase("description")) {
        inKanji = false;
        inKana = false;
        inRomaji = false;
        inDescription = true;
        inTag = false;
    } else if (qName.equalsIgnoreCase("tag")) {
        inKanji = false;
        inKana = false;
        inRomaji = false;
        inDescription = false;
        inTag = true;
    }
}

From source file:edu.ku.brc.specify.toycode.mexconabio.FMPCreateTable.java

public void startElement(String namespaceURI, String localName, String qName, Attributes attrs) {
    buffer.setLength(0);//  ww w  .j a  va2  s.  c o  m

    if (localName.equals("FIELD")) {
        FieldDef fldDef = new FieldDef();
        fields.add(fldDef);

        for (int i = 0; i < attrs.getLength(); i++) {
            String attr = attrs.getLocalName(i);
            String value = attrs.getValue(i);
            if (attr.equals("EMPTYOK")) {
                fldDef.setNullable(value.equals("YES"));

            } else if (attr.equals("NAME")) {
                value = StringUtils.capitalize(value.trim());
                value = StringUtils.deleteWhitespace(value);
                value = StringUtils.replace(value, "_", "");

                if ((value.charAt(0) >= '0' && value.charAt(0) <= '9') || value.equalsIgnoreCase("New")
                        || value.equalsIgnoreCase("Group")) {
                    value = "Fld" + value;
                }

                String fixedStr = convertFromTwoByteUTF8(value);
                fixedStr = StringUtils.replace(fixedStr, ".", "");
                fixedStr = StringUtils.replace(fixedStr, ":", "");
                fixedStr = StringUtils.replace(fixedStr, "/", "_");
                fldDef.setName(fixedStr);
                fldDef.setOrigName(value);

            } else if (attr.equals("TYPE")) {
                if (value.equals("TEXT")) {
                    fldDef.setType(DataType.eText);

                } else if (value.equals("NUMBER")) {
                    fldDef.setType(DataType.eNumber);

                } else if (value.equals("DATE")) {
                    fldDef.setType(DataType.eDate);

                } else if (value.equals("TIME")) {
                    fldDef.setType(DataType.eTime);
                } else {
                    System.err.println("Unknown Type[" + value + "]");
                }
            }
        }
    }
}