/* * (c) Copyright 2009 Hewlett-Packard Development Company, LP * All rights reserved. * [See end of file] */ package migrate; import java.io.IOException; import java.io.Writer; import org.openjena.atlas.logging.Log; import com.hp.hpl.jena.graph.Node; import com.hp.hpl.jena.graph.Triple; import com.hp.hpl.jena.shared.JenaException; import com.hp.hpl.jena.sparql.core.Quad; import com.hp.hpl.jena.vocabulary.XSD; public class FmtUtils2 { // Replacement for FmtUtils - this one is centered around streamed out. // Prologue versions. public static void output(Writer out, Quad quad) {} public static void output(Writer out, Triple triple) {} public static void output(Writer out, Node node) {} public static void outputURI(Writer out, String uri) {} public static void outputLiteral(Writer out, String lex, String datatype, String lang) {} public static void outputBNode(Writer out, String label) {} private static void write(Writer out, int ch) { try { out.write(ch) ; } catch (IOException ex) { exception(ex) ; } } private static void write(Writer out, CharSequence chars) { try { out.append(chars) ; } catch (IOException ex) { exception(ex) ; } } private static void exception(IOException ex) { throw new RuntimeException("IOException", ex) ; } // ---- /** Write a node to the writer (assumed to be UTF-8) */ public static void writeNode(Writer out, Node n) { try { if ( n == null ) throw new IllegalArgumentException("Null node") ; // Concrete blank nodes if ( n.isBlank() ) { writeBlankNode(out, n.getBlankNodeLabel()) ; return ; } if ( n.isLiteral() ) { writeLiteral(out, n.getLiteralLexicalForm(), n.getLiteralDatatypeURI(), n.getLiteralLanguage()) ; return ; } if ( n.isURI() ) { writeURI(out, n.getURI()) ; return ; } if ( n.isVariable() ) { writeVariable(out, n.getName()) ; return ; } Log.fatal(FmtUtils2.class, "Unrecognized node type: "+n) ; return ; } catch (IOException ex) { throw new JenaException(FmtUtils2.class.getSimpleName(), ex) ; } } private static void writeLiteral(Writer out, String lexicalForm, String datatypeURI, String langTag) throws IOException { if ( datatypeURI != null ) { writeLiteralDT(out, lexicalForm, datatypeURI) ; return ; } if ( langTag != null ) { writeLiteralLang(out, lexicalForm, langTag) ; return ; } writeQuotedString(out, '"', lexicalForm) ; } /** Write a string, without quotes, with escapes */ private static void writeQuotedString(Writer out, char quote, String string) throws IOException { out.append(quote) ; final boolean singleLineString = true ; final boolean applyUnicodeEscapes = false ; int len = string.length() ; for (int i = 0; i < len; i++) { char c = string.charAt(i); // Escape escapes and quotes if (c == '\\' || c == '"' ) { out.append('\\') ; out.append(c) ; continue ; } // Characters to literally output. // This would generate 7-bit safe files // if (c >= 32 && c < 127) // { // sbuff.append(c) ; // continue; // } // Whitespace if ( singleLineString && ( c == '\n' || c == '\r' || c == '\f' ) ) { switch(c) { case '\n': out.append("\\n"); break ; case '\r': out.append("\\r"); break ; case '\f': out.append("\\f"); break ; default: break ; } continue ; } // Output as is (subject to UTF-8 encoding on output, that is) if ( ! applyUnicodeEscapes ) // Normal route. out.append(c) ; else { // Unicode escapes // c < 32, c >= 127, not whitespace or other specials if ( c >= 32 && c < 127 ) out.append(c) ; else { String hexstr = Integer.toHexString(c).toUpperCase(); int pad = 4 - hexstr.length(); out.append("\\u"); for (; pad > 0; pad--) out.append("0"); out.append(hexstr); } } } out.append(quote) ; } private static void writeLiteralDT(Writer out, String lexicalForm, String datatypeURI) throws IOException { // Special forms we know how to handle - numbers. if ( datatypeURI.equals(XSD.integer.getURI()) ) { try { String s1 = lexicalForm ; // BigInteger does not allow leading + // so chop it off before the format test // BigDecimal does allow a leading + if ( s1.startsWith("+") ) s1 = s1.substring(1) ; // XXX Better? new java.math.BigInteger(s1) ; out.append(s1) ; return ; } catch (NumberFormatException nfe) {} // No luck. Continue. // Continuing is always safe. } if ( datatypeURI.equals(XSD.decimal.getURI()) ) { if ( lexicalForm.indexOf('.') > 0 ) { try { // BigDecimal does allow a leading + new java.math.BigDecimal(lexicalForm) ; out.append(lexicalForm) ; return ; } catch (NumberFormatException nfe) {} // No luck. Continue. } } if ( datatypeURI.equals(XSD.xdouble.getURI()) ) { // Assumes SPARQL has decimals and doubles. // Must have 'e' or 'E' to be a double short form. if ( lexicalForm.indexOf('e') >= 0 || lexicalForm.indexOf('E') >= 0 ) { try { Double.parseDouble(lexicalForm) ; out.append(lexicalForm) ; // original lexical form. return ; } catch (NumberFormatException nfe) {} // No luck. Continue. } } // if ( datatype.equals(XSD.xboolean.getURI()) ) // { // if ( lexicalForm.equalsIgnoreCase("true") || lexicalForm.equalsIgnoreCase("false") ) // { // out.append(lexicalForm) ; // return ; // } // } // Not a recognized form. writeQuotedString(out, '"', lexicalForm) ; out.append("^^") ; writeURI(out, datatypeURI) ; } private static void writeLiteralLang(Writer out, String lexicalForm, String langTag) throws IOException { writeQuotedString(out, '"', lexicalForm) ; if ( langTag != null && langTag.length()>0) { out.append("@") ; out.append(langTag) ; } } private static void writeURI(Writer out, String uri) throws IOException { out.append('<').append(uri).append('>') ; } private static void writeBlankNode(Writer out, String blankNodeLabel) throws IOException { out.append("<_:").append(blankNodeLabel).append(">") ; } private static void writeVariable(Writer out, String name) throws IOException { out.append('?').append(name) ; } } /* * (c) Copyright 2009 Hewlett-Packard Development Company, LP * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */