Class DomParser


  • public class DomParser
    extends java.lang.Object
    XML parser for OPC UA companion spec files.
    Author:
    Jan-Hendrik Cepok, SSE
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private DomParser​(org.w3c.dom.NodeList objectTypeList, org.w3c.dom.NodeList objectList, org.w3c.dom.NodeList variableList, org.w3c.dom.NodeList methodList, org.w3c.dom.NodeList dataTypeList, org.w3c.dom.NodeList variableTypeList, org.w3c.dom.NodeList aliasList, java.util.ArrayList<BaseType> hierarchy)
      Creates a DOM parser/translator.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void adaptDatatypesToModel​(ObjectType uaObject, MethodType uaMethod)
      Adapts the OPC UA data types to IIP-Ecosphere meta model type names.
      private void addElement​(BaseType element, ElementType type)
      Adds an element to the hierarchy.
      private java.lang.String changeVariableDataTypes​(java.lang.String dataType)
      Turns OPC UA type names to IIP-Ecosphere meta model type names.
      private java.lang.String checkForExternDataType​(java.lang.String dataType)
      Checks for an extern data type.
      private java.lang.String checkForInternDataType​(java.lang.String dataTypeNodeId)
      Checks for intern data type.
      private boolean checkRedundancy​(java.lang.String varName, java.util.ArrayList<FieldType> list)
      Checks for redundant/duplicate variable names in hierarchy.
      private static org.w3c.dom.Element checkRelation​(java.lang.String currentNodeId, org.w3c.dom.NodeList nodes)
      Checks the relations and returns a node with NodeId currentNodeId.
      private static java.io.File[] checkRequiredModels​(DomParser parser, java.lang.String modelName, java.lang.String path, java.lang.String fileName, org.w3c.dom.NodeList nameSpaceUris)
      Checks for required models.
      private void createElement​(ElementType type, org.w3c.dom.Element element, java.lang.String id, java.lang.String displayName, java.lang.String description, java.lang.String documentation, java.util.ArrayList<FieldType> subFields, java.util.ArrayList<FieldType> objectFields, java.util.ArrayList<EnumLiteral> literals, java.util.ArrayList<DataLiteral> dataLiterals, java.lang.String typeDef, boolean optional)
      Creates an element.
      private void createIvmlModel​(java.lang.String fileName, java.io.File ivmlFile)
      Creates the IVML model in the given fileName.
      private static DomParser createParser​(java.lang.String path, java.io.File compSpec, boolean verbose)
      Creates the parser.
      private DomParser.DescriptionOrDocumentation getDescriptionOrDocumentation​(java.lang.String reference, org.w3c.dom.Element refElement)
      Extracts information about description or documentation from refElement.
      private static java.lang.String getFieldDescription​(org.w3c.dom.Element fieldNode)
      Returns the field description of fieldNode.
      private static org.w3c.dom.Element getNextNodeElement​(org.w3c.dom.NodeList nodes, int iterator)
      Returns the next node element.
      private DomParser.TypeListAndType getTypeListAndTypeRootNs​(java.lang.String refId, java.lang.String reference, ElementType type)
      Extracts the type list and type for a given root namespace refId.
      private java.util.ArrayList<FieldType> identifyFields​(org.w3c.dom.Node childNode)
      Identifies the fields of childNode.
      private java.lang.String identifySpecificReference​(java.lang.String reference, org.w3c.dom.Node node, ElementType type)
      Identifies a specific reference.
      static void main​(java.lang.String[] args)
      Executes the parser, per default in verbose mode.
      private static void nop()
      Does nothing, just allows for code convention compliance while bugfixing.
      private void parseFile()
      Parses a file by retrieving all root elements of the objects in objectList and retriving all data types in dataTypeList.
      private void println​(java.lang.String text)
      Prints out information in verbose mode.
      static void process​(java.io.File xmlIn, java.lang.String outName, java.io.File ivmlOut, boolean verbose)
      Processes an OPC XML file.
      private void retrieveAttributes​(org.w3c.dom.Element element, java.util.ArrayList<FieldType> subFields, ElementType type, java.lang.String externNodeId)
      Retrieves the attributes and creates respective elements.
      private java.lang.String retrieveAttributesForExternDataType​(java.lang.String nodeId)
      Retrieves attributes from documents for extern data types.
      private void retrieveAttributesForRefElement​(java.util.ArrayList<FieldType> fields, java.lang.String refId, org.w3c.dom.Element refElement, ElementType elementType)
      Retrieves attribute for a reference element.
      private void retrieveElementTypes()
      Retrieves the element types and nested attributes via #retrieveAttributes(Element, ArrayList, ElementType).
      private java.lang.String retrieveParent​(java.lang.String parentNodeId, org.w3c.dom.NodeList list)
      Retrieves the displayName of given parentNodeId.
      private void retrieveRelatedSubElements​(java.util.ArrayList<FieldType> subElements)
      Retrieves the related sub elements.
      private void retrieveRootElement​(org.w3c.dom.Element object, ElementType type)
      Retrieves the root element.
      private static java.lang.String searchVarName​(BaseType uaElement, java.util.ArrayList<BaseType> hierarchy)
      Searches for a field type variable name.
      void setBaseNameSpace​(java.lang.String baseNameSpace)
      Defines the name space of the basic OPC UA Spec.
      static void setDefaultVerbose​(boolean verbose)
      Changes the default verbose mode used when instantiating a parser.
      void setDocuments​(org.w3c.dom.Document[] documents)
      Defines the OPC UA document collection.
      void setExternAliasLists​(java.util.ArrayList<org.w3c.dom.NodeList> externAliasLists)
      Defines the OPC UA core alias list.
      static void setUsingIvmlFolder​(java.lang.String folder)
      Sets the folder where to generate example using IVML models.
      private static java.lang.String toOsPath​(java.io.File file)
      Turns a file into an operating-system dependent string path.
      private static java.lang.String toOsPath​(java.lang.String path)
      Turns a string (assumed to be a path) into an operating-system dependent string path.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • verboseDefault

        private static boolean verboseDefault
      • usingIvmlFolder

        private static java.lang.String usingIvmlFolder
      • IDENTIFY_FIELDS_PERMITTED_REFERENCE_TYPE

        private static final java.util.Set<java.lang.String> IDENTIFY_FIELDS_PERMITTED_REFERENCE_TYPE
      • documents

        private org.w3c.dom.Document[] documents
      • objectTypeList

        private org.w3c.dom.NodeList objectTypeList
      • objectList

        private org.w3c.dom.NodeList objectList
      • variableList

        private org.w3c.dom.NodeList variableList
      • methodList

        private org.w3c.dom.NodeList methodList
      • dataTypeList

        private org.w3c.dom.NodeList dataTypeList
      • variableTypeList

        private org.w3c.dom.NodeList variableTypeList
      • aliasList

        private org.w3c.dom.NodeList aliasList
      • hierarchy

        private java.util.ArrayList<BaseType> hierarchy
      • verbose

        private boolean verbose
      • baseNameSpace

        private java.lang.String baseNameSpace
      • externAliasLists

        private java.util.ArrayList<org.w3c.dom.NodeList> externAliasLists
    • Constructor Detail

      • DomParser

        private DomParser​(org.w3c.dom.NodeList objectTypeList,
                          org.w3c.dom.NodeList objectList,
                          org.w3c.dom.NodeList variableList,
                          org.w3c.dom.NodeList methodList,
                          org.w3c.dom.NodeList dataTypeList,
                          org.w3c.dom.NodeList variableTypeList,
                          org.w3c.dom.NodeList aliasList,
                          java.util.ArrayList<BaseType> hierarchy)
        Creates a DOM parser/translator.
        Parameters:
        objectTypeList - the already parsed object type list
        objectList - the already parsed object list
        variableList - the already parsed variable list
        methodList - the already parsed method list
        dataTypeList - the already parsed data type list
        variableTypeList - the already parsed variable type list
        aliasList - the already parsed alias list
        hierarchy - the base type hierarchy
    • Method Detail

      • setExternAliasLists

        public void setExternAliasLists​(java.util.ArrayList<org.w3c.dom.NodeList> externAliasLists)
        Defines the OPC UA core alias list.
        Parameters:
        externAliasLists - the list of OPC UA core aliases.
      • setDocuments

        public void setDocuments​(org.w3c.dom.Document[] documents)
        Defines the OPC UA document collection.
        Parameters:
        documents - the collection of documents.
      • setBaseNameSpace

        public void setBaseNameSpace​(java.lang.String baseNameSpace)
        Defines the name space of the basic OPC UA Spec.
        Parameters:
        baseNameSpace - the base name space
      • setDefaultVerbose

        public static void setDefaultVerbose​(boolean verbose)
        Changes the default verbose mode used when instantiating a parser.
        Parameters:
        verbose - verbose or non verbose mode
      • searchVarName

        private static java.lang.String searchVarName​(BaseType uaElement,
                                                      java.util.ArrayList<BaseType> hierarchy)
        Searches for a field type variable name.
        Parameters:
        uaElement - the UA element delivering the node Id to search for
        hierarchy - the type hierarchy to search within
        Returns:
        the variable name of the found field type
      • changeVariableDataTypes

        private java.lang.String changeVariableDataTypes​(java.lang.String dataType)
        Turns OPC UA type names to IIP-Ecosphere meta model type names.
        Parameters:
        dataType - the data type
        Returns:
        the translated data type
      • adaptDatatypesToModel

        private void adaptDatatypesToModel​(ObjectType uaObject,
                                           MethodType uaMethod)
        Adapts the OPC UA data types to IIP-Ecosphere meta model type names.
        Parameters:
        uaObject - the UA object to adapt the types for
        uaMethod - the UA method to adapt the types for
      • nop

        private static void nop()
        Does nothing, just allows for code convention compliance while bugfixing.
      • checkRelation

        private static org.w3c.dom.Element checkRelation​(java.lang.String currentNodeId,
                                                         org.w3c.dom.NodeList nodes)
        Checks the relations and returns a node with NodeId currentNodeId.
        Parameters:
        currentNodeId - the node id to search for
        nodes - the nodes to search
        Returns:
        the found element
      • getNextNodeElement

        private static org.w3c.dom.Element getNextNodeElement​(org.w3c.dom.NodeList nodes,
                                                              int iterator)
        Returns the next node element.
        Parameters:
        nodes - the nodes to search for
        iterator - the 0-based index into nodes
        Returns:
        the next node element
      • retrieveParent

        private java.lang.String retrieveParent​(java.lang.String parentNodeId,
                                                org.w3c.dom.NodeList list)
        Retrieves the displayName of given parentNodeId.
        Parameters:
        parentNodeId - the parent node id
        list - the node list to check
        Returns:
        the root parent
      • checkForInternDataType

        private java.lang.String checkForInternDataType​(java.lang.String dataTypeNodeId)
        Checks for intern data type.
        Parameters:
        dataTypeNodeId - the referenced node id of a data type
        Returns:
        the identified data type
      • checkForExternDataType

        private java.lang.String checkForExternDataType​(java.lang.String dataType)
        Checks for an extern data type.
        Parameters:
        dataType - the data type to look for
        Returns:
        the data type
      • retrieveAttributesForExternDataType

        private java.lang.String retrieveAttributesForExternDataType​(java.lang.String nodeId)
        Retrieves attributes from documents for extern data types.
        Parameters:
        nodeId - the node id of the node to retrieve the attributes for
        Returns:
        the data type
      • identifySpecificReference

        private java.lang.String identifySpecificReference​(java.lang.String reference,
                                                           org.w3c.dom.Node node,
                                                           ElementType type)
        Identifies a specific reference.
        Parameters:
        reference - the reference type to look for
        node - the node to analyze the children
        type - the type to look for
        Returns:
        the reference value
      • getTypeListAndTypeRootNs

        private DomParser.TypeListAndType getTypeListAndTypeRootNs​(java.lang.String refId,
                                                                   java.lang.String reference,
                                                                   ElementType type)
        Extracts the type list and type for a given root namespace refId.
        Parameters:
        refId - the ref id
        reference - the reference type
        type - the actual element type
        Returns:
        the extracted result
      • getDescriptionOrDocumentation

        private DomParser.DescriptionOrDocumentation getDescriptionOrDocumentation​(java.lang.String reference,
                                                                                   org.w3c.dom.Element refElement)
        Extracts information about description or documentation from refElement.
        Parameters:
        reference - the actual reference being processed
        refElement - the reference element to take the data from
        Returns:
        result instance carrying the extracted information
      • identifyFields

        private java.util.ArrayList<FieldType> identifyFields​(org.w3c.dom.Node childNode)
        Identifies the fields of childNode.
        Parameters:
        childNode - the child node to analyze
        Returns:
        the identified fields
      • retrieveAttributesForRefElement

        private void retrieveAttributesForRefElement​(java.util.ArrayList<FieldType> fields,
                                                     java.lang.String refId,
                                                     org.w3c.dom.Element refElement,
                                                     ElementType elementType)
        Retrieves attribute for a reference element.
        Parameters:
        fields - the fields to retrieve the attributes for
        refId - the ref id
        refElement - the ref element
        elementType - the elementType
      • retrieveRootElement

        private void retrieveRootElement​(org.w3c.dom.Element object,
                                         ElementType type)
        Retrieves the root element.
        Parameters:
        object - the element to start with
        type - the type
      • retrieveRelatedSubElements

        private void retrieveRelatedSubElements​(java.util.ArrayList<FieldType> subElements)
        Retrieves the related sub elements.
        Parameters:
        subElements - the sub elements to analyze
      • retrieveAttributes

        private void retrieveAttributes​(org.w3c.dom.Element element,
                                        java.util.ArrayList<FieldType> subFields,
                                        ElementType type,
                                        java.lang.String externNodeId)
        Retrieves the attributes and creates respective elements.
        Parameters:
        element - the element to analyze the child nodes for
        subFields - the sub fields for the creation of output for element
        type - the element type
        externNodeId - the element type
      • getFieldDescription

        private static java.lang.String getFieldDescription​(org.w3c.dom.Element fieldNode)
        Returns the field description of fieldNode.
        Parameters:
        fieldNode - the field node
        Returns:
        the field description
      • createElement

        private void createElement​(ElementType type,
                                   org.w3c.dom.Element element,
                                   java.lang.String id,
                                   java.lang.String displayName,
                                   java.lang.String description,
                                   java.lang.String documentation,
                                   java.util.ArrayList<FieldType> subFields,
                                   java.util.ArrayList<FieldType> objectFields,
                                   java.util.ArrayList<EnumLiteral> literals,
                                   java.util.ArrayList<DataLiteral> dataLiterals,
                                   java.lang.String typeDef,
                                   boolean optional)
        Creates an element.
        Parameters:
        type - the element type.
        element - the actual element
        id - the id
        displayName - the display name
        description - the description
        documentation - the documentation
        subFields - the sub fields
        objectFields - the object fields
        literals - the literals
        dataLiterals - the data literals
        typeDef - the type def
        optional - whether the type is optional
      • addElement

        private void addElement​(BaseType element,
                                ElementType type)
        Adds an element to the hierarchy.
        Parameters:
        element - the element
        type - the element type
      • checkRedundancy

        private boolean checkRedundancy​(java.lang.String varName,
                                        java.util.ArrayList<FieldType> list)
        Checks for redundant/duplicate variable names in hierarchy.
        Parameters:
        varName - the variable name to check for
        list - the list of fields to check
        Returns:
        true if there are duplicates, false else
      • retrieveElementTypes

        private void retrieveElementTypes()
        Retrieves the element types and nested attributes via #retrieveAttributes(Element, ArrayList, ElementType).
      • toOsPath

        private static java.lang.String toOsPath​(java.io.File file)
        Turns a file into an operating-system dependent string path.
        Parameters:
        file - the file
        Returns:
        the path
      • toOsPath

        private static java.lang.String toOsPath​(java.lang.String path)
        Turns a string (assumed to be a path) into an operating-system dependent string path.
        Parameters:
        path - the path to be translated
        Returns:
        the path
      • checkRequiredModels

        private static java.io.File[] checkRequiredModels​(DomParser parser,
                                                          java.lang.String modelName,
                                                          java.lang.String path,
                                                          java.lang.String fileName,
                                                          org.w3c.dom.NodeList nameSpaceUris)
        Checks for required models.
        Parameters:
        parser - the parser instance
        modelName - the model name to check for
        path - the path to check for
        fileName - the file name to check for
        nameSpaceUris - the OPC namespace URIs
        Returns:
        the found/required model files
      • parseFile

        private void parseFile()
        Parses a file by retrieving all root elements of the objects in objectList and retriving all data types in dataTypeList.
      • createParser

        private static DomParser createParser​(java.lang.String path,
                                              java.io.File compSpec,
                                              boolean verbose)
        Creates the parser.
        Parameters:
        path - the path to the OPC UA nodeset models
        compSpec - the companion spec to be parsed
        verbose - verbose output
        Returns:
        the DOM parser after parsing
      • setUsingIvmlFolder

        public static void setUsingIvmlFolder​(java.lang.String folder)
        Sets the folder where to generate example using IVML models.
        Parameters:
        folder - the folder name (by default "src/test/easy")
      • createIvmlModel

        private void createIvmlModel​(java.lang.String fileName,
                                     java.io.File ivmlFile)
        Creates the IVML model in the given fileName.
        Parameters:
        fileName - the file name for the IVML model
        ivmlFile - the output file
      • println

        private void println​(java.lang.String text)
        Prints out information in verbose mode.
        Parameters:
        text - the text to print
      • process

        public static void process​(java.io.File xmlIn,
                                   java.lang.String outName,
                                   java.io.File ivmlOut,
                                   boolean verbose)
        Processes an OPC XML file. [public for testing]
        Parameters:
        xmlIn - the input file
        outName - the output file/model name
        ivmlOut - the full output file name
        verbose - verbose output
      • main

        public static void main​(java.lang.String[] args)
        Executes the parser, per default in verbose mode.
        Parameters:
        args - command line arguments (ignored)