File: ValidatorDriver.java

package info (click to toggle)
libmarc4j-java 2.9.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,908 kB
  • sloc: xml: 109,577; java: 22,102; makefile: 7
file content (70 lines) | stat: -rw-r--r-- 2,802 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
 * Copyright (C) 2002 Bas Peters (mail@bpeters.com)
 *
 * ValidatorDriver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version.
 *
 * ValidatorDriver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public 
 * License along with ValidatorDriver; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
import java.io.*;

import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.sax.*;
import javax.xml.transform.stream.*;
import com.thaiopensource.relaxng.*;
import com.thaiopensource.relaxng.util.*;

/**
 * <p>This is a driver class to test ValidatorFilter.
 *
 * @author <a href="mailto:mail@bpeters.com">Bas Peters</a>
 * @see ValidatorFilter
 */
public class ValidatorDriver {
    public static void main(String args[]) {
	if(args.length < 1) {
	    System.out.println("Driver <rng-schema> <input-file> [<stylesheet>]");
	    return;
	}
	String rngSchema = args[0];
        String input = args[1];
        String stylesheet = (args.length > 2) ? args[2] : null;
        try {
	    SchemaFactory factory = new SchemaFactory();
	    factory.setXMLReaderCreator(new Jaxp11XMLReaderCreator());
	    factory.setDatatypeLibraryFactory(new org.relaxng.datatype.helpers.DatatypeLibraryLoader());
	    Schema s = factory.createSchema(new InputSource(new File(rngSchema).toURL().toString()));
	    ErrorHandlerImpl eh = new ErrorHandlerImpl(System.out);
	    ValidatorHandler vh = s.createValidator();
	    XMLReader producer = factory.getXMLReaderCreator().createXMLReader();
	    XMLFilter filter = new ValidatorFilter();
	    filter.setProperty("http://org.marc4j/relaxng/validator", vh);
	    filter.setErrorHandler(eh);
	    filter.setParent(producer);
	    InputSource in = new InputSource(new File(input).toURL().toString());
	    Source source = new SAXSource(filter, in);
	    Result result = new StreamResult(System.out);
	    TransformerFactory tFactory = TransformerFactory.newInstance();
	    Transformer transformer;
	    if (stylesheet != null)
		transformer = tFactory.newTransformer(new StreamSource(new File(stylesheet).toURL().toString()));
	    else
		transformer = tFactory.newTransformer();
	    transformer.transform(source, result);
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
}