Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Christian Willms
hfc
Commits
50307677
Commit
50307677
authored
Jun 12, 2018
by
Bernd Kiefer
Browse files
Added conversion of strings between internal and external format
parent
68eb49f9
Changes
5
Hide whitespace changes
Inline
Side-by-side
src/main/java/de/dfki/lt/hfc/Utils.java
View file @
50307677
...
...
@@ -10,4 +10,65 @@ public class Utils {
return
result
;
}
public
static
String
escapeDoublequotes
(
String
s
)
{
int
l
=
s
.
length
();
if
(
l
==
0
)
return
s
;
StringBuilder
sb
=
new
StringBuilder
((
int
)(
l
*
1.3
));
if
(
s
.
charAt
(
0
)
==
'"'
)
sb
.
append
(
'\\'
);
int
i
,
c
=
0
;
while
((
i
=
s
.
indexOf
(
'"'
,
c
+
1
))
>=
0
)
{
sb
.
append
(
s
.
substring
(
c
,
i
));
if
(
s
.
charAt
(
i
-
1
)
!=
'\\'
)
{
sb
.
append
(
'\\'
);
}
c
=
i
;
}
sb
.
append
(
s
.
substring
(
c
));
return
sb
.
toString
();
}
/** Puts a backslash in front of every backslash and double quote in the
* string s
*/
public
static
String
stringToExternal
(
String
s
)
{
int
l
=
s
.
length
();
if
(
l
==
0
)
return
s
;
StringBuilder
sb
=
new
StringBuilder
((
int
)(
l
*
1.3
));
int
i
=
0
;
do
{
char
c
=
s
.
charAt
(
i
);
if
(
c
==
'"'
||
c
==
'\\'
)
sb
.
append
(
'\\'
);
sb
.
append
(
c
);
++
i
;
}
while
(
i
<
l
);
return
sb
.
toString
();
}
/** Assumes that the input string s was generated by stringToExternal and
* removes backslashes that are not preceded by a backslash.
*/
public
static
String
externalToString
(
String
s
)
{
int
l
=
s
.
length
();
if
(
l
==
0
)
return
s
;
StringBuilder
sb
=
new
StringBuilder
(
l
);
int
i
=
0
;
do
{
char
c
=
s
.
charAt
(
i
);
if
(
c
==
'\\'
)
{
c
=
s
.
charAt
(++
i
);
// only remove the backslash if followed by " or \
if
(
c
!=
'"'
&&
c
!=
'\\'
)
sb
.
append
(
'\\'
);
}
sb
.
append
(
c
);
++
i
;
}
while
(
i
<
l
);
return
sb
.
toString
();
}
/* Factor 5+ slower than the solution above
public static String escapeDQs(String s) {
return s.replaceAll("(^|[^\\\\])\"", "$1\\\\\\\"");
}
*/
}
src/main/java/de/dfki/lt/hfc/types/XsdString.java
View file @
50307677
package
de.dfki.lt.hfc.types
;
import
de.dfki.lt.hfc.Utils
;
/**
* @author (C) Hans-Ulrich Krieger
* @since JDK 1.5
...
...
@@ -29,16 +31,20 @@ public final class XsdString extends XsdAnySimpleType {
this
.
value
=
val
;
return
;
}
int
index
=
val
.
lastIndexOf
(
'^'
);
if
(
index
==
-
1
)
{
// no suffix "^^<xsd:string>"
index
=
val
.
lastIndexOf
(
'@'
);
// if the string is in "external" form, meaning it starts with '"' and
// ends with '"', '"^^<xsd:string>' or '"@langtag', we have to set value
// to the "internal" form, removing backslashes that escape other
// backslashes or double quotes
String
ext
=
null
;
if
(!
val
.
endsWith
(
"^^<xsd:string>"
))
{
// no suffix "^^<xsd:string>"
int
index
=
val
.
lastIndexOf
(
'@'
);
final
int
length
=
val
.
length
();
if
(
index
==
-
1
)
{
// no language tag
if
(
val
.
charAt
(
0
)
==
'"'
&&
val
.
charAt
(
val
.
length
()
-
1
)
==
'"'
)
{
this
.
value
=
val
.
substring
(
1
,
length
-
1
);
ext
=
val
.
substring
(
1
,
length
-
1
);
}
else
{
this
.
value
=
val
;
}
...
...
@@ -46,14 +52,17 @@ public final class XsdString extends XsdAnySimpleType {
}
else
{
// there is a language tag
this
.
value
=
val
.
substring
(
1
,
index
-
1
);
ext
=
val
.
substring
(
1
,
index
-
1
);
this
.
languageTag
=
val
.
substring
(
index
+
1
,
length
);;
}
}
else
{
this
.
value
=
val
.
substring
(
1
,
index
-
2
);
ext
=
val
.
substring
(
1
,
val
.
length
()
-
15
);
this
.
languageTag
=
null
;
}
if
(
ext
!=
null
)
{
this
.
value
=
Utils
.
externalToString
(
ext
);
}
}
/**
...
...
@@ -76,7 +85,7 @@ public final class XsdString extends XsdAnySimpleType {
*/
public
String
toString
(
boolean
shortIsDefault
)
{
StringBuilder
sb
=
new
StringBuilder
(
"\""
);
sb
.
append
(
this
.
value
);
sb
.
append
(
Utils
.
stringToExternal
(
this
.
value
)
)
;
sb
.
append
(
"\""
);
if
(
this
.
languageTag
!=
null
)
{
sb
.
append
(
"@"
);
...
...
src/test/java/de/dfki/lt/hfc/QueryTest.java
View file @
50307677
...
...
@@ -219,7 +219,7 @@ public class QueryTest {
expected
=
new
String
[][]{{
"\"1\"^^<xsd:int>"
,
"\"1\"^^<xsd:int>"
}};
query
=
new
Query
(
fc
.
tupleStore
);
bt
=
query
.
query
(
"SELECT ?s WHERE ?s <dom:bsl> ?o ?t FILTER ?s != <pal:labval33> AGGREGATE ?number = CountDistinct ?s & ?number2 = CountDistinct <pal:labval33>"
)
;
checkResult
(
fc
,
bt
,
expected
,
"?number"
,
"?
subject
"
);
checkResult
(
fc
,
bt
,
expected
,
"?number"
,
"?
number2
"
);
}
@Test
...
...
src/test/java/de/dfki/lt/hfc/UtilsTest.java
View file @
50307677
...
...
@@ -55,4 +55,139 @@ public class UtilsTest {
reverse
(
in
);
assertArrayEquals
(
expstring
(
exp
,
in
),
exp
,
in
);
}
@Test
public
void
testEscape1
()
{
String
in
=
"abcdefg"
;
String
res
=
Utils
.
escapeDoublequotes
(
in
);
assertEquals
(
in
,
res
);
}
@Test
public
void
testEscape2
()
{
String
in
=
"abc\"de\"fg"
;
String
res
=
Utils
.
escapeDoublequotes
(
in
);
assertEquals
(
"abc\\\"de\\\"fg"
,
res
);
}
@Test
public
void
testEscape3
()
{
String
in
=
"abc\"de\\\"fg"
;
String
res
=
Utils
.
escapeDoublequotes
(
in
);
assertEquals
(
"abc\\\"de\\\"fg"
,
res
);
}
@Test
public
void
testEscape4
()
{
String
in
=
"\"abcde\""
;
String
res
=
Utils
.
escapeDoublequotes
(
in
);
assertEquals
(
"\\\"abcde\\\""
,
res
);
}
@Test
public
void
testEscape5
()
{
String
in
=
"\\\"abcde\\\""
;
String
res
=
Utils
.
escapeDoublequotes
(
in
);
assertEquals
(
"\\\"abcde\\\""
,
res
);
}
@Test
public
void
testEscape6
()
{
String
in
=
""
;
String
res
=
Utils
.
escapeDoublequotes
(
in
);
assertEquals
(
""
,
res
);
}
@Test
public
void
testEscape11
()
{
String
in
=
"\"abc\"aldsjf;lasjkdf\\\"de\"\\\"abc\\\"aldsjf;lasjkdf\\\\\\\""
+
"de\\\"\\\"abc\\\"aldsjf;lasjkdf\\\\\\\""
+
"de\\\"\\\"abc\\\"aldsjf;lasjkdf\\\\\\\"de\\\""
;
String
res
=
null
;
for
(
int
i
=
0
;
i
<
1
;
++
i
)
{
res
=
Utils
.
escapeDoublequotes
(
in
);
}
assertEquals
(
"\\\"abc\\\"aldsjf;lasjkdf\\\"de\\\"\\\"abc\\\"aldsjf;lasjkdf\\\\\\\""
+
"de\\\"\\\"abc\\\"aldsjf;lasjkdf\\\\\\\""
+
"de\\\"\\\"abc\\\"aldsjf;lasjkdf\\\\\\\"de\\\""
,
res
);
}
@Test
public
void
testIntToExt1
()
{
String
in
=
"abcdefg"
;
String
ex
=
"abcdefg"
;
String
res
=
Utils
.
stringToExternal
(
in
);
assertEquals
(
ex
,
res
);
res
=
Utils
.
externalToString
(
ex
);
assertEquals
(
in
,
res
);
}
@Test
public
void
testIntToExt2
()
{
String
in
=
"abc\"de\"fg"
;
String
ex
=
"abc\\\"de\\\"fg"
;
String
res
=
Utils
.
stringToExternal
(
in
);
assertEquals
(
ex
,
res
);
res
=
Utils
.
externalToString
(
ex
);
assertEquals
(
in
,
res
);
}
@Test
public
void
testIntToExt3
()
{
String
in
=
"abc\"de\\\"fg"
;
String
ex
=
"abc\\\"de\\\\\\\"fg"
;
String
res
=
Utils
.
stringToExternal
(
in
);
assertEquals
(
ex
,
res
);
res
=
Utils
.
externalToString
(
ex
);
assertEquals
(
in
,
res
);
}
@Test
public
void
testIntToExt4
()
{
String
in
=
"\"abcde\""
;
String
ex
=
"\\\"abcde\\\""
;
String
res
=
Utils
.
stringToExternal
(
in
);
assertEquals
(
ex
,
res
);
res
=
Utils
.
externalToString
(
ex
);
assertEquals
(
in
,
res
);
}
@Test
public
void
testIntToExt5
()
{
String
in
=
"\\\"abcde\\\""
;
String
ex
=
"\\\\\\\"abcde\\\\\\\""
;
String
res
=
Utils
.
stringToExternal
(
in
);
assertEquals
(
ex
,
res
);
res
=
Utils
.
externalToString
(
ex
);
assertEquals
(
in
,
res
);
}
@Test
public
void
testIntToExt6
()
{
String
in
=
""
;
String
ex
=
""
;
String
res
=
Utils
.
stringToExternal
(
in
);
assertEquals
(
ex
,
res
);
res
=
Utils
.
externalToString
(
ex
);
assertEquals
(
in
,
res
);
}
@Test
public
void
testIntToExt11
()
{
String
in
=
"\"abc\"aldsjf;lasjkdf\\\"de\"\\\"abc\\\"aldsjf;lasjkdf\\\\\\\""
+
"de\\\"\\\"abc\\\"aldsjf;lasjkdf\\\\\\\""
+
"de\\\"\\\"abc\\\"aldsjf;lasjkdf\\\\\\\"de\\\""
;
String
ex
=
"\\\"abc\\\"aldsjf;lasjkdf\\\\\\\"de\\\"\\\\\\\"abc\\\\\\\"aldsjf;lasjkdf\\\\\\\\\\\\\\\""
+
"de\\\\\\\"\\\\\\\"abc\\\\\\\"aldsjf;lasjkdf\\\\\\\\\\\\\\\""
+
"de\\\\\\\"\\\\\\\"abc\\\\\\\"aldsjf;lasjkdf\\\\\\\\\\\\\\\"de\\\\\\\""
;
String
res
=
null
;
for
(
int
i
=
0
;
i
<
1000000
;
++
i
)
{
res
=
Utils
.
stringToExternal
(
in
);
}
assertEquals
(
ex
,
res
);
for
(
int
i
=
0
;
i
<
1
;
++
i
)
{
res
=
Utils
.
externalToString
(
ex
);
}
assertEquals
(
in
,
res
);
}
}
src/test/java/de/dfki/lt/hfc/XsdSimpleTypesTest.java
View file @
50307677
...
...
@@ -329,6 +329,32 @@ public class XsdSimpleTypesTest {
String
res
=
new
XsdString
(
"val"
).
toString
(
true
);
assertEquals
(
"\"val\"^^<xsd:string>"
,
res
);
xs
=
new
XsdString
(
"he\\llo"
,
"en"
);
assertEquals
(
"he\\llo"
,
xs
.
value
);
assertEquals
(
"en"
,
xs
.
languageTag
);
assertEquals
(
"\"he\\\\llo\"@en"
,
xs
.
toString
(
true
));
assertEquals
(
"\"he\\\\llo\"@en"
,
xs
.
toString
(
false
));
xs
=
new
XsdString
(
"\"he\\llo\\"
,
"en"
);
assertEquals
(
"\"he\\llo\\"
,
xs
.
value
);
assertEquals
(
"en"
,
xs
.
languageTag
);
assertEquals
(
"\"\\\"he\\\\llo\\\\\"@en"
,
xs
.
toString
(
true
));
assertEquals
(
"\"\\\"he\\\\llo\\\\\"@en"
,
xs
.
toString
(
false
));
xs
=
new
XsdString
(
"he\\llo"
);
assertEquals
(
"he\\llo"
,
xs
.
value
);
assertEquals
(
null
,
xs
.
languageTag
);
assertEquals
(
"\"he\\\\llo\"^^<xsd:string>"
,
xs
.
toString
(
true
));
assertEquals
(
"\"he\\\\llo\"^^<http://www.w3.org/2001/XMLSchema#string>"
,
xs
.
toString
(
false
));
xs
=
new
XsdString
(
"\"he\\llo\\"
);
assertEquals
(
"\"he\\llo\\"
,
xs
.
value
);
assertEquals
(
null
,
xs
.
languageTag
);
assertEquals
(
"\"\\\"he\\\\llo\\\\\"^^<xsd:string>"
,
xs
.
toString
(
true
));
assertEquals
(
"\"\\\"he\\\\llo\\\\\"^^<http://www.w3.org/2001/XMLSchema#string>"
,
xs
.
toString
(
false
));
}
@Test
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment