Example usage for org.w3c.dom NamedNodeMap getNamedItem

List of usage examples for org.w3c.dom NamedNodeMap getNamedItem

Introduction

In this page you can find the example usage for org.w3c.dom NamedNodeMap getNamedItem.

Prototype

public Node getNamedItem(String name);

Source Link

Document

Retrieves a node specified by name.

Usage

From source file:org.ecoinformatics.datamanager.parser.eml.Eml200Parser.java

/**
 * Pulls the entity information out of the XML and stores it in a hash table.
 *//* www  .  j  a  va2  s  .  co m*/
private void processEntities(CachedXPathAPI xpathapi, NodeList entitiesNodeList, String xpath, String packageId)
        throws SAXException, javax.xml.transform.TransformerException, Exception {
    // Make sure that entities is not null
    if (entitiesNodeList == null) {
        return;
    }

    int entityNodeListLength = entitiesNodeList.getLength();
    numEntities = numEntities + entityNodeListLength;
    String entityName = "";
    String entityDescription = "";
    String entityOrientation = "";
    String entityCaseSensitive = "";
    String entityNumberOfRecords = "-1";
    String onlineUrl = "";
    String numHeaderLines = "0";
    int numFooterLines = 0;
    String fieldDelimiter = null;
    String recordDelimiter = "";
    String compressionMethod = "";
    String encodingMethod = "";
    String quoteCharacter = null;
    String literalCharacter = null;
    boolean isImageEntity = false;
    boolean isGZipDataFile = false;
    boolean isZipDataFile = false;
    boolean isTarDataFile = false;
    boolean isSimpleDelimited = true;
    boolean isCollapseDelimiters = false;
    TextComplexDataFormat[] formatArray = null;

    for (int i = 0; i < entityNodeListLength; i++) {

        if (xpath != null && (xpath.equals(SPATIALRASTERENTITY) || xpath.equals(SPATIALVECTORENTITY))) {
            isImageEntity = true;
        }

        //go through the entities and put the information into the hash.
        elementId++;
        Node entityNode = entitiesNodeList.item(i);
        String id = null;
        NamedNodeMap entityNodeAttributes = entityNode.getAttributes();

        if (entityNodeAttributes != null) {
            Node idNode = entityNodeAttributes.getNamedItem(ID);

            if (idNode != null) {
                id = idNode.getNodeValue();
            }
        }

        NodeList entityNodeChildren = entityNode.getChildNodes();

        for (int j = 0; j < entityNodeChildren.getLength(); j++) {
            Node childNode = entityNodeChildren.item(j);
            String childName = childNode.getNodeName();

            if (childName.equals("entityName")) {
                entityName = childNode.getFirstChild().getNodeValue();
            } else if (childName.equals("entityDescription")) {
                entityDescription = childNode.getFirstChild().getNodeValue();
            } else if (childName.equals("caseSensitive")) {
                entityCaseSensitive = childNode.getFirstChild().getNodeValue();
            } else if (childName.equals("numberOfRecords")) {
                entityNumberOfRecords = childNode.getFirstChild().getNodeValue();
                /*numRecords = (new Integer(entityNumberOfRecords))
                            .intValue();*/
            }

        }

        NodeList attributeOrientationNodeList = xpathapi.selectNodeList(entityNode,
                "physical/dataFormat/textFormat/attributeOrientation");

        if (attributeOrientationNodeList != null && attributeOrientationNodeList.getLength() > 0) {
            entityOrientation = attributeOrientationNodeList.item(0).getFirstChild().getNodeValue();

        }

        NodeList numHeaderLinesNodeList = xpathapi.selectNodeList(entityNode,
                "physical/dataFormat/textFormat/numHeaderLines");

        if ((numHeaderLinesNodeList != null) && (numHeaderLinesNodeList.getLength() > 0)) {
            Node numHeaderLinesNode = numHeaderLinesNodeList.item(0);

            if (numHeaderLinesNode != null) {
                numHeaderLines = numHeaderLinesNode.getFirstChild().getNodeValue();
            }
        }

        NodeList numFooterLinesNodeList = xpathapi.selectNodeList(entityNode,
                "physical/dataFormat/textFormat/numFooterLines");

        if ((numFooterLinesNodeList != null) && (numFooterLinesNodeList.getLength() > 0)) {
            Node numFooterLinesNode = numFooterLinesNodeList.item(0);

            if (numFooterLinesNode != null) {
                String numFooterLinesStr = numFooterLinesNode.getFirstChild().getNodeValue();
                numFooterLines = (new Integer(numFooterLinesStr.trim())).intValue();
            }
        }

        // Here is the simple delimited data file
        NodeList fieldDelimiterNodeList = xpathapi.selectNodeList(entityNode,
                "physical/dataFormat/textFormat/simpleDelimited/fieldDelimiter");

        if (fieldDelimiterNodeList != null && fieldDelimiterNodeList.getLength() > 0) {
            fieldDelimiter = fieldDelimiterNodeList.item(0).getFirstChild().getNodeValue();
        }

        NodeList collapseDelimitersNodeList = xpathapi.selectNodeList(entityNode,
                "physical/dataFormat/textFormat/simpleDelimited/collapseDelimiters");

        if (collapseDelimitersNodeList != null && collapseDelimitersNodeList.getLength() > 0) {

            String collapseDelimiters = collapseDelimitersNodeList.item(0).getFirstChild().getNodeValue();

            if (collapseDelimiters.equalsIgnoreCase("yes")) {
                isCollapseDelimiters = true;
            }
        }

        NodeList quoteCharacterNodeList = xpathapi.selectNodeList(entityNode,
                "physical/dataFormat/textFormat/simpleDelimited/quoteCharacter");

        if (quoteCharacterNodeList != null && quoteCharacterNodeList.getLength() > 0) {
            quoteCharacter = quoteCharacterNodeList.item(0).getFirstChild().getNodeValue();
        }

        NodeList literalCharacterNodeList = xpathapi.selectNodeList(entityNode,
                "physical/dataFormat/textFormat/simpleDelimited/literalCharacter");

        if (literalCharacterNodeList != null && literalCharacterNodeList.getLength() > 0) {
            literalCharacter = literalCharacterNodeList.item(0).getFirstChild().getNodeValue();
        }

        // For complex format data file
        NodeList complexNodeList = xpathapi.selectNodeList(entityNode,
                "physical/dataFormat/textFormat/complex");

        if (complexNodeList != null && complexNodeList.getLength() > 0) {
            //log.debug("in handle complex text data format");
            isSimpleDelimited = false;
            Node complexNode = complexNodeList.item(0);
            NodeList complexChildNodes = complexNode.getChildNodes();
            int complexChildNodesLength = complexChildNodes.getLength();
            Vector formatVector = new Vector();

            for (int k = 0; k < complexChildNodesLength; k++) {
                Node complexChildNode = complexChildNodes.item(k);

                if (complexChildNode != null && complexChildNode.getNodeName().equals("textFixed")) {
                    TextWidthFixedDataFormat textWidthFixedDataFormat = handleTextFixedDataFormatNode(
                            complexChildNode);

                    if (textWidthFixedDataFormat != null) {
                        formatVector.add(textWidthFixedDataFormat);
                        //complexFormatsNumber++;
                    }
                } else if (complexChildNode != null && complexChildNode.getNodeName().equals("textDelimited")) {
                    TextDelimitedDataFormat textDelimitedDataFormat = handleComplexDelimitedDataFormatNode(
                            complexChildNode);

                    if (textDelimitedDataFormat != null) {
                        formatVector.add(textDelimitedDataFormat);
                        //complexFormatsNumber++;
                    }
                }
            }

            // Transfer vector to array
            numberOfComplexFormats = formatVector.size();
            formatArray = new TextComplexDataFormat[numberOfComplexFormats];
            for (int j = 0; j < numberOfComplexFormats; j++) {
                formatArray[j] = (TextComplexDataFormat) formatVector.elementAt(j);
            }
        }

        NodeList recordDelimiterNodeList = xpathapi.selectNodeList(entityNode,
                "physical/dataFormat/textFormat/recordDelimiter");

        if ((recordDelimiterNodeList != null) && (recordDelimiterNodeList.getLength() > 0)) {
            recordDelimiter = recordDelimiterNodeList.item(0).getFirstChild().getNodeValue();
        } else {
            recordDelimiter = "\\r\\n";
        }

        // Get the distribution information
        NodeList urlNodeList = xpathapi.selectNodeList(entityNode, "physical/distribution/online/url");

        if (urlNodeList != null && urlNodeList.getLength() > 0) {
            onlineUrl = urlNodeList.item(0).getFirstChild().getNodeValue();

            if (isDebugging) {
                //log.debug("The url is "+ onlineUrl);
            }
        }

        // Get the compressionMethod information
        NodeList compressionMethodNodeList = xpathapi.selectNodeList(entityNode, "physical/compressionMethod");

        if (compressionMethodNodeList != null && compressionMethodNodeList.getLength() > 0) {
            compressionMethod = compressionMethodNodeList.item(0).getFirstChild().getNodeValue();

            if (isDebugging) {
                //log.debug("Compression method is "+compressionMethod);
            }

            if (compressionMethod != null && compressionMethod.equals(Entity.GZIP)) {
                isGZipDataFile = true;
            } else if (compressionMethod != null && compressionMethod.equals(Entity.ZIP)) {
                isZipDataFile = true;
            }
        }

        // Get encoding method info (mainly for tar file)
        NodeList encodingMethodNodeList = xpathapi.selectNodeList(entityNode, "physical/encodingMethod");

        if (encodingMethodNodeList != null && encodingMethodNodeList.getLength() > 0) {
            encodingMethod = encodingMethodNodeList.item(0).getFirstChild().getNodeValue();

            if (isDebugging) {
                //log.debug("encoding method is "+encodingMethod);
            }

            if (encodingMethod != null && encodingMethod.equals(Entity.TAR)) {
                isTarDataFile = true;
            }
        }

        if (entityOrientation.trim().equals("column")) {
            entityOrientation = Entity.COLUMNMAJOR;
        } else {
            entityOrientation = Entity.ROWMAJOR;
        }

        if (entityCaseSensitive.equals("yes")) {
            entityCaseSensitive = "true";
        } else {
            entityCaseSensitive = "false";
        }

        entityObject = new Entity(id, entityName.trim(), entityDescription.trim(),
                new Boolean(entityCaseSensitive), entityOrientation,
                new Integer(entityNumberOfRecords).intValue());

        entityObject.setNumHeaderLines((new Integer(numHeaderLines)).intValue());
        entityObject.setNumFooterLines(numFooterLines);
        entityObject.setSimpleDelimited(isSimpleDelimited);

        // For simple delimited data file
        if (fieldDelimiter != null) {
            entityObject.setDelimiter(fieldDelimiter);
        }

        if (quoteCharacter != null) {
            entityObject.setQuoteCharacter(quoteCharacter);
        }

        if (literalCharacter != null) {
            entityObject.setLiteralCharacter(literalCharacter);
        }

        entityObject.setCollapseDelimiters(isCollapseDelimiters);
        entityObject.setRecordDelimiter(recordDelimiter);
        entityObject.setURL(onlineUrl);
        entityObject.setCompressionMethod(compressionMethod);
        entityObject.setIsImageEntity(isImageEntity);
        entityObject.setHasGZipDataFile(isGZipDataFile);
        entityObject.setHasZipDataFile(isZipDataFile);
        entityObject.setHasTarDataFile(isTarDataFile);
        entityObject.setPackageId(packageId);

        try {
            NodeList attributeListNodeList = xpathapi.selectNodeList(entityNode, "attributeList");
            processAttributeList(xpathapi, attributeListNodeList, entityObject);
            entityObject.setDataFormatArray(formatArray);
        } catch (Exception e) {
            throw new Exception("Error parsing attributes: " + e.getMessage());
        }

        //entityHash.put(Integer.toString(elementId), entityObject);
        emlDataPackage.add(entityObject);
        //fileHash.put(elementId, onlineUrl); 
    } // end for loop

}

From source file:org.ecoinformatics.datamanager.parser.generic.GenericDataPackageParser.java

/**
 * Processes the attributeList element.//from   ww  w.  j av a 2s .c  o  m
 * 
 * @param  xpathapi  XPath API
 * @param  attributeListNodeList   a NodeList
 * @param  xpath     the XPath path string to the data entity 
 * @param  entObj    the entity object whose attribute list is processed
 */
private void processAttributeList(CachedXPathAPI xpathapi, NodeList attributeListNodeList, String xpath,
        Entity entObj) throws Exception {
    AttributeList attributeList = new AttributeList();
    Node attributeListNode = attributeListNodeList.item(0);

    /*
     * It is allowable in EML to omit the attributeList for an
     * 'otherEntity' data entity.
     */
    if (attributeListNode == null) {
        if (xpath != null && xpath.equals(otherEntityPath)) {
            System.err.println("No attributeList was specified for otherEntity '" + entObj.getName()
                    + "'. This is allowable in EML.");
            return;
        } else {
            throw new Exception("No attributeList was specified for entity '" + entObj.getName() + "'.");
        }
    }

    // Get attributeList element's id attribute
    NamedNodeMap attributeListNodeAttributes = attributeListNode.getAttributes();
    String idString = null;

    if (attributeListNodeAttributes != null) {
        Node idNode = attributeListNodeAttributes.getNamedItem(ID);

        if (idNode != null) {
            idString = idNode.getNodeValue();
            attributeList.setId(idString);

            if (isDebugging) {
                //log.debug("The id value for the attributelist is " + idString);
            }
        }
    }

    NodeList attributeNodeList = xpathapi.selectNodeList(attributeListNode, "attribute");
    NodeList referencesNodeList = xpathapi.selectNodeList(attributeListNode, "references");

    if (attributeNodeList != null && attributeNodeList.getLength() > 0) {
        processAttributes(xpathapi, attributeNodeList, attributeList);

        if (idString != null) {
            attributeListIdHash.put(idString, attributeList);
        }
    } else if (referencesNodeList != null && referencesNodeList.getLength() > 0) {
        // get the references id 
        Node referencesNode = referencesNodeList.item(0);

        if (isDebugging) {
            //log.debug("The reference node's name is "+
            //          referenceNode.getNodeName());
        }

        String referencesId = referencesNode.getFirstChild().getNodeValue();

        if (isDebugging) {
            //log.debug("the reference id is "+ referenceId);
        }

        attributeList = (AttributeList) attributeListIdHash.get(referencesId);
    } else {
        //log.debug(
        //    "The children name of attribute list couldn't be understood");
        throw new Exception(" couldn't be a child of attributeList");
    }

    if (!entityObject.isSimpleDelimited()) {
        int numberOfAttributes = attributeList.getAttributes().length;

        if (numberOfAttributes != numberOfComplexFormats
                || ((numberOfAttributes == numberOfComplexFormats) && (numberOfComplexFormats == 0))) {
            throw new Exception("Complex format elements should have " + "same number as attribute number");
        } else {
            //entityObject.setDataFormatArray(formatArray);
        }
    }

    entityObject.setAttributeList(attributeList);
}

From source file:org.ecoinformatics.datamanager.parser.generic.GenericDataPackageParser.java

/**
 * Processes the attributes in an attribute list. Called by
 * processAttributeList()./*  ww w  .j  av  a2 s  .c  o m*/
 * 
 * @param  xpathapi           the XPath API
 * @param  attributesNodeList a node list
 * @param  attributeList      an AttributeList object
 */
private void processAttributes(CachedXPathAPI xpathapi, NodeList attributesNodeList,
        AttributeList attributeList) throws Exception {
    int attributesNodeListLength = attributesNodeList.getLength();

    // Process each attribute
    for (int i = 0; i < attributesNodeListLength; i++) {
        Node attributeNode = attributesNodeList.item(i);
        NodeList attributeNodeChildren = attributeNode.getChildNodes();
        //NamedNodeMap attAttributes = att.getAttributes();

        String attName = "";
        String attLabel = "";
        String attDefinition = "";
        String attUnit = "";
        String attUnitType = "";
        String attMeasurementScale = "";
        String attPrecision = "";
        Domain domain = null;
        String id = null;
        Vector missingValueCodeVector = new Vector();
        double numberPrecision = 0;
        ArrayList<StorageType> storageTypeArray = new ArrayList<StorageType>();

        // get attribute id
        NamedNodeMap attributeNodeAttributesMap = attributeNode.getAttributes();

        if (attributeNodeAttributesMap != null) {
            Node idNode = attributeNodeAttributesMap.getNamedItem(ID);

            if (idNode != null) {
                id = idNode.getNodeValue();
            }
        }

        elementId++;

        for (int j = 0; j < attributeNodeChildren.getLength(); j++) {
            Node childNode = attributeNodeChildren.item(j);
            String childNodeName = childNode.getNodeName();
            String childNodeValue = childNode.getFirstChild() == null ? null
                    : childNode.getFirstChild().getNodeValue();
            childNodeValue = childNodeValue == null ? childNodeValue : childNodeValue.trim();
            if (childNodeName.equals("attributeName")) {
                if (childNodeValue != null) {
                    attName = childNodeValue.replace('.', '_');
                }
            } else if (childNodeName.equals("attributeLabel")) {
                attLabel = childNodeValue;
            } else if (childNodeName.equals("attributeDefinition")) {
                attDefinition = childNodeValue;
            }
            // Process storageType elements
            else if (childNodeName.equals("storageType")) {
                String storageTypeTextValue = childNodeValue;
                NamedNodeMap storageTypeAttributesMap = childNode.getAttributes();
                StorageType storageType;
                String typeSystem = "";
                Node typeSystemNode = null;

                // Determine whether the typeSystem attribute was specified
                if (storageTypeAttributesMap != null) {
                    typeSystemNode = storageTypeAttributesMap.getNamedItem(typeSystem);

                    if (typeSystemNode != null) {
                        typeSystem = typeSystemNode.getNodeValue();
                    }
                }

                // Use the appropriate StorageType constructor depending on 
                // whether the 'typeSystem' attribute was specified
                if (!typeSystem.equals("")) {
                    storageType = new StorageType(storageTypeTextValue, typeSystem);
                } else {
                    storageType = new StorageType(storageTypeTextValue);
                }

                storageTypeArray.add(storageType);
            } else if (childNodeName.equals("measurementScale")) {
                //unit is tricky because it can be custom or standard
                //Vector info = new Vector();
                //int domainType = Domain.DOM_NONE;
                NodeList measurementScaleChildNodes = childNode.getChildNodes();

                for (int k = 0; k < measurementScaleChildNodes.getLength(); k++) {
                    Node measurementScaleChildNode = measurementScaleChildNodes.item(k);
                    String measurementScaleChildNodeName = measurementScaleChildNode.getNodeName();

                    if (measurementScaleChildNodeName.equals("interval")
                            || measurementScaleChildNodeName.equals("ratio")) {
                        String numberType = null;
                        String min = "", max = "";
                        Node standardUnitNode = xpathapi.selectSingleNode(measurementScaleChildNode,
                                "unit/standardUnit");
                        Node customUnitNode = xpathapi.selectSingleNode(measurementScaleChildNode,
                                "unit/customUnit");

                        if (standardUnitNode != null) {
                            attUnit = standardUnitNode.getFirstChild().getNodeValue();
                            attUnitType = Attribute.STANDARDUNIT;
                        } else if (customUnitNode != null) {
                            attUnit = customUnitNode.getFirstChild().getNodeValue();
                            attUnitType = Attribute.CUSTOMUNIT;
                        } else {
                            System.err.println("Unable to determine attribute unit.");
                        }

                        Node precisionNode = xpathapi.selectSingleNode(measurementScaleChildNode, "precision");

                        if (precisionNode != null) {
                            // precision is optional in EML201 so if it is
                            // not provided, the attPrecision will be the
                            // empty string
                            attPrecision = precisionNode.getFirstChild().getNodeValue();
                            numberPrecision = (new Double(attPrecision)).doubleValue();

                        }

                        Node numericDomainNode = xpathapi.selectSingleNode(measurementScaleChildNode,
                                "numericDomain");
                        NodeList numericDomainChildNodes = numericDomainNode.getChildNodes();

                        for (int index = 0; index < numericDomainChildNodes.getLength(); index++) {
                            String numericDomainChildNodeName = numericDomainChildNodes.item(index)
                                    .getNodeName();

                            if (numericDomainChildNodeName.equals("numberType")) {
                                // Got number type
                                numberType = numericDomainChildNodes.item(index).getFirstChild().getNodeValue();

                                if (isDebugging) {
                                    //log.debug("The number type is "+ numberType);
                                }
                            } else if (numericDomainChildNodeName.equals("boundsGroup")) {
                                // Got bounds group
                                NodeList boundsNodeList = xpathapi.selectNodeList(numericDomainNode,
                                        "./bounds");

                                for (i = 0; i < boundsNodeList.getLength(); i++) {
                                    NodeList aNodeList;
                                    Node boundsNode;

                                    //String exclMin = null, exclMax = null;
                                    try {
                                        aNodeList = xpathapi.selectNodeList(boundsNodeList.item(i),
                                                "./minimum");
                                        boundsNode = aNodeList.item(0);
                                        min = boundsNode.getFirstChild().getNodeValue();
                                        /*exclMin = bound.getAttributes()
                                            .getNamedItem("exclusive")
                                            .getNodeValue();*/
                                        aNodeList = xpathapi.selectNodeList(boundsNodeList.item(0),
                                                "./maximum");
                                        boundsNode = aNodeList.item(0);
                                        max = boundsNode.getFirstChild().getNodeValue();
                                        /*exclMax = bound.getAttributes()
                                            .getNamedItem("exclusive")
                                            .getNodeValue();*/
                                    } catch (Exception e) {
                                        //log.debug("Error in handle bound ", e);
                                    }
                                }
                            }
                        }

                        Double minNum = null;
                        Double maxNum = null;

                        if (!min.trim().equals("")) {
                            minNum = new Double(min);
                        }

                        if (!max.trim().equals("")) {
                            maxNum = new Double(max);
                        }

                        NumericDomain numericDomain = new NumericDomain(numberType, minNum, maxNum);
                        numericDomain.setPrecision(numberPrecision);
                        domain = numericDomain;

                    } else if (measurementScaleChildNodeName.equals("nominal")
                            || measurementScaleChildNodeName.equals("ordinal")) {
                        NodeList nonNumericDomainChildNodes = xpathapi
                                .selectSingleNode(measurementScaleChildNode, "nonNumericDomain")
                                .getChildNodes();

                        for (int m = 0; m < nonNumericDomainChildNodes.getLength(); m++) {
                            Node nonNumericDomainChildNode = nonNumericDomainChildNodes.item(m);
                            String nonNumericDomainChildNodeName = nonNumericDomainChildNode.getNodeName();

                            if (nonNumericDomainChildNodeName.equals("textDomain")) {
                                TextDomain textDomain = new TextDomain();
                                NodeList definitionNodeList = xpathapi.selectNodeList(nonNumericDomainChildNode,
                                        "./definition");
                                Node defintionNode = definitionNodeList.item(0);
                                String definition = defintionNode.getFirstChild() == null ? null
                                        : defintionNode.getFirstChild().getNodeValue();

                                if (isDebugging) {
                                    //log.debug(
                                    // "The definition value is "+definition);
                                }

                                textDomain.setDefinition(definition);
                                NodeList patternNodeList = xpathapi.selectNodeList(nonNumericDomainChildNode,
                                        "./pattern");

                                String[] patternList = new String[patternNodeList.getLength()];

                                for (int l = 0; l < patternNodeList.getLength(); l++) {
                                    patternList[l] = patternNodeList.item(l).getFirstChild().getNodeValue();
                                }

                                if (patternList.length > 0) {
                                    textDomain.setPattern(patternList);
                                }

                                domain = textDomain;

                            } else if (nonNumericDomainChildNodeName.equals("enumeratedDomain")) {
                                EnumeratedDomain enumeratedDomain = new EnumeratedDomain();
                                Vector info = new Vector();

                                NodeList codeDefinitionNodeList = xpathapi
                                        .selectNodeList(nonNumericDomainChildNode, "./codeDefinition");

                                for (int l = 0; l < codeDefinitionNodeList.getLength(); l++) {
                                    info.add(codeDefinitionNodeList.item(l).getFirstChild().getNodeValue());
                                }

                                enumeratedDomain.setInfo(info);
                                domain = enumeratedDomain;
                            }
                        }
                    } else if (measurementScaleChildNodeName.equalsIgnoreCase("datetime")) {
                        DateTimeDomain date = new DateTimeDomain();
                        String formatString = (xpathapi.selectSingleNode(measurementScaleChildNode,
                                "./formatString")).getFirstChild().getNodeValue();

                        if (isDebugging) {
                            //log.debug(
                            //          "The format string in date time is " 
                            //          + formatString);
                        }
                        date.setFormatString(formatString);
                        domain = date;
                    }
                }
            } else if (childNodeName.equals("missingValueCode")) {
                //log.debug("in missingValueCode");
                NodeList missingValueCodeChildNodes = childNode.getChildNodes();

                for (int k = 0; k < missingValueCodeChildNodes.getLength(); k++) {
                    Node missingValueCodeChildNode = missingValueCodeChildNodes.item(k);
                    String missingValueCodeChildNodeName = missingValueCodeChildNode.getNodeName();

                    if (missingValueCodeChildNodeName.equals("code")) {
                        Node missingValueCodeTextNode = missingValueCodeChildNode.getFirstChild();

                        if (missingValueCodeTextNode != null) {
                            String missingValueCode = missingValueCodeTextNode.getNodeValue();

                            if (isDebugging) {
                                //log.debug("the missing code is "+missingCode);
                            }

                            missingValueCodeVector.add(missingValueCode);
                            //hasMissingValue = true;
                        }
                    }
                }
            }
        }

        /******************************************************
         * need to use domain type to replace data type
         ******************************************************/
        /*String resolvedType = null;
        //DataType dataType = domain.getDataType();
        //resolvedType = dataType.getName();
        if(isDebugging) {
          //log.debug("The final type is " + resolvedType);
        }*/

        Attribute attObj = new Attribute(id, attName, attLabel, attDefinition, attUnit, attUnitType,
                attMeasurementScale, domain);

        // Add storageType elements to the Attribute object 
        // if any were parsed in the EML
        for (StorageType storageType : storageTypeArray) {
            attObj.addStorageType(storageType);
        }

        // Add missing value code into attribute
        for (int k = 0; k < missingValueCodeVector.size(); k++) {
            String missingValueCode = (String) missingValueCodeVector.elementAt(k);
            if (isDebugging) {
                //log.debug("the mssing value code " + missingCodeValue + 
                //          " was added to attribute");
            }

            attObj.addMissingValueCode(missingValueCode);
        }

        attributeList.add(attObj);
    }
}

From source file:org.ecoinformatics.datamanager.parser.generic.GenericDataPackageParser.java

/**
 * Pulls the entity information out of the XML and stores it in a hash table.
 *//*from  w w  w  .  j a v a 2 s  . co  m*/
private void processEntities(CachedXPathAPI xpathapi, NodeList entitiesNodeList, String xpath, String packageId)
        throws SAXException, javax.xml.transform.TransformerException, Exception {
    // Make sure that entities is not null
    if (entitiesNodeList == null) {
        return;
    }

    int entityNodeListLength = entitiesNodeList.getLength();
    numEntities = numEntities + entityNodeListLength;
    String entityName = "";
    String entityDescription = "";
    String entityOrientation = "";
    String entityCaseSensitive = "";
    String entityNumberOfRecords = "-1";
    String onlineUrl = "";
    String format = null;
    String numHeaderLines = "0";
    int numFooterLines = 0;
    String fieldDelimiter = null;
    String recordDelimiter = "";
    String compressionMethod = "";
    String encodingMethod = "";
    String quoteCharacter = null;
    String literalCharacter = null;
    boolean isImageEntity = false;
    boolean isOtherEntity = false;
    boolean isGZipDataFile = false;
    boolean isZipDataFile = false;
    boolean isTarDataFile = false;
    boolean isSimpleDelimited = true;
    boolean isCollapseDelimiters = false;
    TextComplexDataFormat[] formatArray = null;

    for (int i = 0; i < entityNodeListLength; i++) {

        if (xpath != null) {
            if (xpath.equals(spatialRasterEntityPath) || xpath.equals(spatialVectorEntityPath)) {
                isImageEntity = true;
            } else if (xpath.equals(otherEntityPath)) {
                isOtherEntity = true;
            }
        }

        //go through the entities and put the information into the hash.
        elementId++;
        Node entityNode = entitiesNodeList.item(i);
        String id = null;
        NamedNodeMap entityNodeAttributes = entityNode.getAttributes();

        if (entityNodeAttributes != null) {
            Node idNode = entityNodeAttributes.getNamedItem(ID);

            if (idNode != null) {
                id = idNode.getNodeValue();
            }
        }

        NodeList entityNodeChildren = entityNode.getChildNodes();

        for (int j = 0; j < entityNodeChildren.getLength(); j++) {
            Node childNode = entityNodeChildren.item(j);
            String childName = childNode.getNodeName();
            String childValue = childNode.getFirstChild() == null ? null
                    : childNode.getFirstChild().getNodeValue();

            if (childName.equals("entityName")) {
                entityName = childValue;
            } else if (childName.equals("entityDescription")) {
                entityDescription = childValue;
            } else if (childName.equals("caseSensitive")) {
                entityCaseSensitive = childValue;
            } else if (childName.equals("numberOfRecords")) {
                entityNumberOfRecords = childValue;
                /*numRecords = (new Integer(entityNumberOfRecords))
                            .intValue();*/
            }

        }

        NodeList attributeOrientationNodeList = xpathapi.selectNodeList(entityNode,
                "physical/dataFormat/textFormat/attributeOrientation");

        if (attributeOrientationNodeList != null && attributeOrientationNodeList.getLength() > 0) {
            entityOrientation = attributeOrientationNodeList.item(0).getFirstChild().getNodeValue();

        }

        NodeList numHeaderLinesNodeList = xpathapi.selectNodeList(entityNode,
                "physical/dataFormat/textFormat/numHeaderLines");

        if ((numHeaderLinesNodeList != null) && (numHeaderLinesNodeList.getLength() > 0)) {
            Node numHeaderLinesNode = numHeaderLinesNodeList.item(0);

            if (numHeaderLinesNode != null) {
                numHeaderLines = numHeaderLinesNode.getFirstChild().getNodeValue();
            }
        }

        NodeList numFooterLinesNodeList = xpathapi.selectNodeList(entityNode,
                "physical/dataFormat/textFormat/numFooterLines");

        if ((numFooterLinesNodeList != null) && (numFooterLinesNodeList.getLength() > 0)) {
            Node numFooterLinesNode = numFooterLinesNodeList.item(0);

            if (numFooterLinesNode != null) {
                String numFooterLinesStr = numFooterLinesNode.getFirstChild().getNodeValue();
                numFooterLines = (new Integer(numFooterLinesStr.trim())).intValue();
            }
        }

        // Here is the simple delimited data file
        NodeList fieldDelimiterNodeList = xpathapi.selectNodeList(entityNode,
                "physical/dataFormat/textFormat/simpleDelimited/fieldDelimiter");

        if (fieldDelimiterNodeList != null && fieldDelimiterNodeList.getLength() > 0) {
            fieldDelimiter = fieldDelimiterNodeList.item(0).getFirstChild().getNodeValue();
        }

        NodeList collapseDelimitersNodeList = xpathapi.selectNodeList(entityNode,
                "physical/dataFormat/textFormat/simpleDelimited/collapseDelimiters");

        if (collapseDelimitersNodeList != null && collapseDelimitersNodeList.getLength() > 0) {

            String collapseDelimiters = collapseDelimitersNodeList.item(0).getFirstChild().getNodeValue();

            if (collapseDelimiters.equalsIgnoreCase("yes")) {
                isCollapseDelimiters = true;
            }
        }

        NodeList quoteCharacterNodeList = xpathapi.selectNodeList(entityNode,
                "physical/dataFormat/textFormat/simpleDelimited/quoteCharacter");

        if (quoteCharacterNodeList != null && quoteCharacterNodeList.getLength() > 0) {
            quoteCharacter = quoteCharacterNodeList.item(0).getFirstChild().getNodeValue();
        }

        NodeList literalCharacterNodeList = xpathapi.selectNodeList(entityNode,
                "physical/dataFormat/textFormat/simpleDelimited/literalCharacter");

        if (literalCharacterNodeList != null && literalCharacterNodeList.getLength() > 0) {
            literalCharacter = literalCharacterNodeList.item(0).getFirstChild().getNodeValue();
        }

        // For complex format data file
        NodeList complexNodeList = xpathapi.selectNodeList(entityNode,
                "physical/dataFormat/textFormat/complex");

        if (complexNodeList != null && complexNodeList.getLength() > 0) {
            //log.debug("in handle complex text data format");
            isSimpleDelimited = false;
            Node complexNode = complexNodeList.item(0);
            NodeList complexChildNodes = complexNode.getChildNodes();
            int complexChildNodesLength = complexChildNodes.getLength();
            Vector formatVector = new Vector();

            for (int k = 0; k < complexChildNodesLength; k++) {
                Node complexChildNode = complexChildNodes.item(k);

                if (complexChildNode != null && complexChildNode.getNodeName().equals("textFixed")) {
                    TextWidthFixedDataFormat textWidthFixedDataFormat = handleTextFixedDataFormatNode(
                            complexChildNode);

                    if (textWidthFixedDataFormat != null) {
                        formatVector.add(textWidthFixedDataFormat);
                        //complexFormatsNumber++;
                    }
                } else if (complexChildNode != null && complexChildNode.getNodeName().equals("textDelimited")) {
                    TextDelimitedDataFormat textDelimitedDataFormat = handleComplexDelimitedDataFormatNode(
                            complexChildNode);

                    if (textDelimitedDataFormat != null) {
                        formatVector.add(textDelimitedDataFormat);
                        //complexFormatsNumber++;
                    }
                }
            }

            // Transfer vector to array
            numberOfComplexFormats = formatVector.size();
            formatArray = new TextComplexDataFormat[numberOfComplexFormats];
            for (int j = 0; j < numberOfComplexFormats; j++) {
                formatArray[j] = (TextComplexDataFormat) formatVector.elementAt(j);
            }
        }

        NodeList recordDelimiterNodeList = xpathapi.selectNodeList(entityNode,
                "physical/dataFormat/textFormat/recordDelimiter");

        if ((recordDelimiterNodeList != null) && (recordDelimiterNodeList.getLength() > 0)) {
            recordDelimiter = recordDelimiterNodeList.item(0).getFirstChild().getNodeValue();
        } else {
            recordDelimiter = "\\r\\n";
        }

        // Get the distribution information
        NodeList urlNodeList = xpathapi.selectNodeList(entityNode, "physical/distribution/online/url");

        if (urlNodeList != null && urlNodeList.getLength() > 0) {
            onlineUrl = urlNodeList.item(0).getFirstChild().getNodeValue();

            if (isDebugging) {
                //log.debug("The url is "+ onlineUrl);
            }
        }

        /**
         * Determine file format (mime)
         * Note: this could be better fleshed out in cases where the delimiter is known
         * 
         * physical/dataFormat/textFormat
         * physical/dataFormat/binaryRasterFormat
         * physical/dataFormat/externallyDefinedFormat/formatName
         */
        NodeList formatNodeList = xpathapi.selectNodeList(entityNode,
                "physical/dataFormat/externallyDefinedFormat/formatName");
        if (formatNodeList != null && formatNodeList.getLength() > 0) {
            format = formatNodeList.item(0).getFirstChild().getNodeValue();
        } else {
            // try binary raster
            formatNodeList = xpathapi.selectNodeList(entityNode, "physical/dataFormat/binaryRasterFormat");
            if (formatNodeList != null && formatNodeList.getLength() > 0) {
                format = "application/octet-stream";
            } else {
                formatNodeList = xpathapi.selectNodeList(entityNode, "physical/dataFormat/textFormat");
                if (formatNodeList != null && formatNodeList.getLength() > 0) {
                    format = "text/plain";
                }
                if (isSimpleDelimited) {
                    format = "text/csv";
                }
            }
        }

        // Get the compressionMethod information
        NodeList compressionMethodNodeList = xpathapi.selectNodeList(entityNode, "physical/compressionMethod");

        if (compressionMethodNodeList != null && compressionMethodNodeList.getLength() > 0) {
            compressionMethod = compressionMethodNodeList.item(0).getFirstChild().getNodeValue();

            if (isDebugging) {
                //log.debug("Compression method is "+compressionMethod);
            }

            if (compressionMethod != null && compressionMethod.equals(Entity.GZIP)) {
                isGZipDataFile = true;
            } else if (compressionMethod != null && compressionMethod.equals(Entity.ZIP)) {
                isZipDataFile = true;
            }
        }

        // Get encoding method info (mainly for tar file)
        NodeList encodingMethodNodeList = xpathapi.selectNodeList(entityNode, "physical/encodingMethod");

        if (encodingMethodNodeList != null && encodingMethodNodeList.getLength() > 0) {
            encodingMethod = encodingMethodNodeList.item(0).getFirstChild().getNodeValue();

            if (isDebugging) {
                //log.debug("encoding method is "+encodingMethod);
            }

            if (encodingMethod != null && encodingMethod.equals(Entity.TAR)) {
                isTarDataFile = true;
            }
        }

        if (entityOrientation.trim().equals("column")) {
            entityOrientation = Entity.COLUMNMAJOR;
        } else {
            entityOrientation = Entity.ROWMAJOR;
        }

        if (entityCaseSensitive.equals("yes")) {
            entityCaseSensitive = "true";
        } else {
            entityCaseSensitive = "false";
        }

        entityObject = new Entity(id, entityName == null ? null : entityName.trim(),
                entityDescription == null ? null : entityDescription.trim(), new Boolean(entityCaseSensitive),
                entityOrientation, new Integer(entityNumberOfRecords).intValue());

        entityObject.setNumHeaderLines((new Integer(numHeaderLines)).intValue());
        entityObject.setNumFooterLines(numFooterLines);
        entityObject.setSimpleDelimited(isSimpleDelimited);

        // For simple delimited data file
        if (fieldDelimiter != null) {
            entityObject.setDelimiter(fieldDelimiter);
        }

        if (quoteCharacter != null) {
            entityObject.setQuoteCharacter(quoteCharacter);
        }

        if (literalCharacter != null) {
            entityObject.setLiteralCharacter(literalCharacter);
        }

        entityObject.setCollapseDelimiters(isCollapseDelimiters);
        entityObject.setRecordDelimiter(recordDelimiter);
        entityObject.setURL(onlineUrl);
        entityObject.setDataFormat(format);
        entityObject.setCompressionMethod(compressionMethod);
        entityObject.setIsImageEntity(isImageEntity);
        entityObject.setIsOtherEntity(isOtherEntity);
        entityObject.setHasGZipDataFile(isGZipDataFile);
        entityObject.setHasZipDataFile(isZipDataFile);
        entityObject.setHasTarDataFile(isTarDataFile);
        entityObject.setPackageId(packageId);

        try {
            NodeList attributeListNodeList = xpathapi.selectNodeList(entityNode, "attributeList");
            processAttributeList(xpathapi, attributeListNodeList, xpath, entityObject);
            entityObject.setDataFormatArray(formatArray);
        } catch (Exception e) {
            throw new Exception("Error parsing attributes: " + e.getMessage(), e);
        }

        //entityHash.put(Integer.toString(elementId), entityObject);
        emlDataPackage.add(entityObject);
        //fileHash.put(elementId, onlineUrl); 
    } // end for loop

}

From source file:org.ecoinformatics.seek.datasource.eml.eml2.Eml200Parser.java

/**
 * Method to get the boolean hasImageEntity. If the eml document has
 * SpatialRaster or SpatialVector entity, this variable should be true;
 * /*from  w  w  w .  j  ava  2 s  .  co  m*/
 * @return boolean
 */
/*
 * public boolean getHasImageEntity() { return this.hasImageEntity;
 * 
 * }
 */

/*
 * Porcess the attribute list element
 */
private void processAttributeList(CachedXPathAPI xpathapi, NodeList attList, Entity entObj) throws Exception {
    AttributeList attributeList = new AttributeList();
    Node attListNode = attList.item(0);
    // get attributeList element's attribute - id
    NamedNodeMap idAttribute = attListNode.getAttributes();
    String idString = null;
    if (idAttribute != null) {
        Node id = idAttribute.getNamedItem("id");
        if (id != null) {
            idString = id.getNodeValue();
            if (isDebugging) {
                log.debug("The id value for the attributelist is " + idString);
            }
        }
    }
    NodeList attNodeList = xpathapi.selectNodeList(attListNode, "attribute");
    NodeList referenceNodeList = xpathapi.selectNodeList(attListNode, "references");
    if (attNodeList != null && attNodeList.getLength() > 0) {

        processAttributes(xpathapi, attNodeList, attributeList);
        if (idString != null) {
            attributeListHash.put(idString, attributeList);

        }
    } else if (referenceNodeList != null && referenceNodeList.getLength() > 0) {
        // get the references id
        Node referenceNode = referenceNodeList.item(0);
        if (isDebugging) {
            log.debug("The reference node's name is " + referenceNode.getNodeName());
        }
        String referenceId = referenceNode.getFirstChild().getNodeValue();
        if (isDebugging) {
            log.debug("the reference id is " + referenceId);
        }
        attributeList = attributeListHash.get(referenceId);
    } else {
        log.debug("The children name of attribute list couldn't be understood");
        throw new Exception(" couldn't be a child of attributeList");
    }

    if (!entityObject.isSimpleDelimited()) {
        int length = attributeList.getAttributes().size();
        if (length != complexFormatsNumber || (length == complexFormatsNumber && complexFormatsNumber == 0)) {
            throw new Exception("Complex format elements should have" + " some number as attribute number");
        } else {
            // entityObject.setDataFormatArray(formatArray);
        }
    }

    entityObject.setAttributeList(attributeList);

}

From source file:org.energy_home.jemma.ah.internal.configurator.Configuratore.java

private void traverseConfigurationTree(Node node) {
    lastNode = node;/*w  ww  . ja  v  a2  s  . c om*/
    int nodeType = node.getNodeType();

    switch (nodeType) {
    case Node.DOCUMENT_NODE:
        traverseConfigurationTree(((Document) node).getDocumentElement());
        break;

    // print element with attributes
    case Node.ELEMENT_NODE:
        NamedNodeMap attrs = node.getAttributes();
        String tag = node.getNodeName();

        if ((tag == "location") && (loadLocations)) {
            String name = attrs.getNamedItem("name").getNodeValue();
            String icon = attrs.getNamedItem("icon").getNodeValue();
            String pid = attrs.getNamedItem("pid").getNodeValue();

            locations.add(new Location(pid, name, icon));

        }
        if ((tag == "category") && (loadCategories)) {
            String name = attrs.getNamedItem("name").getNodeValue();
            String icon = attrs.getNamedItem("icon").getNodeValue();
            String pid = attrs.getNamedItem("pid").getNodeValue();

            categories.add(new Category(pid, name, icon));

        } else if (tag == "rule") {
            try {
                String rule = attrs.getNamedItem("filter").getNodeValue();
                rules.add(rule);
            } catch (Exception e) {
                log.error(e);
            }
        } else if (tag == "connect") {
            try {
                String pid1 = attrs.getNamedItem("pid1").getNodeValue();
                String pid2 = attrs.getNamedItem("pid2").getNodeValue();
                this.connAdmin.createConnection(pid1, pid2);
            } catch (Exception e) {
                log.error(e);
            }
        } else if ((tag == "appliance") && (loadAppliances)) {
            props = new Hashtable();
        } else if ((tag == "configuration")) {
            props = new Hashtable();
        } else if ((tag == "property") && (lastNode != null) && (props != null)) {
            // log.debug("last node is " + lastNode.getNodeName());
            String name = attrs.getNamedItem("name").getNodeValue();
            Object propValue = traversePropertyNode(lastNode);
            if (propValue == null) {
                log.error("null property " + name);
            } else {
                props.put(name, propValue);
            }
        }
        NodeList children = node.getChildNodes();

        if (children != null) {
            int len = children.getLength();
            for (int i = 0; i < len; i++) {
                traverseConfigurationTree(children.item(i));
            }
            if (tag.equals("appliance")) {
                // we traversed all appliances children, its time to
                // instantiate the
                // driver
                try {
                    String appliancePid = (String) props.get(Constants.SERVICE_PID);
                    if (appliancePid == null) {
                        // for backward compatibility
                        appliancePid = (String) props.get(IAppliance.APPLIANCE_PID);
                    }

                    String type = (String) props.get(IAppliance.APPLIANCE_TYPE_PROPERTY);

                    if ((appliancePid != null) && (type != null)) {
                        String locationPid = (String) props.get(IAppliance.APPLIANCE_LOCATION_PID_PROPERTY);
                        String categoryPid = (String) props.get(IAppliance.APPLIANCE_CATEGORY_PID_PROPERTY);
                        createAppliance(appliancePid, props);
                        // }
                    } else {
                        log.error("during reading configuration: unable to retrieve driver pid");
                    }
                } catch (Exception e) {
                    log.debug(e.getMessage());
                }
            } else if (tag.equals("configuration")) {
                // we traversed all appliances children, its time to
                // instantiate the
                // driver
                try {
                    configurationsVector.add(props);
                } catch (Exception e) {
                    log.debug(e.getMessage());
                }
            }
        }
        break;

    case Node.TEXT_NODE:
        break;
    }
}

From source file:org.energy_home.jemma.ah.internal.configurator.Configuratore.java

public void traverseConfigurationTreeOld(Node node) {
    lastNode = node;/*from  w  ww .  ja  v a 2s.co m*/
    int nodeType = node.getNodeType();

    switch (nodeType) {
    case Node.DOCUMENT_NODE:
        traverseConfigurationTreeOld(((Document) node).getDocumentElement());
        break;

    // print element with attributes
    case Node.ELEMENT_NODE:
        NamedNodeMap attrs = node.getAttributes();
        String tag = node.getNodeName();

        if ((tag == "location") && (loadLocations)) {
            String name = attrs.getNamedItem("name").getNodeValue();
            String icon = attrs.getNamedItem("icon").getNodeValue();
            String pid = attrs.getNamedItem("pid").getNodeValue();

            try {
                this.hacService.addLocation(new Location(pid, name, icon));
            } catch (Throwable e) {
                // this is a duplicate location, skip it by putting a log
                // message
                log.warn("error while adding location found reading configuration file");
            }
        }
        if ((tag == "category") && (loadCategories)) {
            String name = attrs.getNamedItem("name").getNodeValue();
            String icon = attrs.getNamedItem("icon").getNodeValue();
            String pid = attrs.getNamedItem("pid").getNodeValue();

            categories.add(new Category(pid, name, icon));
        } else if (tag == "rule") {
            try {
                String rule = attrs.getNamedItem("filter").getNodeValue();
                rules.add(rule);
                // this.connAdmin.addBindRule(rule);
            } catch (Exception e) {
                log.error(e);
            }
        } else if (tag == "connect") {
            try {
                String pid1 = attrs.getNamedItem("pid1").getNodeValue();
                String pid2 = attrs.getNamedItem("pid2").getNodeValue();
                //this.connAdmin.createConnection(pid1, pid2);
            } catch (Exception e) {
                log.error(e);
            }
        } else if ((tag == "appliance") && (loadAppliances)) {
            props = new Hashtable();
        } else if ((tag == "property") && (lastNode != null) && (props != null)) {
            // log.debug("last node is " + lastNode.getNodeName());
            String name = attrs.getNamedItem("name").getNodeValue();
            Object propValue = traversePropertyNode(lastNode);
            if (propValue == null) {
                log.error("null property " + name);
            } else {
                props.put(name, propValue);
            }
        }
        NodeList children = node.getChildNodes();

        if (children != null) {
            int len = children.getLength();
            for (int i = 0; i < len; i++) {
                traverseConfigurationTreeOld(children.item(i));
            }
            if (tag.equals("appliance")) {
                // we traversed all appliances children, its time to
                // instantiate the
                // driver
                try {
                    String appliancePid = (String) props.get(Constants.SERVICE_PID);
                    if (appliancePid == null) {
                        // for backward compatibility
                        appliancePid = (String) props.get(IAppliance.APPLIANCE_PID);
                    }

                    String type = (String) props.get(IAppliance.APPLIANCE_TYPE_PROPERTY);

                    if ((appliancePid != null) && (type != null)) {
                        String locationPid = (String) props.get(IAppliance.APPLIANCE_LOCATION_PID_PROPERTY);
                        String categoryPid = (String) props.get(IAppliance.APPLIANCE_CATEGORY_PID_PROPERTY);
                        createAppliance(appliancePid, props);
                        // }
                    } else {
                        log.error("during reading configuration: unable to retrieve driver pid");
                    }
                } catch (Exception e) {
                    log.debug(e.getMessage());
                }
            }
        }
        break;

    case Node.TEXT_NODE:
        break;
    }
}

From source file:org.energy_home.jemma.ah.internal.configurator.Configuratore.java

private Object traverseItemNode(Node node) {
    int nodeType = node.getNodeType();

    switch (nodeType) {
    // print element with attributes
    case Node.ELEMENT_NODE:
        NamedNodeMap attrs = node.getAttributes();
        String tag = node.getNodeName();

        Node item = attrs.getNamedItem("type");
        String type = null;/*ww w.  j  ava2s.  c o  m*/
        if (item != null) {
            type = item.getNodeValue();
        }
        if (tag.equals("item")) {
            return this.getInstance(arrayType, getTextContent(node));
        }
    }
    return null;
}

From source file:org.energy_home.jemma.ah.internal.configurator.Configuratore.java

public Object traversePropertyNode(Node node) {
    int nodeType = node.getNodeType();

    switch (nodeType) {
    // print element with attributes
    case Node.ELEMENT_NODE:
        NamedNodeMap attrs = node.getAttributes();
        String tag = node.getNodeName();

        Node item = attrs.getNamedItem("type");
        String type = null;//from   ww w  .  j  a  v a2s .c  o  m
        if (item != null) {
            type = item.getNodeValue();
        }

        if (tag.equals("property")) {
            if (type.endsWith("[]")) {
                isArray = true;
                arrayType = type.substring(0, type.length() - 2);
            } else {
                isArray = false;
                arrayType = null;
            }

            if (type.equals("Vector")) {
                NodeList children = node.getChildNodes();
                int len = children.getLength();
                Vector container = new Vector();
                for (int i = 0; i < len; i++) {
                    Object res = traverseItemNode(children.item(i));

                    if (res != null) {
                        container.add(res);
                        log.debug("added to vector/array " + res.toString());
                    }
                }

                return container;

            } else if (isArray) {
                NodeList children = node.getChildNodes();
                int len = children.getLength();
                Vector container = new Vector();
                for (int i = 0; i < len; i++) {
                    Object res = traverseItemNode(children.item(i));
                    if (res != null) {
                        container.add(res);
                        log.debug("added to vector/array " + res.toString());
                    }
                }

                if (isBaseType(this.arrayType)) {
                    return fillBaseTypeArrayInstance(arrayType, container);

                } else {
                    return container.toArray(getArrayInstance(arrayType, 0));
                }
            } else {
                return this.getInstance(type, getTextContent(node));
            }
        }
    }

    return null;
}

From source file:org.energy_home.jemma.ah.internal.hac.lib.HacService.java

protected void traverseConfigurationTree(Node node) {
    lastNode = node;//from  ww w  .  j  a va2s . c  om
    int nodeType = node.getNodeType();

    switch (nodeType) {
    case Node.DOCUMENT_NODE:
        traverseConfigurationTree(((Document) node).getDocumentElement());
        break;

    // print element with attributes
    case Node.ELEMENT_NODE:
        NamedNodeMap attrs = node.getAttributes();
        String tag = node.getNodeName();

        if ((tag == "location") && (loadLocations)) {
            String name = attrs.getNamedItem("name").getNodeValue();
            String icon = attrs.getNamedItem("icon").getNodeValue();
            String pid = attrs.getNamedItem("pid").getNodeValue();

            try {
                this.locationsDb.add(new Location(pid, name, icon));
            } catch (HacException e) {
                // this is a duplicate location, skip it by putting a log
                // message
                LOG.warn("error while adding location found reading configuration file", e);
            }
        }
        if (tag == "category") {
            String name = attrs.getNamedItem("name").getNodeValue();
            String icon = attrs.getNamedItem("icon").getNodeValue();
            String pid = attrs.getNamedItem("pid").getNodeValue();

            try {
                categories.add(new Category(pid, name, icon));
            } catch (HacException e) {
                // this is a duplicate location, skip it by putting a log
                // message
                LOG.warn("error while adding location found reading configuration file", e);
            }
        } else if ((tag == "appliance") && (loadAppliances)) {
            /*
             * String name = attrs.getNamedItem("name").getNodeValue();
             * if (log.isDebugEnabled()) log.debug("reading va " + name);
             */
            properties = new Hashtable();
        } else if ((tag == "property") && (lastNode != null) && (properties != null)) {
            // log.debug("last node is " + lastNode.getNodeName());
            String name = attrs.getNamedItem("name").getNodeValue();

            // Node item = attrs.getNamedItem("type");

            // String type = null;
            //
            // if (item != null) {
            // type = item.getNodeValue();
            // }

            Object propValue = traversePropertyNode(lastNode);
            if (propValue == null) {
                LOG.debug("null property " + name);
            } else {
                properties.put(name, propValue);
            }
        }
        NodeList children = node.getChildNodes();

        if (children != null) {
            int len = children.getLength();
            for (int i = 0; i < len; i++) {
                traverseConfigurationTree(children.item(i));
            }
            if (tag.equals("appliance")) {
                // we traversed all appliances children, its time to
                // instantiate the
                // driver
                try {
                    String appliancePid = (String) properties.get(Constants.SERVICE_PID);
                    if (appliancePid == null) {
                        // for backward compatibility
                        appliancePid = (String) properties.get(IAppliance.APPLIANCE_PID);
                    }

                    String type = (String) properties.get(IAppliance.APPLIANCE_TYPE_PROPERTY);

                    if ((appliancePid != null) && (type != null)) {
                        String locationPid = (String) properties
                                .get(IAppliance.APPLIANCE_LOCATION_PID_PROPERTY);
                        String categoryPid = (String) properties
                                .get(IAppliance.APPLIANCE_CATEGORY_PID_PROPERTY);
                        if ((locationPid != null) && (this.locationsDb.getByPid(locationPid) == null)) {
                            LOG.debug("WARNING: device " + servicePid + " specifies an unknown location pid");
                        } else if ((categoryPid != null)
                                && (this.categories.getCategoryByPid(categoryPid) == null)) {
                            LOG.debug("WARNING: device " + servicePid + " specifies an unknown category pid");
                        } else {
                            createConfiguration(type, appliancePid, properties);
                        }
                    } else {
                        LOG.debug("during reading configuration: unable to retrieve driver pid");
                    }
                } catch (Exception e) {
                    LOG.warn(e.getMessage(), e);
                }
            }
        }
        break;

    case Node.TEXT_NODE:
        break;
    }
}