KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > n3 > N3EventPrinter


1 /*
2  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package com.hp.hpl.jena.n3;
7 import java.io.*;
8 import antlr.collections.AST;
9 import com.hp.hpl.jena.util.FileUtils ;
10
11 /**
12  * @author Andy Seaborne
13  * @version $Id: N3EventPrinter.java,v 1.9 2005/02/21 12:04:04 andy_seaborne Exp $
14  */

15 public class N3EventPrinter implements N3ParserEventHandler
16 {
17     public boolean printStartFinish = false ;
18     
19     PrintWriter out = null;
20     
21     public N3EventPrinter(OutputStream _out)
22     {
23         out = FileUtils.asPrintWriterUTF8(_out) ;
24     }
25     
26     /** Best not to use a PrintWriter, but use an OutputStreamWriter (buffered)
27      * with charset "UTF-8".
28      */

29     
30     public N3EventPrinter(PrintWriter _out)
31     {
32         out = _out;
33     }
34
35     public void error(Exception JavaDoc ex, String JavaDoc message) { println(out, "Error: "+message) ; flush(out); }
36     //public void warning(Exception ex, String message) { println(out, "Warning: "+message) ; flush(out) ; }
37
//public void deprecated(Exception ex, String message) { println(out, "Deprecated: "+message) ; flush(out) ; }
38

39     public void startDocument()
40     {
41         if ( printStartFinish )
42         {
43             println(out, "Document start");
44             flush(out);
45         }
46     }
47     public void endDocument()
48     {
49         if ( printStartFinish )
50         {
51             println(out, "Document end");
52             flush(out);
53         }
54     }
55
56     public void startFormula(int line, String JavaDoc context)
57     {
58         if ( printStartFinish )
59         {
60             print(out, "Formula start: "+context) ;
61             flush(out) ;
62         }
63     }
64     
65     public void endFormula(int line, String JavaDoc context)
66     {
67         if ( printStartFinish )
68         {
69             print(out, "Formula finish: "+context) ;
70             flush(out) ;
71         }
72     }
73
74     public void directive(int line, AST directive, AST[] args, String JavaDoc context)
75     {
76         if ( context != null )
77             print(out, context+" ") ;
78
79         print(out, directive.getText()) ;
80
81         for (int i = 0; i < args.length; i++)
82         {
83             print(out, " ");
84             printSlot(out, args[i]) ;
85         }
86         println(out) ;
87         flush(out);
88     }
89
90     public void quad(int line, AST subj, AST prop, AST obj, String JavaDoc context)
91     {
92         if ( context != null )
93             print(out, context+" ") ;
94
95         print(out, "[ ");
96         printSlot(out, subj);
97         print(out, " , ");
98         printSlot(out, prop);
99         print(out, " , ");
100         printSlot(out, obj);
101         println(out, " ]");
102         flush(out);
103     }
104
105     static public String JavaDoc formatSlot(AST slot)
106     {
107         try {
108             StringWriter sw = new StringWriter() ;
109             printSlot(sw, slot) ;
110             sw.close() ;
111             return sw.toString() ;
112         } catch (IOException ioEx) {}
113         return null ;
114     }
115
116     private static void printSlot(Writer out, AST ast) { printSlot(out, ast, true) ; }
117     private static void printSlot(Writer out, AST ast, boolean printType)
118     {
119         try {
120             if (ast == null)
121             {
122                 out.write("<null>");
123                 return;
124             }
125     
126             int tokenType = ast.getType();
127             String JavaDoc tmp = ast.toString();
128             if (tmp.equals(""))
129                 tmp = "<empty string>";
130             
131             switch (tokenType)
132             {
133                 case N3Parser.LITERAL:
134                 out.write('"');
135                 printString(out, tmp);
136                 out.write('"');
137                 
138                 AST a1 = ast.getNextSibling() ;
139                 AST a2 = (a1==null?null:a1.getNextSibling()) ;
140                 printLiteralModifier(out, a1) ;
141                 printLiteralModifier(out, a2) ;
142                 break ;
143                 
144                 case N3Parser.UVAR:
145                 // Is this a compound variable (i.e. with datatype condition)?
146
AST ast2 = ast.getFirstChild() ;
147                 out.write(tmp) ;
148                 if ( ast2 != null )
149                 {
150                     out.write("^^") ;
151                     printSlot(out, ast2, false) ;
152                 }
153                 break ;
154                 
155                 // Write anything else.
156
default:
157                 out.write(tmp) ;
158                 break ;
159             }
160             
161             if ( printType )
162             {
163                 out.write('(');
164                 out.write(N3Parser.getTokenNames()[tokenType]);
165                 out.write(')');
166             }
167         } catch (IOException ioEx) {}
168         
169     }
170
171     private static void printString(Writer out, String JavaDoc s)
172     {
173         try {
174             for (int i = 0; i < s.length(); i++)
175             {
176                 char c = s.charAt(i);
177                 if (c == '\\' || c == '"')
178                 {
179                     out.write('\\');
180                     out.write(c);
181                 }
182                 else if (c == '\n')
183                 {
184                     out.write("\\n");
185                 }
186                 else if (c == '\r')
187                 {
188                     out.write("\\r");
189                 }
190                 else if (c == '\t')
191                 {
192                     out.write("\\t");
193                 }
194                 else if (c >= 32 && c < 127)
195                 {
196                     out.write(c);
197                 }
198                 else
199                 {
200                     String JavaDoc hexstr = Integer.toHexString(c).toUpperCase();
201                     int pad = 4 - hexstr.length();
202                     out.write("\\u");
203                     for (; pad > 0; pad--)
204                         out.write("0");
205                     out.write(hexstr);
206                 }
207             }
208         } catch (IOException ioEx) {}
209     }
210
211     private static void printLiteralModifier(Writer out, AST a) throws IOException
212     {
213         if ( a == null )
214             return ;
215         int i = a.getType() ;
216         switch (a.getType())
217         {
218             case N3Parser.DATATYPE :
219                 out.write("^^");
220                 AST dt = a.getFirstChild() ;
221                 printSlot(out, dt, false) ;
222                 break;
223             case N3Parser.AT_LANG :
224                 //out.write("@");
225
out.write(a.getText());
226                 break ;
227             default :
228                 System.err.println(
229                     "Error in grammar - not a datatype or lang tag: "
230                         + a.getText()
231                         + "/"
232                         + N3Parser.getTokenNames()[a.getType()]);
233         }
234     }
235
236
237     private static void print(PrintWriter out, String JavaDoc s)
238     {
239         out.print(s);
240     }
241     private static void println(PrintWriter out, String JavaDoc s)
242     {
243         out.println(s) ;
244     }
245     private static void println(PrintWriter out)
246     {
247         out.println() ;
248     }
249
250     private static void flush(PrintWriter out)
251     {
252         out.flush() ;
253     }
254 }
255
256 /*
257  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
258  * All rights reserved.
259  *
260  * Redistribution and use in source and binary forms, with or without
261  * modification, are permitted provided that the following conditions
262  * are met:
263  * 1. Redistributions of source code must retain the above copyright
264  * notice, this list of conditions and the following disclaimer.
265  * 2. Redistributions in binary form must reproduce the above copyright
266  * notice, this list of conditions and the following disclaimer in the
267  * documentation and/or other materials provided with the distribution.
268  * 3. The name of the author may not be used to endorse or promote products
269  * derived from this software without specific prior written permission.
270  *
271  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
272  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
273  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
274  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
275  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
276  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
277  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
278  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
279  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
280  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
281  */

282
Popular Tags