Changeset 567


Ignore:
Timestamp:
11/07/12 11:17:40 (12 years ago)
Author:
gosiaw
Message:

Some refactoring done

File:
1 edited

Legend:

Unmodified
Added
Removed
  • DCWoRMS/trunk/src/test/DEBBTranslator/src/DEBB2DCWoRMSTranslator.java

    r566 r567  
    4242        // 3) 
    4343 
    44         private static final String DEFAULT_OUTPUT_FILE_NAME = "xml/DCWORMS_TEST.xml"; 
    45 //      private static final String DEFAULT_INPUT_FILE_NAME = "xml/PLMXML_PSNCRECS.xml"; 
    46         private static final String DEFAULT_INPUT_FILE_NAME = "xml/PLMXML_HLRSSmallServerRoom.xml"; 
     44        private static final String DEFAULT_DCWORMS_OUTPUT_FILE_NAME = "xml/DCWORMS_TEST.xml"; 
     45        // private static final String DEFAULT_INPUT_FILE_NAME = 
     46        // "xml/PLMXML_PSNCRECS.xml"; 
     47        private static final String DEFAULT_PLMXML_INPUT_FILE_NAME = "xml/PLMXML_HLRSSmallServerRoom.xml"; 
     48        private static final String DEFAULT_DEBB_COMPONENT_INPUT_FILE_NAME = "xml/DEBBComponents.xml"; 
    4749        private static DocumentBuilder xmlDocumentBuilder; 
    4850        private static DocumentBuilderFactory domFactory; 
     
    6163         */ 
    6264        public static void main(String[] args) { 
    63                 String inputFileName = DEFAULT_INPUT_FILE_NAME; 
    64                 String outputFileName = DEFAULT_OUTPUT_FILE_NAME; 
     65                String inputFileName = DEFAULT_PLMXML_INPUT_FILE_NAME; 
     66                String outputFileName = DEFAULT_DCWORMS_OUTPUT_FILE_NAME; 
    6567 
    6668                if (args != null && args.length > 0) { 
    6769                        inputFileName = args[0]; 
    6870                        if (inputFileName != null && inputFileName.isEmpty() == false) { 
    69                                 inputFileName = DEFAULT_INPUT_FILE_NAME; 
     71                                inputFileName = DEFAULT_PLMXML_INPUT_FILE_NAME; 
    7072                        } 
    7173 
    7274                        outputFileName = args[1]; 
    7375                        if (outputFileName != null && outputFileName.isEmpty() == false) { 
    74                                 outputFileName = DEFAULT_OUTPUT_FILE_NAME; 
     76                                outputFileName = DEFAULT_DCWORMS_OUTPUT_FILE_NAME; 
    7577                        } 
    7678                } 
     
    8082 
    8183                        // Open and read content of the input file (i.e. PLM XML document) 
    82                         Document plmxmlDocument = readInputFile(inputFileName); 
     84                        Document plmxmlDocument = readPLMXMLInputFile(inputFileName); 
    8385 
    8486                        XPathFactory xpathFactory = XPathFactory.newInstance(); 
     
    9193                                        .compile("//plm:ProductDef/plm:InstanceGraph/@rootRefs"); 
    9294                        Object result = expr.evaluate(plmxmlDocument, XPathConstants.NODE); 
    93                         Node rootId = (Node) result; 
    94                         System.out.println(rootId.getNodeName()); 
    95                         System.out.println(rootId.getNodeValue()); 
     95                        Node rootIdNode = (Node) result; 
     96                        System.out.println(rootIdNode.getNodeName()); 
     97                        System.out.println(rootIdNode.getNodeValue()); 
    9698 
    9799                        // Creating destination, dcworms document 
     
    109111                        rootElement.appendChild(resourcesElement); 
    110112 
    111                         //***************************************************************** 
     113                        // ***************************************************************** 
    112114                        // Creating (main) scheduler element 
    113115                        // TODO: Fill scheduler with reasonable data! 
     
    118120                        resourcesElement.appendChild(schedulerElement); 
    119121 
    120                         //***************************************************************** 
     122                        // ***************************************************************** 
    121123                        // Transform top level DEBB and all nested DEBBs 
    122124                        transformDEBB(plmxmlDocument, dcwormsDocument, resourcesElement, 
    123                                         xpath, rootId.getNodeValue()); 
     125                                        xpath, rootIdNode.getNodeValue()); 
    124126 
    125127                        // write the content into xml file 
    126                         saveOutputFile(outputFileName, dcwormsDocument); 
     128                        saveDCWoRMSOutputFile(outputFileName, dcwormsDocument); 
    127129 
    128130                } catch (ParserConfigurationException e) { 
     
    165167        } 
    166168 
    167         private static Document readInputFile(String inputFileName) { 
     169        private static Document readPLMXMLInputFile(String inputFileName) { 
    168170                Document plmxmlDocument; 
    169171                try { 
     
    180182        } 
    181183 
    182         private static void saveOutputFile(String outputFileName, 
     184        private static Document readDEBBComponentInputFile(String inputFileName) { 
     185                Document debbComponentDocument; 
     186                try { 
     187                        debbComponentDocument = xmlDocumentBuilder.parse(inputFileName); 
     188                        return debbComponentDocument; 
     189                } catch (SAXException e) { 
     190                        // TODO Auto-generated catch block 
     191                        e.printStackTrace(); 
     192                } catch (IOException e) { 
     193                        // TODO Auto-generated catch block 
     194                        e.printStackTrace(); 
     195                } 
     196                return null; 
     197        } 
     198 
     199        private static void saveDCWoRMSOutputFile(String outputFileName, 
    183200                        Document dcwormsDocument) { 
    184201 
     
    238255 
    239256                // Find DEBB with given id and transform it 
    240                 //***************************************************************** 
     257                // ***************************************************************** 
    241258                // 1) ProductInstance - 
    242259                XPathExpression expr = xpath 
     
    244261                                                + instanceId + "']"); 
    245262                Object result = expr.evaluate(plmxmlDocument, XPathConstants.NODE); 
    246                 Node productInstance = (Node) result; 
    247                 Node debbName = productInstance.getAttributes().getNamedItem("name"); 
    248                 System.out.println("name=" + debbName.getNodeValue()); 
    249                 Node partRef = productInstance.getAttributes().getNamedItem("partRef"); 
    250                 System.out.println("partRef=" + partRef.getNodeValue()); 
    251                 String partId = partRef.getNodeValue().substring(1); // Get rid of # at 
    252                                                                                                                                 // the beginning 
     263                Node productInstanceNode = (Node) result; 
     264                Node debbNameNode = productInstanceNode.getAttributes().getNamedItem( 
     265                                "name"); 
     266                System.out.println("name=" + debbNameNode.getNodeValue()); 
     267                Node partRefNode = productInstanceNode.getAttributes().getNamedItem( 
     268                                "partRef"); 
     269                System.out.println("partRef=" + partRefNode.getNodeValue()); 
     270                String partId = partRefNode.getNodeValue().substring(1); // Get rid of # 
     271                                                                                                                                        // at 
     272                                                                                                                                        // the 
     273                                                                                                                                        // beginning 
    253274                // UserData - hostname 
    254275                expr = xpath 
    255276                                .compile("plm:UserData/plm:UserValue[@title='hostname']/@value"); 
    256                 result = expr.evaluate(productInstance, XPathConstants.NODE); 
    257                 Node debbHostName = (Node) result; 
    258  
    259                 //      UserData - sequence 
     277                result = expr.evaluate(productInstanceNode, XPathConstants.NODE); 
     278                Node debbHostNameNode = (Node) result; 
     279 
     280                // UserData - sequence 
    260281                expr = xpath 
    261282                                .compile("plm:UserData/plm:UserValue[@title='sequence']/@value"); 
    262                 result = expr.evaluate(productInstance, XPathConstants.NODE); 
    263                 Node debbSequence = (Node) result; 
    264  
    265                 //      UserData - label 
     283                result = expr.evaluate(productInstanceNode, XPathConstants.NODE); 
     284                Node debbSequenceNode = (Node) result; 
     285 
     286                // UserData - label 
    266287                expr = xpath 
    267288                                .compile("plm:UserData/plm:UserValue[@title='label']/@value"); 
    268                 result = expr.evaluate(productInstance, XPathConstants.NODE); 
    269                 Node debbLabel = (Node) result; 
    270  
    271                 //      UserData - location 
     289                result = expr.evaluate(productInstanceNode, XPathConstants.NODE); 
     290                Node debbLabelNode = (Node) result; 
     291 
     292                // UserData - location 
    272293                expr = xpath 
    273294                                .compile("plm:UserData/plm:UserValue[@title='location']/@value"); 
    274                 result = expr.evaluate(productInstance, XPathConstants.NODE); 
    275                 Node debbLocation = (Node) result; 
    276                  
    277                 //***************************************************************** 
     295                result = expr.evaluate(productInstanceNode, XPathConstants.NODE); 
     296                Node debbLocationNode = (Node) result; 
     297 
     298                // ***************************************************************** 
    278299                // 2) ProductRevisionView 
    279300                expr = xpath 
     
    281302                                                + partId + "']"); 
    282303                result = expr.evaluate(plmxmlDocument, XPathConstants.NODE); 
    283                 Node productRevisionView = (Node) result; 
     304                Node productRevisionViewNode = (Node) result; 
    284305                // debbName = productRevisionView.getAttributes().getNamedItem("name"); 
    285306                // System.out.println("name=" + debbName.getNodeValue()); 
     
    289310                expr = xpath 
    290311                                .compile("plm:UserData/plm:UserValue[@title='DEBBComponentId']/@value"); 
    291                 result = expr.evaluate(productRevisionView, XPathConstants.NODE); 
    292                 Node debbComponentId = (Node) result; 
    293                 if (debbComponentId != null) { 
     312                result = expr.evaluate(productRevisionViewNode, XPathConstants.NODE); 
     313                Node debbComponentIdNode = (Node) result; 
     314                if (debbComponentIdNode != null) { 
    294315                        System.out.println("debbComponentId=" 
    295                                         + debbComponentId.getNodeValue()); 
     316                                        + debbComponentIdNode.getNodeValue()); 
    296317                } 
    297318 
     
    299320                expr = xpath 
    300321                                .compile("plm:UserData/plm:UserValue[@title='DEBBLevel']/@value"); 
    301                 result = expr.evaluate(productRevisionView, XPathConstants.NODE); 
    302                 Node debbLevel = (Node) result; 
    303                 if (debbLevel != null) { 
    304                         System.out.println("debbLevel=" + debbLevel.getNodeValue()); 
     322                result = expr.evaluate(productRevisionViewNode, XPathConstants.NODE); 
     323                Node debbLevelNode = (Node) result; 
     324                if (debbLevelNode != null) { 
     325                        System.out.println("debbLevel=" + debbLevelNode.getNodeValue()); 
    305326                } 
    306327 
     
    308329                expr = xpath 
    309330                                .compile("plm:UserData/plm:UserValue[@title='DEBBComponentFile']/@value"); 
    310                 result = expr.evaluate(productRevisionView, XPathConstants.NODE); 
    311                 Node debbComponentFileName = (Node) result; 
    312                 if (debbComponentFileName == null) { 
     331                result = expr.evaluate(productRevisionViewNode, XPathConstants.NODE); 
     332                Node debbComponentFileNameNode = (Node) result; 
     333                String debbComponentFileName = null; 
     334                if (debbComponentFileNameNode != null) { 
    313335                        // Default DEBBComponent file is assumed 
    314  
    315                 } 
    316                 System.out.println("debbComponentFileName=" + debbComponentFileName); 
    317  
    318                 //***************************************************************** 
     336                        debbComponentFileName = debbComponentFileNameNode.getNodeValue(); 
     337                } else { 
     338                        debbComponentFileName = DEFAULT_DEBB_COMPONENT_INPUT_FILE_NAME; 
     339                } 
     340                System.out.println("debbComponentFileToOpen=" + debbComponentFileName); 
     341 
     342                // ***************************************************************** 
    319343                // Add computingResource element for the DEBB 
    320                 if (debbLevel != null) { 
     344                if (debbLevelNode != null) { 
    321345                        Element computingResourceElement = dcwormsDocument 
    322346                                        .createElement("computingResource"); 
    323347                        Attr nameAttribute = dcwormsDocument.createAttribute("name"); 
    324                         nameAttribute.setValue(debbName.getNodeValue()); 
     348                        nameAttribute.setValue(debbNameNode.getNodeValue()); 
    325349                        computingResourceElement.setAttributeNode(nameAttribute); 
    326350 
    327351                        Attr classAttribute = dcwormsDocument.createAttribute("class"); 
    328                         String dcwormsClass = computingResourceClasses.get(debbLevel 
     352                        String dcwormsClass = computingResourceClasses.get(debbLevelNode 
    329353                                        .getNodeValue()); 
    330354                        classAttribute.setValue(dcwormsClass); 
    331355                        computingResourceElement.setAttributeNode(classAttribute); 
    332356 
    333                         if (debbHostName != null) {                              
    334                                 Element parameterElement =dcwormsDocument.createElement("parameter"); 
    335                                 Attr parameterNameAttribute = dcwormsDocument.createAttribute("name"); 
     357                        if (debbHostNameNode != null) { 
     358                                Element parameterElement = dcwormsDocument 
     359                                                .createElement("parameter"); 
     360                                Attr parameterNameAttribute = dcwormsDocument 
     361                                                .createAttribute("name"); 
    336362                                parameterNameAttribute.setValue("hostname"); 
    337363                                parameterElement.setAttributeNode(parameterNameAttribute); 
    338364                                Element valueElement = dcwormsDocument.createElement("value"); 
    339                                 valueElement.appendChild(dcwormsDocument.createTextNode(debbHostName.getNodeValue())); 
     365                                valueElement.appendChild(dcwormsDocument 
     366                                                .createTextNode(debbHostNameNode.getNodeValue())); 
    340367                                parameterElement.appendChild(valueElement); 
    341368                                computingResourceElement.appendChild(parameterElement); 
    342369                        } 
    343370 
    344                         if (debbSequence != null) { 
     371                        if (debbSequenceNode != null) { 
    345372                                // TODO: Sequence is described unclearly in the deliverable, not 
    346373                                // used in any example. Not sure what to do with it. 
    347                                 Element parameterElement =dcwormsDocument.createElement("parameter"); 
    348                                 Attr parameterNameAttribute = dcwormsDocument.createAttribute("name"); 
     374                                Element parameterElement = dcwormsDocument 
     375                                                .createElement("parameter"); 
     376                                Attr parameterNameAttribute = dcwormsDocument 
     377                                                .createAttribute("name"); 
    349378                                parameterNameAttribute.setValue("sequence"); 
    350379                                parameterElement.setAttributeNode(parameterNameAttribute); 
    351380                                Element valueElement = dcwormsDocument.createElement("value"); 
    352                                 valueElement.appendChild(dcwormsDocument.createTextNode(debbSequence.getNodeValue())); 
     381                                valueElement.appendChild(dcwormsDocument 
     382                                                .createTextNode(debbSequenceNode.getNodeValue())); 
    353383                                parameterElement.appendChild(valueElement); 
    354                                 computingResourceElement.appendChild(parameterElement);                          
     384                                computingResourceElement.appendChild(parameterElement); 
    355385                        } 
    356386                        debbParentElement.appendChild(computingResourceElement); 
    357387 
    358                         if (debbLabel != null) {                                 
    359                                 Element parameterElement =dcwormsDocument.createElement("parameter"); 
    360                                 Attr parameterNameAttribute = dcwormsDocument.createAttribute("name"); 
     388                        if (debbLabelNode != null) { 
     389                                Element parameterElement = dcwormsDocument 
     390                                                .createElement("parameter"); 
     391                                Attr parameterNameAttribute = dcwormsDocument 
     392                                                .createAttribute("name"); 
    361393                                parameterNameAttribute.setValue("label"); 
    362394                                parameterElement.setAttributeNode(parameterNameAttribute); 
    363395                                Element valueElement = dcwormsDocument.createElement("value"); 
    364                                 valueElement.appendChild(dcwormsDocument.createTextNode(debbLabel.getNodeValue())); 
     396                                valueElement.appendChild(dcwormsDocument 
     397                                                .createTextNode(debbLabelNode.getNodeValue())); 
    365398                                parameterElement.appendChild(valueElement); 
    366399                                computingResourceElement.appendChild(parameterElement); 
    367                                  
    368                         } 
    369  
    370                         if (debbLocation != null) { 
    371                                 Element parameterElement =dcwormsDocument.createElement("parameter"); 
    372                                 Attr parameterNameAttribute = dcwormsDocument.createAttribute("name"); 
     400 
     401                        } 
     402 
     403                        if (debbLocationNode != null) { 
     404                                Element parameterElement = dcwormsDocument 
     405                                                .createElement("parameter"); 
     406                                Attr parameterNameAttribute = dcwormsDocument 
     407                                                .createAttribute("name"); 
    373408                                parameterNameAttribute.setValue("location"); 
    374409                                parameterElement.setAttributeNode(parameterNameAttribute); 
    375410                                Element valueElement = dcwormsDocument.createElement("value"); 
    376                                 valueElement.appendChild(dcwormsDocument.createTextNode(debbLocation.getNodeValue())); 
     411                                valueElement.appendChild(dcwormsDocument 
     412                                                .createTextNode(debbLocationNode.getNodeValue())); 
    377413                                parameterElement.appendChild(valueElement); 
    378414                                computingResourceElement.appendChild(parameterElement); 
    379                                  
    380                         } 
    381                          
    382                         //***************************************************************** 
     415 
     416                        } 
     417 
     418                        // ***************************************************************** 
     419                        // Read data from DEBBComponent file(s) 
     420                        Document debbComponentDocument = readDEBBComponentInputFile(debbComponentFileName); 
     421 
     422                        // Find component 
     423 
     424                        // ***************************************************************** 
    383425                        // Get nested instances - nested DEBBs and ask them to transform 
    384426                        // themselves 
    385                         Node instanceRefs = productRevisionView.getAttributes() 
     427                        Node instanceRefsNode = productRevisionViewNode.getAttributes() 
    386428                                        .getNamedItem("instanceRefs"); 
    387429                        // Check if anything is nested at all 
    388                         if (instanceRefs != null) { 
    389                                 String instances = instanceRefs.getNodeValue(); 
     430                        if (instanceRefsNode != null) { 
     431                                String instances = instanceRefsNode.getNodeValue(); 
    390432                                System.out.println("instances=" + instances); 
    391433                                // Separate instance ids 
Note: See TracChangeset for help on using the changeset viewer.