Clover coverage report - Maven Clover report
Coverage timestamp: Tue Sep 16 2008 01:16:37 EEST
file stats: LOC: 151   Methods: 8
NCLOC: 96   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
FileValidator.java 0% 0% 0% 0%
coverage
 1    /*
 2    wsmo4j - a WSMO API and Reference Implementation
 3    Copyright (c) 2005, University of Innsbruck, Austria
 4    This library is free software; you can redistribute it and/or modify it under
 5    the terms of the GNU Lesser General Public License as published by the Free
 6    Software Foundation; either version 2.1 of the License, or (at your option)
 7    any later version.
 8    This library is distributed in the hope that it will be useful, but WITHOUT
 9    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 10    FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 11    details.
 12    You should have received a copy of the GNU Lesser General Public License along
 13    with this library; if not, write to the Free Software Foundation, Inc.,
 14    59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 15    */
 16   
 17    package org.deri.wsmo4j.validator;
 18   
 19    import java.io.File;
 20    import java.io.FileNotFoundException;
 21    import java.io.FileReader;
 22    import java.io.IOException;
 23    import java.security.InvalidParameterException;
 24    import java.util.ArrayList;
 25    import java.util.HashMap;
 26    import java.util.List;
 27   
 28    import org.wsmo.common.TopEntity;
 29    import org.wsmo.common.exception.InvalidModelException;
 30    import org.wsmo.factory.Factory;
 31    import org.wsmo.factory.LogicalExpressionFactory;
 32    import org.wsmo.factory.WsmoFactory;
 33    import org.wsmo.validator.ValidationError;
 34    import org.wsmo.validator.ValidationMessage;
 35    import org.wsmo.validator.ValidationWarning;
 36    import org.wsmo.validator.WsmlValidator;
 37    import org.wsmo.wsml.Parser;
 38    import org.wsmo.wsml.ParserException;
 39   
 40    /**
 41    * A file validator that validates a given WSML document
 42    * given in the form of a file handle and prints complaints
 43    * from the parser and the validator out to the console.
 44    *
 45    * It is targeted at external programs rather than human
 46    * users, in particular at <i>glue</i> languages like perl
 47    * and phyton. All of its output adheres to the same regular
 48    * expression and is going to <code>stdout</code> rather than
 49    * <code>stderr</code>, defaulting to be accessible via
 50    * shell pipes.
 51    *
 52    * TODO make this class self-checking, to verify the
 53    * outgoing lines against the regular expression
 54    *
 55    * <pre>
 56    * Created on Apr 16, 2006
 57    * Committed by $Author: morcen $
 58    * $Source$
 59    * </pre>
 60    *
 61    * @author Thomas Haselwanter
 62    *
 63    * @version $Revision: 1946 $ $Date: 2007-04-02 15:13:28 +0300 (Mon, 02 Apr 2007) $code
 64    */
 65    public class FileValidator {
 66   
 67    private static final String usage = "wsmlvalidator [file]";
 68    private Parser parser;
 69    private WsmoFactory factory;
 70    private LogicalExpressionFactory leFactory;
 71    private WsmlValidator validator;
 72    private List <ValidationError> errors;
 73    private List <ValidationWarning> warnings;
 74   
 75  0 public FileValidator() {
 76  0 super();
 77  0 initialise();
 78    }
 79   
 80  0 private void initialise() {
 81  0 HashMap <String, Object> createParams = new HashMap <String, Object> ();
 82  0 createParams.put(Factory.PROVIDER_CLASS, "org.deri.wsmo4j.factory.LogicalExpressionFactoryImpl");
 83  0 leFactory = Factory.createLogicalExpressionFactory(createParams);
 84  0 factory = Factory.createWsmoFactory(null);
 85  0 createParams = new HashMap <String, Object> ();
 86  0 createParams.put(Factory.WSMO_FACTORY, factory);
 87  0 createParams.put(Factory.LE_FACTORY, leFactory);
 88  0 parser = Factory.createParser(createParams);
 89  0 validator = Factory.createWsmlValidator(null);
 90    }
 91   
 92  0 private void printMessages(List messages) {
 93  0 String p = "";
 94  0 for (int i = 0; i < messages.size(); i++) {
 95  0 ValidationMessage vm = (ValidationMessage) messages.get(i);
 96  0 p = "Validator::[1,1]::" + vm.getClass().getName() + "::" + vm.getReason();
 97  0 System.out.println(p);
 98    }
 99    }
 100   
 101  0 private void validate(File document)
 102    throws FileNotFoundException, IOException, ParserException, InvalidModelException {
 103  0 if (!document.exists())
 104  0 throw new FileNotFoundException("File doesn't exist.");
 105  0 if (!document.canRead())
 106  0 throw new FileNotFoundException("File not readable, check permissions.");
 107  0 TopEntity[] topEntities = parser.parse(new FileReader(document));
 108  0 errors = new ArrayList <ValidationError> ();
 109  0 warnings = new ArrayList <ValidationWarning> ();
 110  0 for (int i = 0; i < topEntities.length; i++) {
 111  0 validator.isValid(topEntities[i], errors, warnings);
 112    }
 113    }
 114   
 115  0 public static void main(String[] args) {
 116  0 FileValidator fv = new FileValidator();
 117  0 try {
 118  0 sanitycheck(args);
 119  0 fv.validate(new File(args[0]));
 120  0 fv.printMessages(fv.getErrors());
 121  0 fv.printMessages(fv.getWarnings());
 122    } catch (ParserException pe) {
 123  0 String token = pe.getFoundToken();
 124  0 if (token == null || token == "")
 125  0 token = " "; //fallback if somebody tries to precisely tag the error location
 126  0 System.out.println("Parser::[" + pe.getErrorLine() + "," + pe.getErrorPos() + "]::" +
 127    pe.getFoundToken() + "::" + pe.getMessage());
 128    } catch (Throwable t) {
 129  0 System.out.println("Failure::[1,1]::" + t.getClass().getName() + "::" +
 130    "Failure: " + t.getClass().getName() + ": " + t.getStackTrace()[0].getClassName() + ":" + t.getStackTrace()[0].getLineNumber());
 131  0 t.printStackTrace(System.err);
 132  0 System.exit(1);
 133    }
 134    }
 135   
 136  0 private static void sanitycheck(String[] args) throws InvalidParameterException {
 137    //FIXME stronger sanity checks and sanitation
 138  0 if (args.length < 1) {
 139  0 throw new InvalidParameterException("No file name supplied. Usage: " + usage);
 140    }
 141    }
 142   
 143  0 public List getErrors() {
 144  0 return errors;
 145    }
 146   
 147  0 public List getWarnings() {
 148  0 return warnings;
 149    }
 150   
 151    }