Author: fguillaume
Date: Fri Jun 12 14:51:37 2009
New Revision: 784143
URL: http://svn.apache.org/viewvc?rev=784143&view=rev
Log:
Cleanup of XmlProperty
Modified:
incubator/chemistry/trunk/chemistry/chemistry-api/src/main/java/org/apache/chemistry/PropertyDefinition.java
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/AbstractObjectReader.java
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/PropertyIterator.java
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/ValueIterator.java
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/XmlProperty.java
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/test/java/org/apache/chemistry/atompub/client/stax/TestPropertyIterator.java
incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/xml/stax/ElementIterator.java
incubator/chemistry/trunk/chemistry/chemistry-jcr/src/main/java/org/apache/chemistry/jcr/JcrPropertyDefinition.java
Modified:
incubator/chemistry/trunk/chemistry/chemistry-api/src/main/java/org/apache/chemistry/PropertyDefinition.java
URL:
http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-api/src/main/java/org/apache/chemistry/PropertyDefinition.java?rev=784143&r1=784142&r2=784143&view=diff
==============================================================================
---
incubator/chemistry/trunk/chemistry/chemistry-api/src/main/java/org/apache/chemistry/PropertyDefinition.java
(original)
+++
incubator/chemistry/trunk/chemistry/chemistry-api/src/main/java/org/apache/chemistry/PropertyDefinition.java
Fri Jun 12 14:51:37 2009
@@ -258,4 +258,13 @@
*/
boolean validates(Serializable value);
+ /**
+ * Checks if a value can be set in this property, and returns an error
+ * message if not.
+ *
+ * @param value the candidate value
+ * @return null if the value can be set, an error message if not
+ */
+ String validationError(Serializable value);
+
}
Modified:
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/AbstractObjectReader.java
URL:
http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/AbstractObjectReader.java?rev=784143&r1=784142&r2=784143&view=diff
==============================================================================
---
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/AbstractObjectReader.java
(original)
+++
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/AbstractObjectReader.java
Fri Jun 12 14:51:37 2009
@@ -17,10 +17,12 @@
package org.apache.chemistry.atompub.client.stax;
import java.util.ArrayList;
+import java.util.List;
import javax.xml.stream.XMLStreamException;
import org.apache.chemistry.Property;
+import org.apache.chemistry.PropertyDefinition;
import org.apache.chemistry.Type;
import org.apache.chemistry.atompub.CMIS;
import org.apache.chemistry.xml.stax.ChildrenNavigator;
@@ -67,55 +69,61 @@
}
}
+ /*
+ * Reads the properties. Because the ObjectTypeId may not be known
+ * initially, the properties' types cannot be computed on the fly. So the
+ * properties are held until the type is found.
+ */
protected void readProperties(ReadContext ctx, StaxReader reader, T object)
throws XMLStreamException {
PropertyIterator it = new PropertyIterator(reader);
- ArrayList<XmlProperty> prefetch = null;
+ List<XmlProperty> incomplete = null;
Type entryType = ctx.getType();
+ // find the type
if (entryType == null) {
- prefetch = new ArrayList<XmlProperty>();
+ incomplete = new ArrayList<XmlProperty>();
while (it.hasNext()) {
XmlProperty p = it.next();
- if (Property.TYPE_ID.equals(p.value)) {
+ if (Property.TYPE_ID.equals(p.getName())) {
+ // type has been found
String v = (String) p.getXmlValue();
entryType = ctx.getRepository().getType(v);
if (entryType == null) {
throw new ParseException("No such type: " + v);
}
- prefetch.add(p);
+ incomplete.add(p);
+ // stop looking for type
break;
} else {
- prefetch.add(p);
+ incomplete.add(p);
}
}
if (entryType == null) {
throw new IllegalStateException("Type not known");
}
}
- if (prefetch != null) {
- for (XmlProperty p : prefetch) {
- p.def = entryType.getPropertyDefinition((String) p.value);
- if (p.def == null) {
- throw new ParseException("No such property definition: "
- + p.value + " in type " + entryType);
- }
- p.value = XmlProperty.NULL;
- // System.out.println("adding prefetched >>>>>
"+reader.getName()+" -> "+p.getXmlValue());
- readProperty(ctx, reader, object, p);
+ // fill in the type for incomplete properties
+ if (incomplete != null) {
+ for (XmlProperty p : incomplete) {
+ readPropertyWithType(ctx, reader, object, p, entryType);
}
}
// consume the rest of the stream
while (it.hasNext()) {
XmlProperty p = it.next();
- p.def = entryType.getPropertyDefinition((String) p.value);
- if (p.def == null) {
- throw new ParseException("No such property definition: "
- + p.value + " in type " + entryType);
- }
- p.value = XmlProperty.NULL;
- // System.out.println("adding non prefetched >>>>>
"+reader.getName()+" -> "+p.getXmlValue());
- readProperty(ctx, reader, object, p);
+ readPropertyWithType(ctx, reader, object, p, entryType);
+ }
+ }
+
+ protected void readPropertyWithType(ReadContext ctx, StaxReader reader,
+ T object, XmlProperty p, Type entryType) {
+ PropertyDefinition def = entryType.getPropertyDefinition(p.getName());
+ if (def == null) {
+ throw new ParseException("No such property definition: "
+ + p.getName() + " in type: " + entryType);
}
+ p.setDefinition(def);
+ readProperty(ctx, reader, object, p);
}
protected void readAllowableActions(ReadContext ctx, StaxReader reader,
Modified:
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/PropertyIterator.java
URL:
http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/PropertyIterator.java?rev=784143&r1=784142&r2=784143&view=diff
==============================================================================
---
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/PropertyIterator.java
(original)
+++
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/PropertyIterator.java
Fri Jun 12 14:51:37 2009
@@ -17,6 +17,7 @@
package org.apache.chemistry.atompub.client.stax;
import java.util.ArrayList;
+import java.util.List;
import javax.xml.stream.XMLStreamException;
@@ -50,7 +51,7 @@
}
@Override
- protected XmlProperty createValue() throws XMLStreamException {
+ protected XmlProperty getValue() throws XMLStreamException {
String key = reader.getAttributeValue(CMIS.NAME);
if (key == null) {
throw new XMLStreamException(
@@ -59,24 +60,19 @@
+ ". No name specified");
}
ValueIterator vi = new ValueIterator(reader);
- XmlProperty xp = new XmlProperty();
- xp.value = key; // use value to temporary store the key
if (!vi.hasNext()) {
- return xp;
+ return new XmlProperty(key);
}
String val = vi.next();
if (!vi.hasNext()) {
- xp.xmlValue = val;
- return xp;
+ return new XmlProperty(key, val);
}
- ArrayList<String> vals = new ArrayList<String>();
+ List<String> vals = new ArrayList<String>();
vals.add(val);
do {
- val = vi.next();
- vals.add(val);
+ vals.add(vi.next());
} while (vi.hasNext());
- xp.xmlValue = vals;
- return xp;
+ return new XmlProperty(key, vals);
}
}
Modified:
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/ValueIterator.java
URL:
http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/ValueIterator.java?rev=784143&r1=784142&r2=784143&view=diff
==============================================================================
---
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/ValueIterator.java
(original)
+++
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/ValueIterator.java
Fri Jun 12 14:51:37 2009
@@ -23,7 +23,7 @@
import org.apache.chemistry.xml.stax.StaxReader;
/**
- *
+ * Iterator whose {...@link #next} method returns the element's text.
*/
public class ValueIterator extends ChildrenIterator<String> {
@@ -38,7 +38,7 @@
}
@Override
- protected String createValue() throws XMLStreamException {
+ protected String getValue() throws XMLStreamException {
return reader.getElementText();
}
Modified:
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/XmlProperty.java
URL:
http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/XmlProperty.java?rev=784143&r1=784142&r2=784143&view=diff
==============================================================================
---
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/XmlProperty.java
(original)
+++
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/XmlProperty.java
Fri Jun 12 14:51:37 2009
@@ -33,40 +33,58 @@
public class XmlProperty implements Property {
private static enum NoValue {
- @SuppressWarnings("hiding")
- NULL
+ NO_VALUE
};
- public static final Serializable NULL = NoValue.NULL;
+ public static final Serializable NULL = NoValue.NO_VALUE;
- protected PropertyDefinition def;
+ private PropertyDefinition def;
- protected Serializable value = NULL;
+ /**
+ * The internal state can be:
+ * <ol>
+ * <li>if def is null, then value hold the property name,
+ * <li>if def is not null, then value is either {...@link NULL} if not yet
+ * computed from xmlValue, or it holds the actual Java value.
+ * </ol>
+ */
+ private Serializable value;
+
+ /**
+ * The XML value, either a {...@code String} or a {...@code List<String>}.
+ */
+ private Object xmlValue;
- protected Object xmlValue;
+ public XmlProperty(String name) {
+ value = name;
+ }
- public XmlProperty() {
+ public XmlProperty(String name, String xmlValue) {
+ value = name;
+ this.xmlValue = xmlValue;
}
- public XmlProperty(PropertyDefinition def) {
- this.def = def;
+ public XmlProperty(String name, List<String> xmlValue) {
+ value = name;
+ this.xmlValue = xmlValue;
}
- public XmlProperty(PropertyDefinition def, String value) {
+ public XmlProperty(PropertyDefinition def) {
this.def = def;
- this.xmlValue = value;
+ value = NULL;
}
- public XmlProperty(PropertyDefinition def, List<String> value) {
+ public XmlProperty(PropertyDefinition def, String xmlValue) {
this.def = def;
- this.xmlValue = value;
+ this.xmlValue = xmlValue;
+ value = NULL;
}
/**
* Gets the property name.
*/
public String getName() {
- return def.getName();
+ return def == null ? (String) value : def.getName();
}
/**
@@ -140,33 +158,22 @@
}
public void setValue(Serializable value) {
- if (!def.validates(value)) {
- throw new IllegalArgumentException("Not a valid value: " + value);
+ String error = def.validationError(value);
+ if (error != null) {
+ throw new IllegalArgumentException(this + " cannot receive value: "
+ + value + ": " + error);
// TODO use custom exceptions
}
this.value = value;
}
- public void setValueUnsafe(Serializable value) {
- this.value = value;
- }
-
public Object getXmlValue() {
return xmlValue;
}
- public void setXmlValue(String value) {
- this.xmlValue = value;
- this.value = NULL;
- }
-
- public void setXmlValue(List<String> value) {
- this.xmlValue = value;
- this.value = NULL;
- }
-
public void setDefinition(PropertyDefinition def) {
this.def = def;
+ value = NULL;
}
public PropertyDefinition getDefinition() {
Modified:
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/test/java/org/apache/chemistry/atompub/client/stax/TestPropertyIterator.java
URL:
http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/test/java/org/apache/chemistry/atompub/client/stax/TestPropertyIterator.java?rev=784143&r1=784142&r2=784143&view=diff
==============================================================================
---
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/test/java/org/apache/chemistry/atompub/client/stax/TestPropertyIterator.java
(original)
+++
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/test/java/org/apache/chemistry/atompub/client/stax/TestPropertyIterator.java
Fri Jun 12 14:51:37 2009
@@ -17,7 +17,6 @@
*/
package org.apache.chemistry.atompub.client.stax;
-import java.io.Serializable;
import java.net.URL;
import java.util.Arrays;
import java.util.LinkedList;
@@ -26,8 +25,6 @@
import junit.framework.TestCase;
import org.apache.chemistry.atompub.CMIS;
-import org.apache.chemistry.atompub.client.stax.PropertyIterator;
-import org.apache.chemistry.atompub.client.stax.XmlProperty;
import org.apache.chemistry.xml.stax.StaxReader;
/**
@@ -42,9 +39,7 @@
PropertyIterator pi = new PropertyIterator(sr);
List<String> names = new LinkedList<String>();
while (pi.hasNext()) {
- XmlProperty p = pi.next();
- Serializable value = p.value;
- names.add(value.toString());
+ names.add(pi.next().getName());
}
assertEquals(Arrays.asList("string_null", "string", "date",
"string_array", "date_array"), names);
Modified:
incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/xml/stax/ElementIterator.java
URL:
http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/xml/stax/ElementIterator.java?rev=784143&r1=784142&r2=784143&view=diff
==============================================================================
---
incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/xml/stax/ElementIterator.java
(original)
+++
incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/xml/stax/ElementIterator.java
Fri Jun 12 14:51:37 2009
@@ -22,20 +22,22 @@
import javax.xml.stream.XMLStreamException;
/**
- * An iterator over the XML elements in the stream that create element objects
- * each time next method is called.
+ * An iterator over the XML elements in the stream that creates objects each
+ * time {...@link #next} method is called.
*/
public abstract class ElementIterator<T> implements Iterator<T> {
protected StaxReader reader;
+ /** If null, then the state is not known. */
protected Boolean hasNext;
public ElementIterator(StaxReader sr) {
this.reader = sr;
}
- protected abstract T createValue() throws XMLStreamException;
+ /** Gets the value from the element in the reader. */
+ protected abstract T getValue() throws XMLStreamException;
protected boolean forward() throws XMLStreamException {
return reader.fwd();
@@ -60,21 +62,17 @@
hasNext = Boolean.FALSE;
return false;
}
-
return hasNext.booleanValue();
}
public T next() {
- if (hasNext == null) {
- hasNext();
- }
- if (!hasNext) {
+ if (!hasNext()) {
throw new NoSuchElementException("No more elements in stream");
}
- hasNext = null;
+ hasNext = null; // value will be consumed by getValue()
try {
- return createValue();
- } catch (Exception e) {
+ return getValue();
+ } catch (XMLStreamException e) {
throw new ParseException(e);
}
}
Modified:
incubator/chemistry/trunk/chemistry/chemistry-jcr/src/main/java/org/apache/chemistry/jcr/JcrPropertyDefinition.java
URL:
http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-jcr/src/main/java/org/apache/chemistry/jcr/JcrPropertyDefinition.java?rev=784143&r1=784142&r2=784143&view=diff
==============================================================================
---
incubator/chemistry/trunk/chemistry/chemistry-jcr/src/main/java/org/apache/chemistry/jcr/JcrPropertyDefinition.java
(original)
+++
incubator/chemistry/trunk/chemistry/chemistry-jcr/src/main/java/org/apache/chemistry/jcr/JcrPropertyDefinition.java
Fri Jun 12 14:51:37 2009
@@ -146,7 +146,12 @@
}
public boolean validates(Serializable value) {
+ return validationError(value) == null;
+ }
+
+ public String validationError(Serializable value) {
// TODO Auto-generated method stub
- return false;
+ return "error TODO";
}
+
}