idok-commit AT lists.psi.ch
Subject: Commit emails of the iDok project
List archive
- From: "AFS account Roman Geus" <geus AT savannah.psi.ch>
- To: idok-commit AT lists.psi.ch
- Subject: [idok-commit] idok commit r256 - trunk/java/ch/idok/service/server/search/rest
- Date: Fri, 26 Sep 2008 11:49:38 +0200
- List-archive: <https://lists.web.psi.ch/pipermail/idok-commit/>
- List-id: Commit emails of the iDok project <idok-commit.lists.psi.ch>
Author: geus
Date: Fri Sep 26 11:49:38 2008
New Revision: 256
Log:
Deleted invalid XML characters from match environment, implemented workaround
for bug in Restlet 1.1rc2 (incorrect handling of strings containing multibyte
characters)
Modified:
trunk/java/ch/idok/service/server/search/rest/RestSearchServiceResource.java
Modified:
trunk/java/ch/idok/service/server/search/rest/RestSearchServiceResource.java
==============================================================================
---
trunk/java/ch/idok/service/server/search/rest/RestSearchServiceResource.java
(original)
+++
trunk/java/ch/idok/service/server/search/rest/RestSearchServiceResource.java
Fri Sep 26 11:49:38 2008
@@ -78,6 +78,19 @@
}
/**
+ * Debugging code (will be removed) once the corresponding bug in
Restlet is
+ * fixed
+ */
+ @GET
+ @Produces("text/plain")
+ public Response dsearch() {
+ String result = "Romän";
+ logger.finest("Length: " + result.length() + " "
+ + result.getBytes().length);
+ return Response.ok().entity(result).build();
+ }
+
+ /**
* Resource method for all iDok search queries
*
* @param path
@@ -228,7 +241,9 @@
if ((resultType & SearchService.MATCH_ENVIRONEMENT) != 0) {
Element envElement = doc.createElement("Environment");
- envElement.setTextContent(hit.getEnvironment());
+ // Remove possible invalid XML characters in environment
+ envElement.setTextContent(stripNonValidXMLCharacters(hit
+ .getEnvironment()));
hitElement.appendChild(envElement);
}
@@ -311,19 +326,58 @@
serializer.setOutputProperty(OutputKeys.METHOD, "xml");
serializer.setOutputProperty(OutputKeys.ENCODING,
"ISO-8859-1");
// Use of a non-existing DTD will cause JSNOP to fail in IE6
-// serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
-// "users.dtd");
+ // serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
+ // "users.dtd");
serializer.setOutputProperty(OutputKeys.INDENT, "no");
serializer.transform(source, streamResult);
- String json = new JSONObject().put("xml",
out.toString()).toString();
+ String json = new JSONObject().put("xml", out.toString())
+ .toString();
sb.append(json);
} catch (Throwable e) {
logger.log(Level.FINER, "Could not render XML", e);
return RestExceptionResponse.generateXHTML(e);
}
sb.append(")");
- return Response.ok().entity(sb).type("text/javascript").build();
+// logger.finest(sb.toString());
+// logger.finest("Length: " + sb.toString().length() + " "
+// + sb.toString().getBytes().length);
+ /*
+ * We directly return the byte array since Restlet 1.1rc2
+ * incorrectly deals with strings containing multibyte characters
+ */
+ return Response.ok().entity(sb.toString().getBytes()).type(
+ "text/javascript").build();
} else
return response;
}
+
+ /**
+ * This method ensures that the output String has only valid XML unicode
+ * characters as specified by the XML 1.0 standard. For reference, please
+ * see <a href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char">the
+ * standard</a>. This method will return an empty String if the input is
+ * null or empty.
+ *
+ * @param in
+ * The String whose non-valid characters we want to remove.
+ * @return The in String, stripped of non-valid characters.
+ */
+ public String stripNonValidXMLCharacters(String in) {
+ StringBuffer out = new StringBuffer(); // Used to hold the output.
+ char current; // Used to reference the current character.
+
+ if (in == null || ("".equals(in)))
+ return ""; // vacancy test.
+ for (int i = 0; i < in.length(); i++) {
+ current = in.charAt(i); // NOTE: No IndexOutOfBoundsException
caught
+ // here; it should not happen.
+ if ((current == 0x9) || (current == 0xA) || (current == 0xD)
+ || ((current >= 0x20) && (current <= 0xD7FF))
+ || ((current >= 0xE000) && (current <= 0xFFFD))
+ || ((current >= 0x10000) && (current <= 0x10FFFF)))
+ out.append(current);
+ }
+ return out.toString();
+ }
+
}
- [idok-commit] idok commit r256 - trunk/java/ch/idok/service/server/search/rest, AFS account Roman Geus, 09/26/2008
Archive powered by MHonArc 2.6.19.