1 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 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 29 30 public N3EventPrinter(PrintWriter _out) 31 { 32 out = _out; 33 } 34 35 public void error(Exception ex, String message) { println(out, "Error: "+message) ; flush(out); } 36 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 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 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 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 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 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 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 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 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 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 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(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 s) 238 { 239 out.print(s); 240 } 241 private static void println(PrintWriter out, String 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 282 | Popular Tags |