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 2015/12/22 18:34:48 UTC
[2/4] jena git commit: QuotedStringOutput replaces EscapeProc
QuotedStringOutput replaces EscapeProc
Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/72bcb915
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/72bcb915
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/72bcb915
Branch: refs/heads/master
Commit: 72bcb91525832e8f74d73bd8e6adb76cf5d92f39
Parents: af17137
Author: Andy Seaborne <an...@apache.org>
Authored: Sun Dec 20 17:33:00 2015 +0000
Committer: Andy Seaborne <an...@apache.org>
Committed: Tue Dec 22 17:32:00 2015 +0000
----------------------------------------------------------------------
.../apache/jena/riot/out/NodeFormatterNT.java | 66 +++++++------
.../apache/jena/riot/out/NodeFormatterTTL.java | 40 +++++---
.../riot/out/quoted/QuotedStringOutput.java | 32 +++++++
.../riot/out/quoted/QuotedStringOutputBase.java | 52 +++++++++++
.../riot/out/quoted/QuotedStringOutputNT.java | 34 +++++++
.../riot/out/quoted/QuotedStringOutputTTL.java | 41 +++++++++
.../quoted/QuotedStringOutputTTL_MultiLine.java | 55 +++++++++++
.../apache/jena/riot/out/quoted/QuotedURI.java | 49 ++++++++++
.../java/org/apache/jena/riot/out/TS_Out.java | 3 +-
.../jena/riot/out/TestQuotedStringOutput.java | 97 ++++++++++++++++++++
10 files changed, 421 insertions(+), 48 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/jena/blob/72bcb915/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFormatterNT.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFormatterNT.java b/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFormatterNT.java
index 5a260c7..c96eb61 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFormatterNT.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFormatterNT.java
@@ -20,67 +20,65 @@ package org.apache.jena.riot.out;
import org.apache.jena.atlas.io.AWriter ;
+import org.apache.jena.atlas.lib.CharSpace ;
+import org.apache.jena.atlas.lib.EscapeStr ;
+import org.apache.jena.riot.out.quoted.QuotedStringOutput ;
+import org.apache.jena.riot.out.quoted.QuotedStringOutputNT ;
+import org.apache.jena.riot.out.quoted.QuotedURI ;
public class NodeFormatterNT extends NodeFormatterBase
{
// Formatting for NTriples
// Turtles extends this class to intercept forms it can do better.
- private final EscapeProc escapeProc ;
+ private final QuotedStringOutput quotedStringProc ;
+ private final QuotedURI quotedUriProc ;
public NodeFormatterNT() { this(CharSpace.UTF8) ; }
- public NodeFormatterNT(CharSpace charSpace) { escapeProc = new EscapeProc(charSpace) ;}
+ public NodeFormatterNT(CharSpace charSpace) {
+ quotedStringProc = new QuotedStringOutputNT(charSpace);
+ quotedUriProc = new QuotedURI(charSpace) ;
+ }
@Override
- public void formatURI(AWriter w, String uriStr)
- {
- w.print('<') ;
- escapeProc.writeURI(w, uriStr) ;
- w.print('>') ;
+ public void formatURI(AWriter w, String uriStr) {
+ quotedUriProc.writeURI(w, uriStr);
}
@Override
- public void formatVar(AWriter w, String name)
- {
- w.print('?') ;
- escapeProc.writeStr(w, name) ;
+ public void formatVar(AWriter w, String name) {
+ w.print('?');
+ EscapeStr.stringEsc(w, name, false);
}
@Override
- public void formatBNode(AWriter w, String label)
- {
- w.print("_:") ;
- String lab = NodeFmtLib.encodeBNodeLabel(label) ;
- w.print(lab) ;
+ public void formatBNode(AWriter w, String label) {
+ w.print("_:");
+ String lab = NodeFmtLib.encodeBNodeLabel(label);
+ w.print(lab);
}
@Override
- public void formatLitString(AWriter w, String lex)
- {
- writeEscaped(w, lex) ;
+ public void formatLitString(AWriter w, String lex) {
+ writeEscaped(w, lex);
}
- private void writeEscaped(AWriter w, String lex)
- {
- w.print('"') ;
- escapeProc.writeStr(w, lex) ;
- w.print('"') ;
+ private void writeEscaped(AWriter w, String lex) {
+ quotedStringProc.writeStr(w, lex);
}
@Override
- public void formatLitLang(AWriter w, String lex, String langTag)
- {
- writeEscaped(w, lex) ;
- w.print('@') ;
- w.print(langTag) ;
+ public void formatLitLang(AWriter w, String lex, String langTag) {
+ writeEscaped(w, lex);
+ w.print('@');
+ w.print(langTag);
}
@Override
- public void formatLitDT(AWriter w, String lex, String datatypeURI)
- {
- writeEscaped(w, lex) ;
- w.print("^^") ;
- formatURI(w, datatypeURI) ;
+ public void formatLitDT(AWriter w, String lex, String datatypeURI) {
+ writeEscaped(w, lex);
+ w.print("^^");
+ formatURI(w, datatypeURI);
}
}
http://git-wip-us.apache.org/repos/asf/jena/blob/72bcb915/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFormatterTTL.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFormatterTTL.java b/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFormatterTTL.java
index c4b2fbe..ca8fabe 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFormatterTTL.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/out/NodeFormatterTTL.java
@@ -21,6 +21,7 @@ package org.apache.jena.riot.out ;
import java.net.MalformedURLException ;
import org.apache.jena.atlas.io.AWriter ;
+import org.apache.jena.atlas.lib.CharSpace ;
import org.apache.jena.atlas.lib.Pair ;
import org.apache.jena.datatypes.xsd.XSDDatatype ;
import org.apache.jena.graph.Node ;
@@ -31,6 +32,7 @@ import org.apache.jena.riot.system.PrefixMap ;
import org.apache.jena.riot.system.PrefixMapFactory ;
import org.apache.jena.riot.system.RiotChars ;
+/** Node formatter for Turtle using single line strings */
public class NodeFormatterTTL extends NodeFormatterNT
{
private final NodeToLabel nodeToLabel ;
@@ -206,37 +208,49 @@ public class NodeFormatterTTL extends NodeFormatterNT
@Override
public void formatLitDT(AWriter w, String lex, String datatypeURI) {
+ boolean b = writeLiteralAbbreviated(w, lex, datatypeURI) ;
+ if ( b ) return ;
+ writeLiteralLongForm(w, lex, datatypeURI) ;
+ }
+
+ protected void writeLiteralLongForm(AWriter w, String lex, String datatypeURI) {
+ writeLiteralOneLine(w, lex, datatypeURI);
+ }
+
+ protected void writeLiteralOneLine(AWriter w, String lex, String datatypeURI) {
+ super.formatLitDT(w, lex, datatypeURI) ;
+ }
+
+ /** Write in a short form, e.g. integer.
+ * @return True if a short form was output else false.
+ */
+ protected boolean writeLiteralAbbreviated(AWriter w, String lex, String datatypeURI) {
if ( dtDecimal.equals(datatypeURI) ) {
if ( validDecimal(lex) ) {
w.print(lex) ;
- return ;
+ return true ;
}
} else if ( dtInteger.equals(datatypeURI) ) {
if ( validInteger(lex) ) {
w.print(lex) ;
- return ;
+ return true ;
}
- }
- if ( dtDouble.equals(datatypeURI) ) {
+ } else if ( dtDouble.equals(datatypeURI) ) {
if ( validDouble(lex) ) {
w.print(lex) ;
- return ;
+ return true ;
}
- }
- // Boolean
- if ( dtBoolean.equals(datatypeURI) ) {
+ } else if ( dtBoolean.equals(datatypeURI) ) {
// We leave "0" and "1" as-is assumign that if written like that,
// there was a reason.
if ( lex.equals("true") || lex.equals("false") ) {
w.print(lex) ;
- return ;
+ return true ;
}
}
-
- // else.
- super.formatLitDT(w, lex, datatypeURI) ;
+ return false ;
}
-
+
private static boolean validInteger(String lex) {
int N = lex.length() ;
if ( N == 0 )
http://git-wip-us.apache.org/repos/asf/jena/blob/72bcb915/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedStringOutput.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedStringOutput.java b/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedStringOutput.java
new file mode 100644
index 0000000..35d2968
--- /dev/null
+++ b/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedStringOutput.java
@@ -0,0 +1,32 @@
+/**
+ * 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.apache.jena.riot.out.quoted;
+
+import org.apache.jena.atlas.io.AWriter ;
+
+/** Interface for writing quoted strings for Turtle, TriG, N-triples and N-Quads */
+public interface QuotedStringOutput {
+ public char getQuoteChar() ;
+
+ /** Write a string on one line */
+ public void writeStr(AWriter writer, String s) ;
+
+ /** Write a string, possible on several lines */
+ public void writeStrMultiLine(AWriter writer, String s) ;
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/72bcb915/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedStringOutputBase.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedStringOutputBase.java b/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedStringOutputBase.java
new file mode 100644
index 0000000..db62bb0
--- /dev/null
+++ b/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedStringOutputBase.java
@@ -0,0 +1,52 @@
+/**
+ * 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.apache.jena.riot.out.quoted;
+
+import org.apache.jena.atlas.io.AWriter ;
+import org.apache.jena.atlas.lib.CharSpace ;
+import org.apache.jena.atlas.lib.EscapeStr ;
+
+/** Quoted strign output - single line, settable quote character and char space. */
+public class QuotedStringOutputBase implements QuotedStringOutput {
+ protected final CharSpace charSpace ;
+ protected final char quoteChar;
+
+ protected QuotedStringOutputBase(char quoteChar, CharSpace charSpace) {
+ this.charSpace = charSpace ;
+ this.quoteChar = quoteChar ;
+ }
+
+ @Override
+ public char getQuoteChar() { return quoteChar ; }
+
+ @Override
+ public void writeStr(AWriter writer, String str) {
+ // Only " strings in N-Triples/N-Quads
+ writer.print(getQuoteChar());
+ EscapeStr.stringEsc(writer, str, getQuoteChar(), true, charSpace) ;
+ writer.print(getQuoteChar());
+ }
+
+ @Override
+ public void writeStrMultiLine(AWriter writer, String str) {
+ // No multiline strings in N-Triples/N-Quads.
+ writeStr(writer, str) ;
+ }
+}
+
http://git-wip-us.apache.org/repos/asf/jena/blob/72bcb915/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedStringOutputNT.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedStringOutputNT.java b/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedStringOutputNT.java
new file mode 100644
index 0000000..ec3c9f5
--- /dev/null
+++ b/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedStringOutputNT.java
@@ -0,0 +1,34 @@
+/**
+ * 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.apache.jena.riot.out.quoted;
+
+import org.apache.jena.atlas.lib.CharSpace ;
+import org.apache.jena.atlas.lib.Chars ;
+
+/** Escape processor for N-Triples/N-Quads */
+public class QuotedStringOutputNT extends QuotedStringOutputBase {
+
+ public QuotedStringOutputNT() {
+ this(CharSpace.UTF8) ;
+ }
+
+ public QuotedStringOutputNT(CharSpace charSpace) {
+ super(Chars.CH_QUOTE2, charSpace) ;
+ }
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/72bcb915/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedStringOutputTTL.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedStringOutputTTL.java b/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedStringOutputTTL.java
new file mode 100644
index 0000000..0ac1257
--- /dev/null
+++ b/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedStringOutputTTL.java
@@ -0,0 +1,41 @@
+/**
+ * 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.apache.jena.riot.out.quoted;
+
+import org.apache.jena.atlas.lib.CharSpace ;
+import org.apache.jena.atlas.lib.Chars ;
+
+/** Escape processor for Turtle. */
+public class QuotedStringOutputTTL extends QuotedStringOutputBase {
+
+ public QuotedStringOutputTTL() {
+ this(Chars.CH_QUOTE2, CharSpace.UTF8) ;
+ }
+
+ /** Always use the given quote character (0 means use " or ' as needed) */
+ public QuotedStringOutputTTL(char quoteChar) {
+ this(quoteChar, CharSpace.UTF8) ;
+ }
+
+ /** Turtle is UTF-8 : ASCII is for non-standard needs */
+ protected QuotedStringOutputTTL(char quoteChar, CharSpace charSpace) {
+ super(quoteChar, charSpace) ;
+ }
+}
+
http://git-wip-us.apache.org/repos/asf/jena/blob/72bcb915/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedStringOutputTTL_MultiLine.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedStringOutputTTL_MultiLine.java b/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedStringOutputTTL_MultiLine.java
new file mode 100644
index 0000000..1f8d869
--- /dev/null
+++ b/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedStringOutputTTL_MultiLine.java
@@ -0,0 +1,55 @@
+/**
+ * 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.apache.jena.riot.out.quoted;
+
+import org.apache.jena.atlas.io.AWriter ;
+import org.apache.jena.atlas.lib.CharSpace ;
+import org.apache.jena.atlas.lib.EscapeStr ;
+
+public class QuotedStringOutputTTL_MultiLine extends QuotedStringOutputTTL {
+
+ public QuotedStringOutputTTL_MultiLine() {
+ super() ;
+ }
+
+ /** Always use the given quote character */
+ public QuotedStringOutputTTL_MultiLine(char quoteChar) {
+ super(quoteChar) ;
+ }
+
+ /** Turtle is UTF-8 : ASCII is for non-standard needs */
+ protected QuotedStringOutputTTL_MultiLine(char quoteChar, CharSpace charSpace) {
+ super(quoteChar, charSpace) ;
+ }
+
+ /** Write a string using triple quotes. Try to avoid escapes by looking for the quote character first. */
+ @Override
+ public void writeStrMultiLine(AWriter w, String s) {
+ quote3(w) ;
+ EscapeStr.stringEsc(w, s, quoteChar, false, charSpace);
+ quote3(w) ;
+ }
+
+ private void quote3(AWriter w) {
+ w.print(quoteChar) ;
+ w.print(quoteChar) ;
+ w.print(quoteChar) ;
+ }
+}
+
http://git-wip-us.apache.org/repos/asf/jena/blob/72bcb915/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedURI.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedURI.java b/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedURI.java
new file mode 100644
index 0000000..3d0292e
--- /dev/null
+++ b/jena-arq/src/main/java/org/apache/jena/riot/out/quoted/QuotedURI.java
@@ -0,0 +1,49 @@
+/**
+ * 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.apache.jena.riot.out.quoted;
+
+import org.apache.jena.atlas.io.AWriter ;
+import org.apache.jena.atlas.lib.CharSpace ;
+import org.apache.jena.atlas.lib.Chars ;
+import org.apache.jena.atlas.lib.EscapeStr ;
+
+public class QuotedURI {
+ private final CharSpace charSpace ;
+
+ public QuotedURI() {
+ this(CharSpace.UTF8) ;
+ }
+
+ public QuotedURI(CharSpace charSpace) {
+ this.charSpace = charSpace ;
+ }
+
+ /** Write a string for a URI on one line. */
+ public void writeURI(AWriter w, String s) {
+ // URIs do not have an escape mechanism. %-is an encoding.
+ // We can either print as-is or convert to %-encoding.
+ // (Ignoring host names which be in puny code).
+ w.print(Chars.CH_LT);
+ if ( CharSpace.isAscii(charSpace) )
+ EscapeStr.writeASCII(w, s) ;
+ else
+ w.print(s) ;
+ w.print(Chars.CH_GT);
+ }
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/72bcb915/jena-arq/src/test/java/org/apache/jena/riot/out/TS_Out.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/org/apache/jena/riot/out/TS_Out.java b/jena-arq/src/test/java/org/apache/jena/riot/out/TS_Out.java
index a093312..f13735c 100644
--- a/jena-arq/src/test/java/org/apache/jena/riot/out/TS_Out.java
+++ b/jena-arq/src/test/java/org/apache/jena/riot/out/TS_Out.java
@@ -24,7 +24,8 @@ import org.junit.runners.Suite ;
@RunWith(Suite.class)
@Suite.SuiteClasses( {
- TestNodeFmt.class
+ TestQuotedStringOutput.class
+ , TestNodeFmt.class
, TestNodeFmtLib.class
})
http://git-wip-us.apache.org/repos/asf/jena/blob/72bcb915/jena-arq/src/test/java/org/apache/jena/riot/out/TestQuotedStringOutput.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/org/apache/jena/riot/out/TestQuotedStringOutput.java b/jena-arq/src/test/java/org/apache/jena/riot/out/TestQuotedStringOutput.java
new file mode 100644
index 0000000..6336ddd
--- /dev/null
+++ b/jena-arq/src/test/java/org/apache/jena/riot/out/TestQuotedStringOutput.java
@@ -0,0 +1,97 @@
+/**
+ * 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.apache.jena.riot.out;
+
+import static org.junit.Assert.assertEquals ;
+
+import org.apache.jena.atlas.io.StringWriterI ;
+import org.apache.jena.riot.out.quoted.QuotedStringOutput ;
+import org.apache.jena.riot.out.quoted.QuotedStringOutputNT ;
+import org.apache.jena.riot.out.quoted.QuotedStringOutputTTL ;
+import org.apache.jena.riot.out.quoted.QuotedStringOutputTTL_MultiLine ;
+import org.junit.Test ;
+
+public class TestQuotedStringOutput {
+
+ static void testSingleLine(QuotedStringOutput proc, String input, String expected) {
+ StringWriterI w = new StringWriterI() ;
+ proc.writeStr(w, input);
+ String output = w.toString() ;
+ expected = proc.getQuoteChar()+expected+proc.getQuoteChar() ;
+ assertEquals(expected, output) ;
+ }
+
+ static void testMultiLine(QuotedStringOutput proc, String input, String expected) {
+ StringWriterI w = new StringWriterI() ;
+ proc.writeStrMultiLine(w, input);
+ String output = w.toString() ;
+ assertEquals(expected, output) ;
+ }
+
+ private QuotedStringOutput escProcNT = new QuotedStringOutputNT() ;
+ private QuotedStringOutput escProcTTL_S2 = new QuotedStringOutputTTL('"') ;
+ private QuotedStringOutput escProcTTL_S1 = new QuotedStringOutputTTL('\'') ;
+
+ private QuotedStringOutput escProcTTL_M2 = new QuotedStringOutputTTL_MultiLine('"') ;
+ private QuotedStringOutput escProcTTL_M1 = new QuotedStringOutputTTL_MultiLine('\'') ;
+
+ @Test public void escape_nt_00() { testSingleLine(escProcNT, "", "") ; }
+ @Test public void escape_nt_01() { testSingleLine(escProcNT, "abc", "abc") ; }
+ @Test public void escape_nt_02() { testSingleLine(escProcNT, "abc\ndef", "abc\\ndef") ; }
+
+ @Test public void escape_nt_03() { testSingleLine(escProcNT, "\"", "\\\"") ; }
+ @Test public void escape_nt_04() { testSingleLine(escProcNT, "'", "'") ; }
+ @Test public void escape_nt_05() { testSingleLine(escProcNT, "xyz\t", "xyz\\t") ; }
+
+ @Test public void escape_ttl_singleline_quote2_00() { testSingleLine(escProcTTL_S2, "", "") ; }
+ @Test public void escape_ttl_singleline_quote2_01() { testSingleLine(escProcTTL_S2, "abc", "abc") ; }
+ @Test public void escape_ttl_singleline_quote2_02() { testSingleLine(escProcTTL_S2, "abc\ndef", "abc\\ndef") ; }
+
+ @Test public void escape_ttl_singleline_quote2_03() { testSingleLine(escProcTTL_S2, "\"", "\\\"") ; }
+ @Test public void escape_ttl_singleline_quote2_04() { testSingleLine(escProcTTL_S2, "'", "'") ; }
+ @Test public void escape_ttl_singleline_quote2_05() { testSingleLine(escProcTTL_S2, "xyz\t", "xyz\\t") ; }
+
+ @Test public void escape_ttl_singleline_quote1_00() { testSingleLine(escProcTTL_S1, "", "") ; }
+ @Test public void escape_ttl_singleline_quote1_01() { testSingleLine(escProcTTL_S1, "abc", "abc") ; }
+ @Test public void escape_ttl_singleline_quote1_02() { testSingleLine(escProcTTL_S1, "abc\ndef", "abc\\ndef") ; }
+
+ @Test public void escape_ttl_singleline_quote1_03() { testSingleLine(escProcTTL_S1, "\"", "\"") ; }
+ @Test public void escape_ttl_singleline_quote1_04() { testSingleLine(escProcTTL_S1, "'", "\\'") ; }
+ @Test public void escape_ttl_singleline_quote1_05() { testSingleLine(escProcTTL_S1, "xyz\t", "xyz\\t") ; }
+
+ // Multiple with single line output processor.
+ @Test public void escape_ttl_singleline_quote2_multiline_str0() { testMultiLine(escProcTTL_S2, "", "\"\"") ; }
+ @Test public void escape_ttl_singleline_quote2_multiline_str1() { testMultiLine(escProcTTL_S2, "abc", "\"abc\"") ; }
+ @Test public void escape_ttl_singleline_quote2_multiline_str2() { testMultiLine(escProcTTL_S2, "abc\ndef", "\"abc\\ndef\"") ; }
+
+ @Test public void escape_ttl_singleline_quote2_multiline_str3() { testMultiLine(escProcTTL_S2, "\"", "\"\\\"\"") ; }
+ @Test public void escape_ttl_singleline_quote2_multiline_str4() { testMultiLine(escProcTTL_S2, "'", "\"'\"") ; }
+ @Test public void escape_ttl_singleline_quote2_multiline_str5() { testMultiLine(escProcTTL_S2, "xyz\t", "\"xyz\\t\"") ; }
+
+ // Multiple with single line output processor.
+ @Test public void escape_ttl_singleline_quote1_multiline_str0() { testMultiLine(escProcTTL_S1, "", "''") ; }
+ @Test public void escape_ttl_singleline_quote1_multiline_str1() { testMultiLine(escProcTTL_S1, "abc", "'abc'") ; }
+ @Test public void escape_ttl_singleline_quote1_multiline_str2() { testMultiLine(escProcTTL_S1, "abc\ndef", "'abc\\ndef'") ; }
+
+ @Test public void escape_ttl_singleline_quote1_multiline_str3() { testMultiLine(escProcTTL_S1, "\"", "'\"'") ; }
+ @Test public void escape_ttl_singleline_quote1_multiline_str4() { testMultiLine(escProcTTL_S1, "'", "'\\''") ; }
+ @Test public void escape_ttl_singleline_quote1_multiline_str5() { testMultiLine(escProcTTL_S1, "xyz\t", "'xyz\\t'") ; }
+
+ // Multiline, multiline
+}