KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > common > SimpleParser


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.common;
10
11 import net.sourceforge.chaperon.build.LexicalAutomatonBuilder;
12 import net.sourceforge.chaperon.build.ParserAutomatonBuilder;
13 import net.sourceforge.chaperon.model.grammar.Grammar;
14 import net.sourceforge.chaperon.model.grammar.GrammarFactory;
15 import net.sourceforge.chaperon.model.lexicon.Lexicon;
16 import net.sourceforge.chaperon.model.lexicon.LexiconFactory;
17 import net.sourceforge.chaperon.process.LexicalAutomaton;
18 import net.sourceforge.chaperon.process.LexicalProcessor;
19 import net.sourceforge.chaperon.process.ParserAutomaton;
20 import net.sourceforge.chaperon.process.ParserProcessor;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.impl.SimpleLog;
24
25 import org.xml.sax.XMLReader JavaDoc;
26 import org.xml.sax.helpers.AttributesImpl JavaDoc;
27 import org.xml.sax.helpers.LocatorImpl JavaDoc;
28
29 import java.io.File JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.io.InputStreamReader JavaDoc;
32 import java.io.LineNumberReader JavaDoc;
33
34 import javax.xml.parsers.SAXParserFactory JavaDoc;
35 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
36 import javax.xml.transform.sax.TransformerHandler JavaDoc;
37 import javax.xml.transform.stream.StreamResult JavaDoc;
38
39 /**
40  * Simple example for the using of the Chaperon parser.
41  *
42  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels </a>
43  * @version CVS $Id: SimpleParser.java,v 1.7 2003/12/09 19:55:52 benedikta Exp $
44  */

45 public class SimpleParser
46 {
47   public static void process(File JavaDoc lexiconFile, File JavaDoc grammarFile, File JavaDoc inFile, File JavaDoc outFile)
48     throws Exception JavaDoc
49   {
50     // Create log
51
Log log = new SimpleLog("log");
52
53     // Create factory for SAX parser
54
SAXParserFactory JavaDoc parserFactoryImpl = SAXParserFactory.newInstance();
55     parserFactoryImpl.setNamespaceAware(true);
56
57     // Get a SAX parser
58
XMLReader JavaDoc xmlparser = parserFactoryImpl.newSAXParser().getXMLReader();
59
60     // Create a lexicon model for a given lexicon file
61
LexiconFactory lexiconfactory = new LexiconFactory();
62     xmlparser.setContentHandler(lexiconfactory);
63     xmlparser.parse(lexiconFile.toString());
64
65     Lexicon lexicon = lexiconfactory.getLexicon();
66
67     // Build a automaton from the lexicon model
68
LexicalAutomaton lexicalautomaton =
69       (new LexicalAutomatonBuilder(lexicon, log)).getLexicalAutomaton();
70
71     // Create a processor for the lexicon
72
LexicalProcessor lexer = new LexicalProcessor();
73     lexer.setLog(log);
74     lexer.setLexicalAutomaton(lexicalautomaton);
75
76     // Get a SAX parser
77
xmlparser = parserFactoryImpl.newSAXParser().getXMLReader();
78
79     // Create a grammar model for a given grammar file
80
GrammarFactory grammarfactory = new GrammarFactory();
81     xmlparser.setContentHandler(grammarfactory);
82     xmlparser.parse(grammarFile.toString());
83
84     Grammar grammar = grammarfactory.getGrammar();
85
86     // Build a automaton from the grammar model
87
ParserAutomaton parserautomaton =
88       (new ParserAutomatonBuilder(grammar, log)).getParserAutomaton();
89
90     // Create a processor for the grammar
91
ParserProcessor parser = new ParserProcessor();
92     parser.setLog(log);
93     parser.setParserAutomaton(parserautomaton);
94
95     // Create factory for SAX transformer
96
SAXTransformerFactory JavaDoc transformerFactoryImpl =
97       (SAXTransformerFactory JavaDoc)SAXTransformerFactory.newInstance();
98
99     // Create serializer to write the SAX stream into a file
100
TransformerHandler JavaDoc serializer = transformerFactoryImpl.newTransformerHandler();
101     serializer.setResult(new StreamResult JavaDoc(outFile));
102
103     // Connect components into a pipeline
104
lexer.setContentHandler(parser);
105     parser.setContentHandler(serializer);
106
107     // Push text into this pipeline
108
// Create locator, which help to find possible syntax errors
109
LocatorImpl JavaDoc locator = new LocatorImpl JavaDoc();
110     locator.setSystemId(inFile.toURL().toString());
111     locator.setLineNumber(1);
112     locator.setColumnNumber(1);
113     lexer.setDocumentLocator(locator);
114
115     // Start document
116
lexer.startDocument();
117
118     // Start 'text' element, which the parser dispatch
119
lexer.startElement("http://chaperon.sourceforge.net/schema/text/1.0", "text", "text",
120                        new AttributesImpl JavaDoc());
121
122     LineNumberReader JavaDoc reader =
123       new LineNumberReader JavaDoc(new InputStreamReader JavaDoc(new FileInputStream JavaDoc(inFile)));
124
125     String JavaDoc line;
126     String JavaDoc newline = null;
127     String JavaDoc separator = System.getProperty("line.separator");
128
129     // Push text
130
while (true)
131     {
132       if (newline==null)
133         line = reader.readLine();
134       else
135         line = newline;
136
137       if (line==null)
138         break;
139
140       newline = reader.readLine();
141
142       line = (newline!=null) ? (line+separator) : line;
143
144       locator.setLineNumber(reader.getLineNumber());
145       locator.setColumnNumber(1);
146       lexer.characters(line.toCharArray(), 0, line.length());
147
148       if (newline==null)
149         break;
150     }
151
152     reader.close();
153
154     // End 'text' element
155
lexer.endElement("http://chaperon.sourceforge.net/schema/text/1.0", "text", "text");
156
157     // End document
158
lexer.endDocument();
159   }
160
161   public static void main(String JavaDoc[] args)
162   {
163     File JavaDoc lexiconFile = new File JavaDoc(args[0]);
164     File JavaDoc grammarFile = new File JavaDoc(args[1]);
165     File JavaDoc inFile = new File JavaDoc(args[2]);
166     File JavaDoc outFile = new File JavaDoc(args[3]);
167
168     try
169     {
170       process(lexiconFile, grammarFile, inFile, outFile);
171     }
172     catch (Exception JavaDoc e)
173     {
174       e.printStackTrace();
175     }
176   }
177 }
178
Popular Tags