001: /*
002: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * [See end of file]
004: */
005:
006: package com.hp.hpl.jena.n3;
007:
008: import java.io.*;
009: import antlr.collections.AST;
010: import com.hp.hpl.jena.util.FileUtils;
011:
012: /**
013: * @author Andy Seaborne
014: * @version $Id: N3EventPrinter.java,v 1.12 2008/01/02 12:04:48 andy_seaborne Exp $
015: */
016: public class N3EventPrinter implements N3ParserEventHandler {
017: public boolean printStartFinish = false;
018:
019: PrintWriter out = null;
020:
021: public N3EventPrinter(OutputStream _out) {
022: out = FileUtils.asPrintWriterUTF8(_out);
023: }
024:
025: /** Best not to use a PrintWriter, but use an OutputStreamWriter (buffered)
026: * with charset "UTF-8".
027: */
028:
029: public N3EventPrinter(PrintWriter _out) {
030: out = _out;
031: }
032:
033: public void error(Exception ex, String message) {
034: println(out, "Error: " + message);
035: flush(out);
036: }
037:
038: //public void warning(Exception ex, String message) { println(out, "Warning: "+message) ; flush(out) ; }
039: //public void deprecated(Exception ex, String message) { println(out, "Deprecated: "+message) ; flush(out) ; }
040:
041: public void startDocument() {
042: if (printStartFinish) {
043: println(out, "Document start");
044: flush(out);
045: }
046: }
047:
048: public void endDocument() {
049: if (printStartFinish) {
050: println(out, "Document end");
051: flush(out);
052: }
053: }
054:
055: public void startFormula(int line, String context) {
056: if (printStartFinish) {
057: print(out, "Formula start: " + context);
058: flush(out);
059: }
060: }
061:
062: public void endFormula(int line, String context) {
063: if (printStartFinish) {
064: print(out, "Formula finish: " + context);
065: flush(out);
066: }
067: }
068:
069: public void directive(int line, AST directive, AST[] args,
070: String context) {
071: if (context != null)
072: print(out, context + " ");
073:
074: print(out, directive.getText());
075:
076: for (int i = 0; i < args.length; i++) {
077: print(out, " ");
078: printSlot(out, args[i]);
079: }
080: println(out);
081: flush(out);
082: }
083:
084: public void quad(int line, AST subj, AST prop, AST obj,
085: String context) {
086: if (context != null)
087: print(out, context + " ");
088:
089: print(out, "[ ");
090: printSlot(out, subj);
091: print(out, " , ");
092: printSlot(out, prop);
093: print(out, " , ");
094: printSlot(out, obj);
095: println(out, " ]");
096: flush(out);
097: }
098:
099: static public String formatSlot(AST slot) {
100: try {
101: StringWriter sw = new StringWriter();
102: printSlot(sw, slot);
103: sw.close();
104: return sw.toString();
105: } catch (IOException ioEx) {
106: }
107: return null;
108: }
109:
110: private static void printSlot(Writer out, AST ast) {
111: printSlot(out, ast, true);
112: }
113:
114: private static void printSlot(Writer out, AST ast, boolean printType) {
115: try {
116: if (ast == null) {
117: out.write("<null>");
118: return;
119: }
120:
121: int tokenType = ast.getType();
122: String tmp = ast.toString();
123: if (tmp.equals(""))
124: tmp = "<empty string>";
125:
126: switch (tokenType) {
127: case N3Parser.LITERAL:
128: out.write('"');
129: printString(out, tmp);
130: out.write('"');
131:
132: AST a1 = ast.getNextSibling();
133: AST a2 = (a1 == null ? null : a1.getNextSibling());
134: printLiteralModifier(out, a1);
135: printLiteralModifier(out, a2);
136: break;
137:
138: case N3Parser.UVAR:
139: // Is this a compound variable (i.e. with datatype condition)?
140: AST ast2 = ast.getFirstChild();
141: out.write(tmp);
142: if (ast2 != null) {
143: out.write("^^");
144: printSlot(out, ast2, false);
145: }
146: break;
147:
148: // Write anything else.
149: default:
150: out.write(tmp);
151: break;
152: }
153:
154: if (printType) {
155: out.write('(');
156: out.write(N3Parser.getTokenNames()[tokenType]);
157: out.write(')');
158: }
159: } catch (IOException ioEx) {
160: }
161:
162: }
163:
164: private static void printString(Writer out, String s) {
165: try {
166: for (int i = 0; i < s.length(); i++) {
167: char c = s.charAt(i);
168: if (c == '\\' || c == '"') {
169: out.write('\\');
170: out.write(c);
171: } else if (c == '\n') {
172: out.write("\\n");
173: } else if (c == '\r') {
174: out.write("\\r");
175: } else if (c == '\t') {
176: out.write("\\t");
177: } else if (c >= 32 && c < 127) {
178: out.write(c);
179: } else {
180: String hexstr = Integer.toHexString(c)
181: .toUpperCase();
182: int pad = 4 - hexstr.length();
183: out.write("\\u");
184: for (; pad > 0; pad--)
185: out.write("0");
186: out.write(hexstr);
187: }
188: }
189: } catch (IOException ioEx) {
190: }
191: }
192:
193: private static void printLiteralModifier(Writer out, AST a)
194: throws IOException {
195: if (a == null)
196: return;
197: int i = a.getType();
198: switch (a.getType()) {
199: case N3Parser.DATATYPE:
200: out.write("^^");
201: AST dt = a.getFirstChild();
202: printSlot(out, dt, false);
203: break;
204: case N3Parser.AT_LANG:
205: //out.write("@");
206: out.write(a.getText());
207: break;
208: default:
209: System.err
210: .println("Error in grammar - not a datatype or lang tag: "
211: + a.getText()
212: + "/"
213: + N3Parser.getTokenNames()[a.getType()]);
214: }
215: }
216:
217: private static void print(PrintWriter out, String s) {
218: out.print(s);
219: }
220:
221: private static void println(PrintWriter out, String s) {
222: out.println(s);
223: }
224:
225: private static void println(PrintWriter out) {
226: out.println();
227: }
228:
229: private static void flush(PrintWriter out) {
230: out.flush();
231: }
232: }
233:
234: /*
235: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
236: * All rights reserved.
237: *
238: * Redistribution and use in source and binary forms, with or without
239: * modification, are permitted provided that the following conditions
240: * are met:
241: * 1. Redistributions of source code must retain the above copyright
242: * notice, this list of conditions and the following disclaimer.
243: * 2. Redistributions in binary form must reproduce the above copyright
244: * notice, this list of conditions and the following disclaimer in the
245: * documentation and/or other materials provided with the distribution.
246: * 3. The name of the author may not be used to endorse or promote products
247: * derived from this software without specific prior written permission.
248: *
249: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
250: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
251: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
252: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
253: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
254: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
256: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
258: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
259: */
|