Package javax.xml.parsers

Examples of javax.xml.parsers.DocumentBuilderFactory


    return parser.getXMLReader();
  }

  public static org.w3c.dom.Document createDocument(boolean validating,
                                                    boolean namespaceAware) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(validating);
    factory.setNamespaceAware(namespaceAware);

    DocumentBuilder builder = factory.newDocumentBuilder();

    return builder.newDocument();
  }
View Full Code Here


      throws FactoryConfigurationError {
      if (factoryName == null || factoryName.length() < 1) {
         return newDocumentBuilderFactory();
      }
      try {
         DocumentBuilderFactory dbf = (DocumentBuilderFactory) factory.getClass().getClassLoader().loadClass(factoryName).newInstance();
         return dbf;
      } catch (Exception e) {
         throw new FactoryConfigurationError(e,e.getMessage());
      }
   }
View Full Code Here

      //input.setEncoding("ISO-8859-2");
      //input.setSystemId("9999999999");
      final String ME = "DOMParser";
      if (glob == null) glob = Global.instance();
      try {
         DocumentBuilderFactory dbf = glob.getDocumentBuilderFactory();
         DocumentBuilder db = dbf.newDocumentBuilder();
         return db.parse(input);
      } catch (javax.xml.parsers.ParserConfigurationException e) {
         log.severe("Problems when building DOM parser: " + e.toString() + "\n" + xmlKey_literal);
         throw new XmlBlasterException(glob, ErrorCode.RESOURCE_CONFIGURATION, ME, "Problems when building DOM tree from your XML-ASCII string\n" + xmlKey_literal, e);
      } catch (java.io.IOException e) {
View Full Code Here

    }

    try
    // to cast to XException
    {
      DocumentBuilderFactory factory = DocumentBuilderFactory
          .newInstance();
      factory
          .setIgnoringComments(getIgnoringComments(messageName,
              system));
      factory
          .setIgnoringElementContentWhitespace(getIgnoringElementContentWhitespace(
              messageName, system));
      factory.setValidating(getValidating(messageName, system));
      factory.setNamespaceAware(getNamespaceAware(messageName, system));

      docBuilder = factory.newDocumentBuilder();
      docBuilder.setErrorHandler(new XParserErrorHandler());
    } // try
    catch (ParserConfigurationException e)
    {
      throw new XException(Constants.LOCATION_INTERN,
View Full Code Here

   
    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 ();
View Full Code Here

        int count=-1;
        while(fileIt.hasNext()){
          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 ();

View Full Code Here

    String urlpttn = null;
    String type = null;
    boolean loginRequired = false;
    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) {
View Full Code Here

     * Get a parsed XML DOM from the given inputstream. Used to process the
     * web.xml application deployment descriptors. Returns null if the parse fails,
     * so the effect is as if there was no web.xml file available.
     */
    protected Document parseStreamToXML(File webXmlFile) {
        DocumentBuilderFactory factory = getBaseDBF();
       
        URL localXSD25 = this.commonLoader.getResource(LOCAL_ENTITY_TABLE[3][2]);
        URL localXSD24 = this.commonLoader.getResource(LOCAL_ENTITY_TABLE[2][2]);
       
        // Test for XSD compliance
        try {
            factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                    "http://www.w3.org/2001/XMLSchema");
            if (localXSD25 != null) {
                factory.setAttribute(SCHEMA_SOURCE_PROPERTY, localXSD25.toString());
                Logger.log(Logger.FULL_DEBUG, Launcher.RESOURCES, "WebXmlParser.Local25XSDEnabled");
            } else if (localXSD24 != null) {
                factory.setAttribute(SCHEMA_SOURCE_PROPERTY, localXSD24.toString());
                Logger.log(Logger.FULL_DEBUG, Launcher.RESOURCES, "WebXmlParser.Local24XSDEnabled");
            } else {
                Logger.log(Logger.WARNING, Launcher.RESOURCES, "WebXmlParser.2524XSDNotFound");
            }
        } catch (Throwable err) {
            // if non-compliant parser, then parse as non-XSD compliant
            Logger.log(Logger.WARNING, Launcher.RESOURCES, "WebXmlParser.NonXSDParser");
            try {
                this.rethrowValidationExceptions = false;
                return parseAsV23Webapp(webXmlFile);
            } catch (Throwable v23Err) {
                Logger.log(Logger.ERROR, Launcher.RESOURCES, "WebXmlParser.WebXML23ParseError", v23Err);
                return null;
            }
        }
       
        // XSD compliant parser available, so parse as 2.5
        try {
            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) {
View Full Code Here

        }
    }
   
    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);
    }
View Full Code Here

        return builder.parse(webXmlFile);
    }
   
    private DocumentBuilderFactory getBaseDBF() {
        // Use JAXP to create a document builder
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setExpandEntityReferences(false);
        factory.setValidating(true);
        factory.setNamespaceAware(true);
        factory.setIgnoringComments(true);
        factory.setCoalescing(true);
        factory.setIgnoringElementContentWhitespace(true);
        return factory;
    }
View Full Code Here

TOP

Related Classes of javax.xml.parsers.DocumentBuilderFactory

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.