You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2011/08/31 18:40:00 UTC
svn commit: r1163690 - in /incubator/jena/Jena2/ARQ/trunk:
src-dev/dev/DevARQ.java src/org/openjena/riot/out/EscapeStr.java
src/org/openjena/riot/out/NodeFormatterTTL.java
src/org/openjena/riot/out/OutputLangUtils.java
Author: andy
Date: Wed Aug 31 16:40:00 2011
New Revision: 1163690
URL: http://svn.apache.org/viewvc?rev=1163690&view=rev
Log:
Output utilities (WIP)
Added:
incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/EscapeStr.java (with props)
Modified:
incubator/jena/Jena2/ARQ/trunk/src-dev/dev/DevARQ.java
incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/NodeFormatterTTL.java
incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/OutputLangUtils.java
Modified: incubator/jena/Jena2/ARQ/trunk/src-dev/dev/DevARQ.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src-dev/dev/DevARQ.java?rev=1163690&r1=1163689&r2=1163690&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src-dev/dev/DevARQ.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src-dev/dev/DevARQ.java Wed Aug 31 16:40:00 2011
@@ -8,6 +8,14 @@ public class DevARQ
// Do FILTER placement before filter equality -- but dnager of breaking up BGPs.
// riot.out.SinkTripleOutput and BufferingWriter : respect encoder
+ // Escaping characters by policy.
+ // EscapePolicy(Writer, String)
+ // See also OutputLangUtils -- extract code and deletes
+ // Make an object so it can have per-instance flags
+ // ASCII vs UTF-8
+ // Abbreviate numbers or not.
+ // Avoids creating intermediate strings.
+ // == Class with two subclasses. Turtle policy and N-triples policy.
// Automatically create graphs for in-memorry update.
// DatasetFactory.create() ; returns an auto-add dataset.
Added: incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/EscapeStr.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/EscapeStr.java?rev=1163690&view=auto
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/EscapeStr.java (added)
+++ incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/EscapeStr.java Wed Aug 31 16:40:00 2011
@@ -0,0 +1,119 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openjena.riot.out;
+
+import java.io.IOException ;
+import java.io.StringWriter ;
+import java.io.Writer ;
+
+import org.openjena.atlas.io.IO ;
+import org.openjena.atlas.io.OutputUtils ;
+
+public class EscapeStr
+{
+ // Tests: TestOutput
+ // See also OutputLangUtils.outputEsc.
+ private final boolean ascii ;
+
+ public EscapeStr(boolean asciiOnly) { this.ascii = asciiOnly ; }
+
+ public void writeURI(Writer w, String s)
+ {
+ try
+ {
+ if ( ascii )
+ stringEsc(w, s, true, ascii) ;
+ else
+ // It's a URI - assume legal.
+ w.write(s) ;
+ } catch (IOException e) { IO.exception(e) ; }
+ }
+
+ public void writeStr(Writer w, String s)
+ {
+ try
+ {
+ stringEsc(w, s, true, ascii) ;
+ } catch (IOException e) { IO.exception(e) ; }
+ }
+
+ public void writeStrMultiLine(Writer w, String s)
+ {
+ // N-Triples does not have """
+ try
+ {
+ stringEsc(w, s, false, ascii) ;
+ } catch (IOException e) { IO.exception(e) ; }
+ }
+
+ // Utility
+ /*
+ * Escape characters in a string according to Turtle rules.
+ */
+ public static String stringEsc(String s)
+ { return stringEsc(s, true, false) ; }
+
+ private static String stringEsc(String s, boolean singleLineString, boolean asciiOnly)
+ {
+ try
+ {
+ Writer sb = new StringWriter() ;
+ stringEsc(sb, s, singleLineString, asciiOnly) ;
+ return sb.toString() ;
+ } catch (IOException e) { IO.exception(e) ; return null ; }
+ }
+
+ private static void stringEsc(Writer out, String s, boolean singleLineString, boolean asciiOnly) throws IOException
+ {
+ int len = s.length() ;
+ for (int i = 0; i < len; i++) {
+ char c = s.charAt(i);
+
+ // \\ Escape always possible.
+ if (c == '\\')
+ {
+ out.write('\\') ;
+ out.write(c) ;
+ continue ;
+ }
+ if ( singleLineString )
+ {
+ if ( c == '"' ) { out.write("\\\""); continue ; }
+ else if (c == '\n') { out.write("\\n"); continue ; }
+ else if (c == '\t') { out.write("\\t"); continue ; }
+ else if (c == '\r') { out.write("\\r"); continue ; }
+ else if (c == '\f') { out.write("\\f"); continue ; }
+ }
+ // Not \-style esacpe.
+ if ( c >= 32 && c < 127 )
+ out.write(c);
+ else if ( !asciiOnly )
+ out.write(c);
+ else
+ {
+ // Outside the charset range.
+ // Does not cover beyond 16 bits codepoints directly
+ // (i.e. \U escapes) but Java keeps these as surrogate
+ // pairs and will print as characters
+ out.write( "\\u") ;
+ OutputUtils.printHex(out, c, 4) ;
+ }
+ }
+ }
+}
Propchange: incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/EscapeStr.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/NodeFormatterTTL.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/NodeFormatterTTL.java?rev=1163690&r1=1163689&r2=1163690&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/NodeFormatterTTL.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/NodeFormatterTTL.java Wed Aug 31 16:40:00 2011
@@ -20,6 +20,7 @@ package org.openjena.riot.out;
import java.io.IOException ;
import java.io.Writer ;
+import java.net.MalformedURLException ;
import org.openjena.atlas.io.IO ;
import org.openjena.atlas.lib.Pair ;
@@ -27,6 +28,9 @@ import org.openjena.riot.system.PrefixMa
import org.openjena.riot.system.RiotChars ;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype ;
+import com.hp.hpl.jena.iri.IRI ;
+import com.hp.hpl.jena.iri.IRIFactory ;
+import com.hp.hpl.jena.iri.IRIRelativize ;
public class NodeFormatterTTL extends NodeFormatterNT
{
@@ -59,11 +63,39 @@ public class NodeFormatterTTL extends No
return ;
}
}
- } catch (IOException ex) { IO.exception(ex) ; }
+
+ // Attemp base abbreviation.
+ if ( baseIRI != null )
+ {
+ String x = abbrevByBase(uriStr, baseIRI) ;
+ if ( x != null )
+ {
+ w.write('<') ;
+ w.write(x) ;
+ w.write('>') ;
+ return ;
+ }
+ }
+ } catch (IOException ex) { IO.exception(ex) ; }
+
super.formatURI(w, uriStr) ;
}
+
+ static private int relFlags = IRIRelativize.SAMEDOCUMENT | IRIRelativize.CHILD ;
+ static private String abbrevByBase(String uri, String base)
+ {
+ if ( base == null )
+ return null ;
+ IRI baseIRI = IRIFactory.jenaImplementation().construct(base) ;
+ IRI rel = baseIRI.relativize(uri, relFlags) ;
+ String r = null ;
+ try { r = rel.toASCIIString() ; }
+ catch (MalformedURLException ex) { r = rel.toString() ; }
+ return r ;
+ }
-/*private-testing*/ static boolean safeForPrefix(String str)
+ /*private-testing*/
+ static boolean safeForPrefix(String str)
{
int N = str.length() ;
if ( N == 0 ) return true ;
Modified: incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/OutputLangUtils.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/OutputLangUtils.java?rev=1163690&r1=1163689&r2=1163690&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/OutputLangUtils.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/OutputLangUtils.java Wed Aug 31 16:40:00 2011
@@ -243,6 +243,7 @@ public class OutputLangUtils
* @param useSlashEscapes Whether to use \t etc (\\ is awlays possible).
*
*/
+ @Deprecated
static public void outputEsc(Writer out, String s, boolean useSlashEscapes)
{
int len = s.length() ;