Coverage Report - org.xmlhammer.gui.stylesheetvalidator.StylesheetValidatorModule
 
Classes in this File Line Coverage Branch Coverage Complexity
StylesheetValidatorModule
86% 
92% 
0
 
 1  
 /*
 2  
  * $Id$
 3  
  *
 4  
  * The contents of this file are subject to the Mozilla Public License 
 5  
  * Version 1.1 (the "License"); you may not use this file except in 
 6  
  * compliance with the License. You may obtain a copy of the License at 
 7  
  * http://www.mozilla.org/MPL/ 
 8  
  *
 9  
  * Software distributed under the License is distributed on an "AS IS" basis, 
 10  
  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
 11  
  * for the specific language governing rights and limitations under the License.
 12  
  *
 13  
  * The Original Code is XML Hammer code. (org.xmlhammer.*)
 14  
  *
 15  
  * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
 16  
  * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
 17  
  * All Rights Reserved.
 18  
  *
 19  
  * Contributor(s): Edwin Dankert <edankert@gmail.com>
 20  
  */
 21  
 
 22  
 package org.xmlhammer.gui.stylesheetvalidator;
 23  
 
 24  
 import java.io.IOException;
 25  
 import java.net.URI;
 26  
 import java.util.ArrayList;
 27  
 
 28  
 import javax.xml.parsers.ParserConfigurationException;
 29  
 import javax.xml.transform.TransformerException;
 30  
 import javax.xml.transform.TransformerFactory;
 31  
 import javax.xml.transform.stream.StreamSource;
 32  
 
 33  
 import org.apache.log4j.Logger;
 34  
 import org.bounce.util.URIUtils;
 35  
 import org.xml.sax.SAXException;
 36  
 import org.xmlhammer.DefaultErrorHandler;
 37  
 import org.xmlhammer.Module;
 38  
 import org.xmlhammer.ResultModel;
 39  
 import org.xmlhammer.DefaultErrorHandler.Error;
 40  
 import org.xmlhammer.DefaultErrorHandler.Fatal;
 41  
 import org.xmlhammer.DefaultErrorHandler.Problem;
 42  
 import org.xmlhammer.DefaultErrorHandler.Warning;
 43  
 import org.xmlhammer.gui.status.StatusModel;
 44  
 import org.xmlhammer.gui.status.ValidationStatusModel;
 45  
 import org.xmlhammer.model.preferences.Preferences;
 46  
 import org.xmlhammer.model.project.Project;
 47  
 
 48  
 /**
 49  
  * Put comment...
 50  
  * 
 51  
  * @version $Revision$, $Date$
 52  
  * @author Edwin Dankert <edankert@gmail.com>
 53  
  */
 54  
 public class StylesheetValidatorModule extends Module {
 55  88
     private TransformerFactory factory = null;
 56  
 
 57  
     /**
 58  
      * Sets-up an XML Validator Module.
 59  
      * 
 60  
      * @param preferences the global preferences.
 61  
      * @param project the project specific properties;
 62  
      * @throws ParserConfigurationException 
 63  
      * @throws SAXException 
 64  
      */
 65  
         public StylesheetValidatorModule(Preferences preferences, Project project, Logger logger) {
 66  44
         this(preferences, project, logger, true);
 67  44
         }
 68  
     
 69  
     public StylesheetValidatorModule(Preferences preferences, Project project, Logger logger, boolean logSettings) {
 70  88
         super(preferences, project, logger, logSettings);
 71  
         
 72  88
         factory = getTransformerFactory();
 73  88
     }
 74  
 
 75  
     @Override
 76  
         public void execute(StatusModel status, ResultModel result, URI uri) {
 77  88
                 if ( uri != null) {
 78  88
                         getLogger().info( "validate stylesheet: "+URIUtils.toString(uri));
 79  
 
 80  88
             DefaultErrorHandler errorHandler = new DefaultErrorHandler(uri);
 81  
 
 82  
             try {
 83  88
                 factory.setErrorListener(errorHandler);
 84  88
                 factory.newTransformer(new StreamSource(uri.toString()));
 85  44
             } catch (TransformerException e) {
 86  
                 try {
 87  44
                     errorHandler.error(e);
 88  44
                 } catch (Exception x) {}
 89  44
             }
 90  
  
 91  88
             ArrayList<Problem> list = errorHandler.getProblems();
 92  88
             if ( list.size() > 0) {
 93  44
                 if (status instanceof ValidationStatusModel) {
 94  22
                     ((ValidationStatusModel)status).setValid(false);
 95  
                 }
 96  
                 
 97  44
                             for ( Problem problem : list) {
 98  176
                     if ( problem instanceof Warning) {
 99  0
                         logWarning(uri, (TransformerException)problem.getException());
 100  
                         
 101  0
                         if (result != null) {
 102  0
                             result.addWarning( uri, (TransformerException)problem.getException());
 103  0
                         }
 104  176
                     } else if ( problem instanceof Error) {
 105  132
                         logError(uri, (TransformerException)problem.getException());
 106  
                         
 107  132
                         if (result != null) {
 108  66
                             result.addError( uri, (TransformerException)problem.getException());
 109  66
                         }
 110  44
                     } else if ( problem instanceof Fatal) {
 111  44
                         logFatal(uri, problem.getException());
 112  
                         
 113  44
                         if (result != null) {
 114  22
                             Exception exception = problem.getException();
 115  
                             
 116  22
                             if ( exception instanceof IOException) {
 117  0
                                 result.addFatal( uri, (IOException)problem.getException());
 118  0
                             } else {
 119  22
                                 result.addFatal( uri, (TransformerException)problem.getException());
 120  
                             }
 121  
                         }
 122  
                     }
 123  176
                             }
 124  44
             } else {
 125  
                 
 126  44
                 getLogger().info( "\t[valid] Valid Stylesheet");
 127  
                 
 128  44
                 if (result != null) {
 129  22
                     result.addValid(uri);
 130  
                 }
 131  
             }
 132  
                 }
 133  88
         }
 134  
 }