|
From: <lh...@us...> - 2008-11-24 15:35:49
|
Revision: 234
http://tinytim.svn.sourceforge.net/tinytim/?rev=234&view=rev
Author: lheuer
Date: 2008-11-24 15:35:42 +0000 (Mon, 24 Nov 2008)
Log Message:
-----------
- Added a constructor to JTMTopicMapWriter to specify the encoding (default: utf-8)
- Minor changes, mostly docs
Modified Paths:
--------------
tinytim-mio/trunk/src/main/java/org/tinytim/mio/JSONWriter.java
tinytim-mio/trunk/src/main/java/org/tinytim/mio/JTMTopicMapWriter.java
Modified: tinytim-mio/trunk/src/main/java/org/tinytim/mio/JSONWriter.java
===================================================================
--- tinytim-mio/trunk/src/main/java/org/tinytim/mio/JSONWriter.java 2008-11-23 18:17:48 UTC (rev 233)
+++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/JSONWriter.java 2008-11-24 15:35:42 UTC (rev 234)
@@ -25,7 +25,7 @@
* it is good enough to support JTM.
*
* @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a>
- * @version $Rev:$ - $Date:$
+ * @version $Rev$ - $Date$
*/
final class JSONWriter {
@@ -34,27 +34,51 @@
private int _depth;
private boolean _prettify;
- public JSONWriter(OutputStream out) throws IOException {
- _out = new OutputStreamWriter(out, "utf-8");
+ public JSONWriter(OutputStream out, String encoding) throws IOException {
+ _out = new OutputStreamWriter(out, encoding);
}
+ /**
+ * Enables / disables newlines and indentation of JSON elements.
+ * (newlines and indentation is enabled by default)
+ *
+ * @param prettify <tt>true</tt> to enable prettified JSON, otherwise <tt>false</tt>.
+ */
public void setPrettify(boolean prettify) {
_prettify = prettify;
}
+ /**
+ * Returns if newlines and indentation are enabled.
+ *
+ * @return <tt>true</tt> if prettified JSON is enabled, otherwise <tt>false</tt>.
+ */
public boolean getPrettify() {
return _prettify;
}
+ /**
+ * Indicates the start of the serialization.
+ */
public void startDocument() {
_depth = 0;
}
+ /**
+ * Indicates the end of the serialization.
+ *
+ * @throws IOException If an error occurs.
+ */
public void endDocument() throws IOException {
_out.write('\n');
_out.flush();
}
+ /**
+ * Indents a line, iff {@link #getPrettify()} is enabled.
+ *
+ * @throws IOException If an error occurs.
+ */
private void _indent() throws IOException {
if (!_prettify) {
return;
@@ -70,6 +94,11 @@
_out.write(chars);
}
+ /**
+ * Start of a JSON object.
+ *
+ * @throws IOException If an error occurs.
+ */
public void startObject() throws IOException {
if (_wantComma) {
_out.write(',');
@@ -80,18 +109,33 @@
_wantComma = false;
}
+ /**
+ * End of a JSON object.
+ *
+ * @throws IOException If an error occurs.
+ */
public void endObject() throws IOException {
_out.write('}');
_depth--;
_wantComma = true;
}
+ /**
+ * Start of a JSON array.
+ *
+ * @throws IOException If an error occurs.
+ */
public void startArray() throws IOException {
_out.write('[');
_depth++;
_wantComma = false;
}
+ /**
+ * End of a JSON array.
+ *
+ * @throws IOException If an error occurs.
+ */
public void endArray() throws IOException {
_out.write(']');
_depth--;
@@ -103,8 +147,8 @@
* The writer assumes that the key is a valid JSON string (ensured by
* by JTM) so the keys are not escaped!
*
- * @param key
- * @throws IOException
+ * @param key The key to write.
+ * @throws IOException If an error occurs.
*/
public void key(String key) throws IOException {
if (_wantComma) {
@@ -118,6 +162,12 @@
_wantComma = false;
}
+ /**
+ * The value to write. The value is written in an escaped form.
+ *
+ * @param value The value to write.
+ * @throws IOException If an error occurs.
+ */
public void value(String value) throws IOException {
if (_wantComma) {
_out.write(',');
@@ -127,10 +177,10 @@
}
/**
- *
+ * Escapes a string value.
*
- * @param value
- * @return
+ * @param value The string to escape.
+ * @return An escaped string, usable as JSON value.
*/
public static String escape(String value) {
// Code adapted from JSON.org (JSONObject.quote(String))
@@ -140,12 +190,11 @@
char c = 0;
char[] chars = value.toCharArray();
StringBuilder sb = new StringBuilder(chars.length + 4);
- String t;
sb.append('"');
- for (int i = 0; i < chars.length; i += 1) {
+ for (int i=0; i<chars.length; i++) {
b = c;
c = chars[i];
- switch (chars[i]) {
+ switch (c) {
case '\\':
case '"':
sb.append('\\');
@@ -175,9 +224,10 @@
default:
if (c < ' ' || (c >= '\u0080' && c < '\u00a0') ||
(c >= '\u2000' && c < '\u2100')) {
- t = "000" + Integer.toHexString(c);
- sb.append("\\u" + t.substring(t.length() - 4));
- } else {
+ sb.append("\\u000")
+ .append(Integer.toHexString(c));
+ }
+ else {
sb.append(c);
}
}
Modified: tinytim-mio/trunk/src/main/java/org/tinytim/mio/JTMTopicMapWriter.java
===================================================================
--- tinytim-mio/trunk/src/main/java/org/tinytim/mio/JTMTopicMapWriter.java 2008-11-23 18:17:48 UTC (rev 233)
+++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/JTMTopicMapWriter.java 2008-11-24 15:35:42 UTC (rev 234)
@@ -59,15 +59,30 @@
private Topic _defaultNameType;
/**
+ * Creates a JTM writer, using "utf-8" encoding.
+ *
+ * @param out The stream the JTM is written onto.
+ * @param baseIRI The base IRI which is used to resolve IRIs against.
+ * @throws IOException If an error occurs.
+ */
+ public JTMTopicMapWriter(OutputStream out, String baseIRI) throws IOException {
+ this(out, baseIRI, "utf-8");
+ }
+
+ /**
* Creates a JTM writer.
*
* @param out The stream the JTM is written onto.
* @param baseIRI The base IRI which is used to resolve IRIs against.
+ * @param encoding The encoding to use.
* @throws IOException If an error occurs.
*/
- public JTMTopicMapWriter(OutputStream out, String baseIRI) throws IOException {
+ public JTMTopicMapWriter(OutputStream out, String baseIRI, String encoding) throws IOException {
+ if (encoding == null) {
+ throw new IOException("The encoding must not be null");
+ }
_baseIRI = baseIRI;
- _out = new JSONWriter(out);
+ _out = new JSONWriter(out, encoding);
_out.setPrettify(true);
}
@@ -126,6 +141,16 @@
_out.endDocument();
}
+ /**
+ * Serializes the specified topic.
+ * <p>
+ * The default name type topic is omitted in case it carries no further
+ * characteristics.
+ * </p>
+ *
+ * @param topic The topic to serialize.
+ * @throws IOException If an error occurs.
+ */
private void _writeTopic(Topic topic) throws IOException {
// Ignore the topic if it is the default name type and it has no further
// characteristics
@@ -165,6 +190,12 @@
_out.endObject();
}
+ /**
+ * Serializes the specified name.
+ *
+ * @param name The name to serialize.
+ * @throws IOException If an error occurs.
+ */
private void _writeName(Name name) throws IOException {
_out.startObject();
_writeReifier(name);
@@ -186,6 +217,12 @@
_out.endObject();
}
+ /**
+ * Serializes the specified variant.
+ *
+ * @param variant The variant to serialize.
+ * @throws IOException If an error occurs.
+ */
private void _writeVariant(Variant variant) throws IOException {
_out.startObject();
_writeReifier(variant);
@@ -195,6 +232,12 @@
_out.endObject();
}
+ /**
+ * Serializes the specifed occurrence.
+ *
+ * @param occ The occurrence.
+ * @throws IOException If an error occurs.
+ */
private void _writeOccurrence(Occurrence occ) throws IOException {
_out.startObject();
_writeReifier(occ);
@@ -205,6 +248,12 @@
_out.endObject();
}
+ /**
+ * Writes the value and datatype of an occurrence or variant.
+ *
+ * @param datatyped The datatype-aware construct.
+ * @throws IOException If an error occurs.
+ */
private void _writeDatatypeAware(DatatypeAware datatyped) throws IOException {
Locator datatype = datatyped.getDatatype();
String value = XSD.ANY_URI.equals(datatype) ? datatyped.locatorValue().toExternalForm()
@@ -213,10 +262,24 @@
_writeKeyValue("datatype", datatype.toExternalForm());
}
+ /**
+ * Serializes the item identifiers of the specified construct.
+ *
+ * @param construct The construct to serialize the iids from.
+ * @throws IOException If an error occurs.
+ */
private void _writeItemIdentifiers(Construct construct) throws IOException {
_writeLocators("item_identifiers", construct.getItemIdentifiers());
}
+ /**
+ * Writes a set of locators under the specified name. If the set is
+ * empty, this method does nothing.
+ *
+ * @param name The name (item_identifiers, subject_identifiers, subject_locators)
+ * @param locators A (maybe empty) set of locators.
+ * @throws IOException If an error occurs.
+ */
private void _writeLocators(String name, Set<Locator> locators) throws IOException {
if (locators.isEmpty()) {
return;
@@ -229,6 +292,12 @@
_out.endArray();
}
+ /**
+ * Serializes the specified asssociation.
+ *
+ * @param assoc The association to serialize.
+ * @throws IOException If an error occurs.
+ */
private void _writeAssociation(Association assoc) throws IOException {
Set<Role> roles = assoc.getRoles();
if (roles.isEmpty()) {
@@ -248,6 +317,12 @@
_out.endObject();
}
+ /**
+ * Serializes the specified role.
+ *
+ * @param role The role to serialize.
+ * @throws IOException If an error occurs.
+ */
private void _writeRole(Role role) throws IOException {
_out.startObject();
_writeReifier(role);
@@ -257,10 +332,22 @@
_out.endObject();
}
+ /**
+ * Writes the type of a typed construct.
+ *
+ * @param typed The typed construct.
+ * @throws IOException If an error occurs.
+ */
private void _writeType(Typed typed) throws IOException {
_writeKeyValue("type", _topicRef(typed.getType()));
}
+ /**
+ * Writes the scope.
+ *
+ * @param scoped The scoped construct to retrieve the scope from.
+ * @throws IOException If an error occurs.
+ */
private void _writeScope(Scoped scoped) throws IOException {
IScope scope = ((IScoped) scoped).getScopeObject();
if (scope.isUnconstrained()) {
@@ -303,6 +390,12 @@
_out.endObject();
}
+ /**
+ * Serializes the reifier iff the reifier is not <tt>null</tt>.
+ *
+ * @param reifiable The reifiable construct to retrieve the reifier from.
+ * @throws IOException If an error occurs.
+ */
private void _writeReifier(Reifiable reifiable) throws IOException {
Topic reifier = reifiable.getReifier();
if (reifier == null) {
@@ -311,6 +404,13 @@
_writeKeyValue("reifier", _topicRef(reifier));
}
+ /**
+ * Writes a key/value pair.
+ *
+ * @param key The key to write.
+ * @param value The value to write.
+ * @throws IOException If an error occurs.
+ */
private void _writeKeyValue(String key, String value) throws IOException {
_out.key(key);
_out.value(value);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|