View Javadoc

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  	public FileValidator() {
76  		super();
77  		initialise();
78  	}
79      
80  	private void initialise() {        
81          HashMap <String, Object> createParams = new HashMap <String, Object> ();
82          createParams.put(Factory.PROVIDER_CLASS, "org.deri.wsmo4j.factory.LogicalExpressionFactoryImpl");
83          leFactory = Factory.createLogicalExpressionFactory(createParams);
84          factory = Factory.createWsmoFactory(null);
85          createParams = new HashMap <String, Object> ();
86          createParams.put(Factory.WSMO_FACTORY, factory);
87          createParams.put(Factory.LE_FACTORY, leFactory);
88          parser = Factory.createParser(createParams);
89          validator = Factory.createWsmlValidator(null);
90      }
91          
92      private void printMessages(List messages) {
93      	String p = "";
94      	for (int i = 0; i < messages.size(); i++) {
95      		ValidationMessage vm  = (ValidationMessage) messages.get(i);
96      		p = "Validator::[1,1]::" + vm.getClass().getName() + "::" + vm.getReason();    		
97          	System.out.println(p);
98      	}
99      }
100 
101     private void validate(File document) 
102     		throws FileNotFoundException, IOException, ParserException, InvalidModelException {
103 		if (!document.exists())
104 			throw new FileNotFoundException("File doesn't exist.");
105 		if (!document.canRead())
106 			throw new FileNotFoundException("File not readable, check permissions.");
107 		TopEntity[] topEntities = parser.parse(new FileReader(document));
108 		errors = new ArrayList <ValidationError> ();
109 		warnings = new ArrayList <ValidationWarning> ();
110     	for (int i = 0; i < topEntities.length; i++) {
111 			validator.isValid(topEntities[i], errors, warnings);		
112     	}
113     }
114     
115 	public static void main(String[] args) {
116 		FileValidator fv = new FileValidator();
117 		try {
118 			sanitycheck(args);
119 			fv.validate(new File(args[0]));
120 			fv.printMessages(fv.getErrors());
121 			fv.printMessages(fv.getWarnings());
122 		} catch (ParserException pe) {
123 			String token = pe.getFoundToken();
124 			if (token == null || token == "")
125 				token = " "; //fallback if somebody tries to precisely tag the error location
126 			System.out.println("Parser::[" + pe.getErrorLine() + "," + pe.getErrorPos() + "]::" + 
127 					pe.getFoundToken() + "::" + pe.getMessage());
128 		} catch (Throwable t) {
129 			System.out.println("Failure::[1,1]::" + t.getClass().getName() + "::" +
130 					"Failure: " + t.getClass().getName() + ": " + t.getStackTrace()[0].getClassName() + ":" + t.getStackTrace()[0].getLineNumber());
131 			t.printStackTrace(System.err);
132 			System.exit(1);
133 		}
134 	}
135 
136 	private static void sanitycheck(String[] args) throws InvalidParameterException {
137 		//FIXME stronger sanity checks and sanitation
138 		if (args.length < 1) {
139 			throw new InvalidParameterException("No file name supplied. Usage: " + usage);
140 		}
141 	}
142 
143 	public List getErrors() {
144 		return errors;
145 	}
146 
147 	public List getWarnings() {
148 		return warnings;
149 	}
150 	
151 }