You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by ju...@apache.org on 2005/05/09 13:40:51 UTC
svn commit: r169294 - in
/incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml:
DocumentViewExportVisitor.java SystemViewImportContentHandler.java
Author: jukka
Date: Mon May 9 04:40:50 2005
New Revision: 169294
URL: http://svn.apache.org/viewcvs?rev=169294&view=rev
Log:
JCR-EXT: Upgrade to 0.16.4.1
Modified:
incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/DocumentViewExportVisitor.java
incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/SystemViewImportContentHandler.java
Modified: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/DocumentViewExportVisitor.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/DocumentViewExportVisitor.java?rev=169294&r1=169293&r2=169294&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/DocumentViewExportVisitor.java (original)
+++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/DocumentViewExportVisitor.java Mon May 9 04:40:50 2005
@@ -66,11 +66,7 @@
*
* protected boolean includeProperty(Property property)
* throws RepositoryException {
- * if (property.getName().equals("title")) {
- * return super.includeProperty(property);
- * } else {
- * return false;
- * }
+ * return property.getName().equals("title");
* }
*
* protected boolean includeNode(Node node)
@@ -131,11 +127,14 @@
*/
public class DocumentViewExportVisitor implements ItemVisitor {
+ /** The JCR namespace URI. */
+ private static final String JCR_URI = "http://www.jcp.org/jcr/1.0";
+
/** The special jcr:xmltext property name. */
- private static final String XMLTEXT = "jcr:xmltext";
+ private static final Name XMLTEXT = new Name(JCR_URI, "xmltext");
/** The special jcr:xmlcharacters property name. */
- private static final String XMLCHARACTERS = "jcr:xmlcharacters";
+ private static final Name XMLCHARACTERS = new Name(JCR_URI, "xmlcharacters");
/**
* The SAX content handler for the serialized XML stream.
@@ -182,7 +181,7 @@
* @param property ignored property
* @see ItemVisitor#visit(Property)
*/
- public void visit(Property property) {
+ public final void visit(Property property) {
}
/**
@@ -200,7 +199,7 @@
* @see #includeProperty(Property)
* @see #includeNode(Node)
*/
- public void visit(Node node) throws RepositoryException {
+ public final void visit(Node node) throws RepositoryException {
try {
// start document
if (root == null) {
@@ -235,14 +234,9 @@
/**
* Checks whether the given property should be included in the XML
- * serialization. By default this method returns false for the special
- * "jcr:xmlcharacters" properties and for binary properties if the
- * skipBinary flag is set. Subclasses can extend this behaviour to
- * implement more selective XML serialization.
- * <p>
- * To avoid losing the default behaviour described above, subclasses
- * should always call super.includeProperty(property) instead of
- * simply returning true for a property.
+ * serialization. This method returns <code>true</code> by default,
+ * but subclasses can override this method to implement more selective
+ * XML serialization.
*
* @param property the property to check
* @return true if the property should be included, false otherwise
@@ -250,14 +244,14 @@
*/
protected boolean includeProperty(Property property)
throws RepositoryException {
- return !property.getName().equals(XMLCHARACTERS)
- && (!skipBinary || property.getType() != PropertyType.BINARY);
+ return true;
}
/**
* Checks whether the given node should be included in the XML
- * serialization. This method returns true by default, but subclasses
- * can extend this behaviour to implement selective XML serialization.
+ * serialization. This method returns <code>true</code> by default,
+ * but subclasses override this method to implement selective
+ * XML serialization.
* <p>
* Note that this method is only called for the descendants of the
* root node of the serialized tree. Also, this method is never called
@@ -285,7 +279,8 @@
private void exportText(Node node)
throws SAXException, RepositoryException {
try {
- Property property = node.getProperty(XMLCHARACTERS);
+ Property property =
+ node.getProperty(XMLCHARACTERS.toJCRName(node.getSession()));
char[] characters = property.getString().toCharArray();
handler.characters(characters, 0, characters.length);
} catch (PathNotFoundException ex) {
@@ -307,15 +302,15 @@
*/
private void exportNode(Node node)
throws SAXException, RepositoryException {
- Name qname = getQName(node);
- String localName = escapeName(qname.getLocalPart());
+ Name name = getName(node);
+ String localName = escapeName(name.getLocalPart());
String prefixedName =
- node.getSession().getNamespacePrefix(qname.getNamespaceURI())
+ node.getSession().getNamespacePrefix(name.getNamespaceURI())
+ ":" + localName;
// Start element
handler.startElement(
- qname.getNamespaceURI(), localName, prefixedName,
+ name.getNamespaceURI(), localName, prefixedName,
getAttributes(node));
// Visit child nodes (unless denied by the noRecurse flag)
@@ -331,7 +326,7 @@
// End element
handler.endElement(
- qname.getNamespaceURI(), qname.getLocalPart(), node.getName());
+ name.getNamespaceURI(), name.getLocalPart(), node.getName());
}
/**
@@ -349,12 +344,17 @@
PropertyIterator properties = node.getProperties();
while (properties.hasNext()) {
Property property = properties.nextProperty();
+ /*
+ return !property.getName().equals(XMLCHARACTERS)
+ && (!skipBinary || property.getType() != PropertyType.BINARY);
+ */
if (includeProperty(property)) {
- Name qname = getQName(property);
- String value = getValue(property);
- attributes.addAttribute(qname.getNamespaceURI(),
- qname.getLocalPart(), property.getName(),
- "CDATA", value);
+ Name name = getName(property);
+ attributes.addAttribute(
+ name.getNamespaceURI(),
+ escapeName(name.getLocalPart()),
+ escapeName(name.toJCRName(property.getSession())),
+ "CDATA", escapeValue(property));
}
}
@@ -378,12 +378,26 @@
* @throws RepositoryException on repository errors
* @see Name
*/
- private Name getQName(Item item) throws RepositoryException {
+ private Name getName(Item item) throws RepositoryException {
String name = item.getName();
- if (name.length() == 0) {
- name = "jcr:root";
+ if (name.length() > 0) {
+ return Name.fromJCRName(item.getSession(), name);
+ } else {
+ return new Name("http://www.jcp.org/jcr/1.0", "root");
}
- return Name.fromJCRName(item.getSession(), name);
+ }
+
+ /**
+ * Escapes the given character into a hex escape sequence
+ * <code>_xXXXX_</code> where <code>XXXX</code> is the (uppercase)
+ * hexadecimal representation of the given character.
+ *
+ * @param ch character to be escaped
+ * @return escape string
+ */
+ private static String escapeChar(char ch) {
+ String hex = "000" + Integer.toHexString((int) ch).toUpperCase();
+ return "_x" + hex.substring(hex.length() - 4) + "_";
}
/**
@@ -393,7 +407,7 @@
* @param name original name or prefix
* @return escaped name or prefix
*/
- private String escapeName(String name) {
+ private static String escapeName(String name) {
if (name.length() == 0) {
return name;
}
@@ -401,12 +415,11 @@
StringBuffer buffer = new StringBuffer();
// First character
- if (!XMLChar.isNameStart(name.charAt(0)) || name.startsWith("_x")
- || (name.length() >= 3
- && "xml".equalsIgnoreCase(name.substring(0, 3)))) {
- String hex =
- "000" + Integer.toHexString(name.charAt(0)).toUpperCase();
- buffer.append("_x" + hex.substring(hex.length() - 4) + "_");
+ if (!XMLChar.isNameStart(name.charAt(0))
+ || name.startsWith("_x")
+ || (name.length() >= 3
+ && "xml".equalsIgnoreCase(name.substring(0, 3)))) {
+ buffer.append(escapeChar(name.charAt(0)));
} else {
buffer.append(name.charAt(0));
}
@@ -414,9 +427,7 @@
// Rest of the characters
for (int i = 1; i < name.length(); i++) {
if (!XMLChar.isName(name.charAt(i)) || name.startsWith("_x", i)) {
- String hex =
- "000" + Integer.toHexString(name.charAt(0)).toUpperCase();
- buffer.append("_x" + hex.substring(hex.length() - 4) + "_");
+ buffer.append(escapeChar(name.charAt(i)));
} else {
buffer.append(name.charAt(i));
}
@@ -432,7 +443,7 @@
* @param value original value
* @return escaped value
*/
- private String escapeValue(String value) {
+ private static String escapeValue(String value) {
StringBuffer buffer = new StringBuffer();
for (int i = 1; i < value.length(); i++) {
@@ -459,7 +470,8 @@
* @return document view representation of the property value
* @throws RepositoryException on repository errors
*/
- private String getValue(Property property) throws RepositoryException {
+ private static String escapeValue(Property property)
+ throws RepositoryException {
try {
if (property.getDefinition().isMultiple()) {
StringBuffer buffer = new StringBuffer();
@@ -467,7 +479,7 @@
Value[] values = property.getValues();
for (int i = 0; i < values.length; i++) {
if (i > 0) {
- buffer.append(" ");
+ buffer.append(' ');
}
if (values[i].getType() == PropertyType.BINARY) {
buffer.append(encodeValue(values[i].getStream()));
@@ -494,7 +506,7 @@
* @return Base64-encoded value
* @throws IOException on IO errors
*/
- private String encodeValue(InputStream input) throws IOException {
+ private static String encodeValue(InputStream input) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] bytes = new byte[4096];
for (int n = input.read(bytes); n != -1; n = input.read(bytes)) {
Modified: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/SystemViewImportContentHandler.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/SystemViewImportContentHandler.java?rev=169294&r1=169293&r2=169294&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/SystemViewImportContentHandler.java (original)
+++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/SystemViewImportContentHandler.java Mon May 9 04:40:50 2005
@@ -19,12 +19,11 @@
import java.util.List;
import java.util.Stack;
-import javax.jcr.BooleanValue;
import javax.jcr.NamespaceException;
import javax.jcr.Node;
-import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
+import javax.jcr.ValueFactory;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
@@ -38,6 +37,8 @@
private Session session;
+ private ValueFactory factory;
+
private Stack stack;
private Node node;
@@ -50,6 +51,7 @@
public SystemViewImportContentHandler(Node parent) throws RepositoryException {
this.session = parent.getSession();
+ this.factory = session.getValueFactory();
this.stack = new Stack();
this.node = parent;
this.text = new StringBuffer();
@@ -107,42 +109,12 @@
*/
public void endElement(String uri, String localName, String qName)
throws SAXException {
- if (uri.equals("SV") && localName.equals("value")) {
- String value = text.toString();
- switch (type) {
- case PropertyType.BINARY:
- // TODO values.add(new BinaryValue());
- break;
- case PropertyType.BOOLEAN:
- values.add(new BooleanValue(Boolean.valueOf(value)));
- break;
- case PropertyType.DATE:
- values.add(new BooleanValue(Boolean.valueOf(value)));
- break;
- case PropertyType.DOUBLE:
- values.add(new BooleanValue(Boolean.valueOf(value)));
- break;
- case PropertyType.LONG:
- values.add(new BooleanValue(Boolean.valueOf(value)));
- break;
- case PropertyType.NAME:
- values.add(new BooleanValue(Boolean.valueOf(value)));
- break;
- case PropertyType.PATH:
- values.add(new BooleanValue(Boolean.valueOf(value)));
- break;
- case PropertyType.REFERENCE:
- values.add(new BooleanValue(Boolean.valueOf(value)));
- break;
- case PropertyType.STRING:
- values.add(new BooleanValue(Boolean.valueOf(value)));
- break;
- default:
- }
-
- text.setLength(0);
- }
try {
+ if (uri.equals("SV") && localName.equals("value")) {
+ String value = text.toString();
+ values.add(factory.createValue(value, type));
+ text.setLength(0);
+ }
importText();
node = (Node) stack.pop();