Package javax.xml.parsers

Examples of javax.xml.parsers.DocumentBuilder


    Iterator<String> it = xmlFileList.iterator();
    while(it.hasNext()){
      String fileName = it.next();
      try {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse (new File(fileName));
        System.out.println("Retrieving Metric List from xml file: "+fileName);
        // normalize text representation
        doc.getDocumentElement ().normalize ();

        NodeList metrics = doc.getElementsByTagName("Metric");


          String fileName = fileIt.next();
          count++;
          try{
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse (new File(fileName));
            System.out.println("Gethering metric info from from xml file: "+fileName);
            // normalize text representation
   
     
            if(!CSV){
              //print the name of the xml file as the name of the benchmark
              if(fileName.endsWith(".xml"))
                bench.print(fileName.substring(0,fileName.length()-4));
              else
                bench.print(fileName);
            }
     
           
            HashMap<String, Number> aggregatedValues = new HashMap<String, Number>();
       
           
            //TODO Should compute all metrics always
            //only print out the one we want
            Iterator<String> tempIt = allMetrics.iterator();
            while(tempIt.hasNext()){
              aggregatedValues.put(tempIt.next(),new Integer(0));
            }
         
            int numClasses = aggregateXMLFileMetrics(doc,aggregatedValues);
     
            //at this point the hashmap contains aggregatedValue of all metrics
           
           
           
           
           
            //get the metrics we might need to divide
            Object myTemp = aggregatedValues.get("Total-Conditionals");
            if(myTemp == null){
              System.out.println("Total-Conditionals not found in aggregatedValues");
              System.exit(1)
            }
            double total_if_ifelse = ((Integer)myTemp).doubleValue();
           
           
            myTemp = aggregatedValues.get("Total Loops");
            if(myTemp == null){
              System.out.println("Total Loops not found in aggregatedValues");
              System.exit(1)
            }
            double totalLoops = ((Integer)myTemp).doubleValue();
           
            double totalConditional = total_if_ifelse+totalLoops;
           
            myTemp = aggregatedValues.get("AST-Node-Count");
            if(myTemp == null){
              System.out.println("AST-Node-Count not found in aggregatedValues");
              System.exit(1)
            }
            double astCount = ((Integer)myTemp).doubleValue();

           
           
           
           
            myTemp = aggregatedValues.get("NameCount");
            if(myTemp == null){
              System.out.println("NameCount not found in aggregatedValues");
              System.exit(1)
            }
            double nameCount = ((Double)myTemp).doubleValue();

           
           
           
            myTemp = aggregatedValues.get("Expr-Count");
            if(myTemp == null){
              System.out.println("ExprCount not found in aggregatedValues");
              System.exit(1)
            }
            double exprCount = ((Double)myTemp).doubleValue();

           
            tempIt = columns.iterator();
            while(tempIt.hasNext()){
              String nexttempit = tempIt.next();
              Object temp = aggregatedValues.get(nexttempit);
              //System.out.println("NEXT TEMP IT ISSSSSSSSSSSSSSSSSSSSSS"+nexttempit);
              if(temp instanceof Integer){
                int val = ((Integer)temp).intValue();
                if(CSV){
                  switch(count){
                  case 0://original
                      bench.print(fileName.substring(0,fileName.indexOf('-')));
                  case 1:
                  case 2:
                  case 3:
                  case 4:
                    if(nexttempit.equals("Total-Abrupt")){
                      //no averaging
                      bench.print(","+val);
                    }
                    else if(nexttempit.equals("Total-Cond-Complexity")){
                      if(totalConditional !=0 ){
                        //average by dividing total-cond-complexity for sum of if+ifelse+loops
                        System.out.println("conditional complexit is"+val);
                        System.out.println("totalConditionals are"+totalConditional);

                        bench.print(","+val/totalConditional);
                      }
                      else if (val ==0)
                        bench.print(","+val);
                      else{
                        //val not 0 but toalconds are 0...not good
                        System.out.println("Val not 0 but totalConditionals are zero!!!");
                        System.exit(1);
                      }
                       
                    }
                    else if(nexttempit.equals("D-W-Complexity")){
                      if(astCount !=0 ){
                        //average by dividing D-W-Complexity by node count
                        bench.print(","+val/astCount);
                      }
                      else if (val ==0)
                        bench.print(","+val);
                      else{
                        //val not 0 but astcount is 0...not good
                        System.out.println("Val not 0 but astcount is zero!!!");
                        System.exit(1);
                      }
                       
                    }               
                    else if(nexttempit.equals("Expr-Complexity")){

                      if(exprCount !=0 ){
                        //average by dividing expr-complexity for exprCount
                        bench.print(","+val/exprCount);
                      }
                      else if (val ==0)
                        bench.print(","+val);
                      else{
                        //val not 0 but expr-count are 0...not good
                        System.out.println("Val not 0 but exprcount is zero!!!");
                        System.exit(1);
                      }

                    }
                    else if(nexttempit.equals("Name-Complexity")){

                      if(nameCount !=0 ){
                        //average by dividing name-complexity for nameCount
                        bench.print(","+val/nameCount);
                      }
                      else if (val ==0)
                        bench.print(","+val);
                      else{
                        //val not 0 but name-count are 0...not good
                        System.out.println("Val not 0 but name-count is zero!!!");
                        System.exit(1);
                      }
                    }
                    else{
                      //labeled blocks, locals, if-ifelse, ASTNodeCount
                      bench.print(","+val);
                    }
                    break;
                  default:
                    System.out.println("unhandled count value");
                    System.exit(1);
                  }

                }
                else{
                  //not CSV
                  bench.print("&"+val);
                }
              }
              else if(temp instanceof Double){
                double val = ((Double)temp).doubleValue();
                if(CSV){
                  switch(count){
                  case 0://original
                      bench.print(fileName.substring(0,fileName.indexOf('-')));
                  case 1:
                  case 2:
                  case 3:
                  case 4:
                    if(nexttempit.equals("Total-Abrupt")){
                      //no averaging
                      bench.print(","+val);
                    }
                    else if(nexttempit.equals("Total-Cond-Complexity")){
                      if(totalConditional !=0 ){
                        //average by dividing total-cond-complexity for sum of if+ifelse+loops
                        System.out.println("conditional complexit is"+val);
                        System.out.println("totalConditionals are"+totalConditional);
                        bench.print(","+val/totalConditional);
                      }
                      else if (val ==0)
                        bench.print(","+val);
                      else{
                        //val not 0 but toalconds are 0...not good
                        System.out.println("Val not 0 but totalConditionals are zero!!!");
                        System.exit(1);
                      }
                       
                    }
                    else if(nexttempit.equals("D-W-Complexity")){
                      if(astCount !=0 ){
                        //average by dividing D-W-Complexity by node count
                        bench.print(","+val/astCount);
                      }
                      else if (val ==0)
                        bench.print(","+val);
                      else{
                        //val not 0 but astcount is 0...not good
                        System.out.println("Val not 0 but astcount is zero!!!");
                        System.exit(1);
                      }
                       
                    }               
                    else if(nexttempit.equals("Expr-Complexity")){

                      if(exprCount !=0 ){
                        //average by dividing expr-complexity for exprCount
                        bench.print(","+val/exprCount);
                      }
                      else if (val ==0)
                        bench.print(","+val);
                      else{
                        //val not 0 but expr-count are 0...not good
                        System.out.println("Val not 0 but exprcount is zero!!!");
                        System.exit(1);
                      }

                    }
                    else if(nexttempit.equals("Name-Complexity")){

                      if(nameCount !=0 ){
                        //average by dividing name-complexity for nameCount
                        bench.print(","+val/nameCount);
                      }
                      else if (val ==0)
                        bench.print(","+val);
                      else{
                        //val not 0 but name-count are 0...not good
                        System.out.println("Val not 0 but name-count is zero!!!");
                        System.exit(1);
                      }
                    }
                    else{
                      //labeled blocks, locals, if-ifelse, ASTNodeCount
                      bench.print(","+val);
                    }
                    break;
                  default:
                    System.out.println("unhandled count value");
                    System.exit(1);
                  }

                }
                else
                  bench.print("&"+val);
              }
              else
                throw new RuntimeException("Unknown type of object stored!!!");
              if(CSV){
                if(tempIt.hasNext()){
                  System.out.println("Only allowed one metric for CSV");
                  System.exit(1);
                }               
              }
              else{
                if(tempIt.hasNext())
                  bench.print("   ");
                else
                  bench.println("\\\\");
              }
            }

          }catch (SAXParseException err) {
            System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
            System.out.println(" " + err.getMessage ());
          }
          catch (SAXException e) {
            Exception x = e.getException ();
            ((x == null) ? e : x).printStackTrace ();
          }
          catch (Throwable t) {
            t.printStackTrace ();
          }
        }//done with all files for this benchmark
       
        //print closing for the table for this benchmark
        if(CSV)
          bench.println("");
        else
          printTexTableFooter(bench,"");
      }//done with all benchmarks

      closeWriteFile(bench,newClassName);


    }
    else{   
   
   
      Iterator<String> it = xmlFileList.iterator();
      while(it.hasNext()){
        String fileName = it.next();

        try{
          DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

          DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
          Document doc = docBuilder.parse (new File(fileName));
          System.out.println("Gethering metric info from from xml file: "+fileName);
          // normalize text representation
          doc.getDocumentElement ().normalize ();

         

    boolean defaultEnabled = false;
    try {
      DocumentBuilderFactory factory = DocumentBuilderFactory
          .newInstance();
      factory.setNamespaceAware(true);
      DocumentBuilder domBldr = factory.newDocumentBuilder();
      Document doc = domBldr.parse(url);
      if (doc != null) {
        Element root = doc.getDocumentElement();
        NodeList nl = root.getElementsByTagName(ELM_SHORT_NAME);
        if (nl != null && nl.getLength() > 0) {
          sname = nl.item(0).getFirstChild().getNodeValue();

            if (localXSD25 != null) {
                factory.setAttribute(SCHEMA_SOURCE_PROPERTY, localXSD25.toString());
            } else {
                factory.setAttribute(SCHEMA_SOURCE_PROPERTY, null);
            }
            DocumentBuilder builder = factory.newDocumentBuilder();
            builder.setEntityResolver(this);
            builder.setErrorHandler(this);
            this.rethrowValidationExceptions = true;
            return builder.parse(webXmlFile);
        } catch (Throwable errV25) {
            try {
                // Try as 2.4
                if (localXSD24 != null) {
                    factory.setAttribute(SCHEMA_SOURCE_PROPERTY, localXSD24.toString());
                } else {
                    factory.setAttribute(SCHEMA_SOURCE_PROPERTY, null);
                }
                DocumentBuilder builder = factory.newDocumentBuilder();
                builder.setEntityResolver(this);
                builder.setErrorHandler(this);
                this.rethrowValidationExceptions = true;
                return builder.parse(webXmlFile);
            } catch (Throwable errV24) {
                // Try parsing as a v2.3 spec webapp, and if another error happens, report 2.3, 2.4, 2.5
                try {
                    this.rethrowValidationExceptions = false;
                    return parseAsV23Webapp(webXmlFile);

    }
   
    private Document parseAsV23Webapp(File webXmlFile) throws ParserConfigurationException,
            SAXException, IOException {
        DocumentBuilderFactory factory = getBaseDBF();
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(this);
        builder.setErrorHandler(this);
        return builder.parse(webXmlFile);
    }

            factory.setValidating(false);
            factory.setNamespaceAware(false);
            factory.setIgnoringComments(true);
            factory.setCoalescing(true);
            factory.setIgnoringElementContentWhitespace(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            return builder.parse(in);
        } catch (Throwable errParser) {
            throw new WinstoneException(REALM_RESOURCES
                    .getString("FileRealm.XMLParseError"), errParser);
        }
    }

            schema = (Schema)extensionRegistry.createExtension(Types.class,
                                                               new QName("http://www.w3.org/2001/XMLSchema",
                                                                         "schema"));

            DocumentBuilder docBuilder;
            docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = docBuilder.newDocument();
            Element element = doc.createElement("xsd:schema");
            Map<String, String> schemafileMap = wmodel.getSchemaNSFileMap();

            Set<Map.Entry<String, String>> entryset = schemafileMap.entrySet();
            Iterator<Map.Entry<String, String>> ite = entryset.iterator();

        Document document = null;
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder parser = factory.newDocumentBuilder();
            document = parser.parse(is);
        } catch (ParserConfigurationException ex) {
            throw new ConfigurationException(new Message("PARSER_CONFIGURATION_ERROR_EXC",
                                                         LOG, location), ex);
        } catch (SAXException ex) {
            throw new ConfigurationException(new Message("PARSE_ERROR_EXC", LOG), ex);

        // parse
        Document document = null;
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder parser = factory.newDocumentBuilder();
            document = parser.parse(is);
        } catch (ParserConfigurationException ex) {
            throw new ConfigurationException(new Message("PARSER_CONFIGURATION_ERROR_EXC", LOG), ex);
        } catch (SAXException ex) {
            throw new ConfigurationException(new Message("PARSE_ERROR_EXC", LOG), ex);
        }

     */
    private Document urlToDocument(String url)
        throws Exception
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(new XslTransformerEntityResolver(url));
        return builder.parse(new InputSource(url));
    }

TOP

Related Classes of javax.xml.parsers.DocumentBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.