use of javax.jcr.ValueFormatException in project jackrabbit by apache.
the class StringValue method getDate.
/**
* If the string is in the format described in
* {@link DateValue#getString()}, it is converted directly, otherwise
* a {@link ValueFormatException} is thrown.
*/
@Override
public Calendar getDate() throws ValueFormatException {
// check optional leading sign
char sign = '+';
int start = 0;
if (value.startsWith("-")) {
sign = '-';
start = 1;
} else if (value.startsWith("+")) {
sign = '+';
start = 1;
}
// note that we cannot use java.text.SimpleDateFormat for
// parsing because it can't handle years <= 0 and TZD's
int year, month, day, hour, min, sec, ms;
String tzID;
try {
// year (YYYY)
year = Integer.parseInt(value.substring(start, start + 4));
start += 4;
// delimiter '-'
if (value.charAt(start) != '-') {
throw new ValueFormatException("Not a date: " + value);
}
start++;
// month (MM)
month = Integer.parseInt(value.substring(start, start + 2));
start += 2;
// delimiter '-'
if (value.charAt(start) != '-') {
throw new ValueFormatException("Not a date: " + value);
}
start++;
// day (DD)
day = Integer.parseInt(value.substring(start, start + 2));
start += 2;
// delimiter 'T'
if (value.charAt(start) != 'T') {
throw new ValueFormatException("Not a date: " + value);
}
start++;
// hour (hh)
hour = Integer.parseInt(value.substring(start, start + 2));
start += 2;
// delimiter ':'
if (value.charAt(start) != ':') {
throw new ValueFormatException("Not a date: " + value);
}
start++;
// minute (mm)
min = Integer.parseInt(value.substring(start, start + 2));
start += 2;
// delimiter ':'
if (value.charAt(start) != ':') {
throw new ValueFormatException("Not a date: " + value);
}
start++;
// second (ss)
sec = Integer.parseInt(value.substring(start, start + 2));
start += 2;
// delimiter '.'
if (value.charAt(start) != '.') {
throw new ValueFormatException("Not a date: " + value);
}
start++;
// millisecond (SSS)
ms = Integer.parseInt(value.substring(start, start + 3));
start += 3;
// time zone designator (Z or +00:00 or -00:00)
if (value.charAt(start) == '+' || value.charAt(start) == '-') {
// offset to UTC specified in the format +00:00/-00:00
tzID = "GMT" + value.substring(start);
} else if (value.substring(start).equals("Z")) {
tzID = "GMT";
} else {
throw new ValueFormatException("Invalid time zone in a date: " + value);
}
} catch (IndexOutOfBoundsException e) {
throw new ValueFormatException("Not a date: " + value, e);
} catch (NumberFormatException e) {
throw new ValueFormatException("Not a date: " + value, e);
}
TimeZone tz = TimeZone.getTimeZone(tzID);
// verify id of returned time zone (getTimeZone defaults to "GMT")
if (!tz.getID().equals(tzID)) {
throw new ValueFormatException("Invalid time zone in a date: " + value);
}
// initialize Calendar object
Calendar cal = Calendar.getInstance(tz);
cal.setLenient(false);
// year and era
if (sign == '-' || year == 0) {
// not CE, need to set era (BCE) and adjust year
cal.set(Calendar.YEAR, year + 1);
cal.set(Calendar.ERA, GregorianCalendar.BC);
} else {
cal.set(Calendar.YEAR, year);
cal.set(Calendar.ERA, GregorianCalendar.AD);
}
// month (0-based!)
cal.set(Calendar.MONTH, month - 1);
// day of month
cal.set(Calendar.DAY_OF_MONTH, day);
// hour
cal.set(Calendar.HOUR_OF_DAY, hour);
// minute
cal.set(Calendar.MINUTE, min);
// second
cal.set(Calendar.SECOND, sec);
// millisecond
cal.set(Calendar.MILLISECOND, ms);
try {
// the following call will trigger an IllegalArgumentException
// if any of the set values are illegal or out of range
cal.getTime();
} catch (IllegalArgumentException e) {
throw new ValueFormatException("Not a date: " + value, e);
}
return cal;
}
use of javax.jcr.ValueFormatException in project jackrabbit by apache.
the class PathPropertyTest method testGetNode.
/**
* Since JCR 2.0 a path property can be dereferenced if it points to a
* Node.
* TODO: create several tests out of this one
*/
public void testGetNode() throws RepositoryException {
if (!multiple) {
String nodePath = prop.getParent().getPath();
String propName = prop.getName();
// absolute nodes path
prop.getParent().setProperty(propName, nodePath, PropertyType.PATH);
String value = prop.getString();
Node n = prop.getNode();
assertEquals("The path of the dereferenced property must be equal to the value", n.getPath(), value);
assertTrue("The property value must be resolved to the correct node.", prop.getParent().isSame(n));
// relative node path
prop.getParent().setProperty(propName, ".", PropertyType.PATH);
n = prop.getNode();
assertTrue("The property value must be resolved to the correct node.", prop.getParent().getNode(".").isSame(n));
// non-existing property path
while (session.nodeExists(nodePath)) {
nodePath += "x";
}
prop.getParent().setProperty(propName, nodePath, PropertyType.PATH);
try {
prop.getNode();
fail("Calling Property.getNode() for a PATH value that doesn't have a corresponding Node, ItemNotFoundException is expected");
} catch (ItemNotFoundException e) {
// ok
}
} else {
try {
prop.getNode();
fail("Property.getNode() called on a multivalue property " + "should throw a ValueFormatException.");
} catch (ValueFormatException vfe) {
// ok
}
}
}
use of javax.jcr.ValueFormatException in project jackrabbit by apache.
the class PathPropertyTest method testGetDouble.
/**
* Tests failure from Path type to Double type.
*/
public void testGetDouble() throws RepositoryException {
try {
Value val = PropertyUtil.getValue(prop);
val.getDouble();
fail("Conversion from a Path value to a Double value " + "should throw a ValueFormatException.");
} catch (ValueFormatException vfe) {
// ok
}
}
use of javax.jcr.ValueFormatException in project jackrabbit by apache.
the class PathPropertyTest method testGetProperty.
/**
* Since JCR 2.0 a path property can be dereferenced if it points to a
* Property.
* TODO: create several tests out of this one
*/
public void testGetProperty() throws RepositoryException {
if (!multiple) {
String propPath = prop.getPath();
String propName = prop.getName();
// absolute property path
prop.getParent().setProperty(propName, propPath, PropertyType.PATH);
String path = prop.getString();
Property p = prop.getProperty();
assertEquals("The path of the dereferenced property must be equal to the value", path, p.getPath());
assertTrue("The property value must be resolved to the correct property.", prop.isSame(p));
// relative property path
prop.getParent().setProperty(propName, propName, PropertyType.PATH);
path = prop.getString();
p = prop.getProperty();
assertEquals("The path of the dereferenced property must be equal to the value", path, p.getName());
assertTrue("The property value must be resolved to the correct property.", prop.getParent().getProperty(path).isSame(p));
// non-existing property path
while (session.propertyExists(propPath)) {
propPath += "x";
}
prop.getParent().setProperty(propName, propPath, PropertyType.PATH);
try {
prop.getProperty();
fail("Calling Property.getProperty() for a PATH value that doesn't have a corresponding Property, ItemNotFoundException is expected");
} catch (ItemNotFoundException e) {
// ok
}
} else {
try {
prop.getProperty();
fail("Property.getNode() called on a multivalue property " + "should throw a ValueFormatException.");
} catch (ValueFormatException vfe) {
// ok
}
}
}
use of javax.jcr.ValueFormatException in project jackrabbit by apache.
the class PathPropertyTest method testGetBoolean.
/**
* Tests failure of conversion from Path type to Boolean type.
*/
public void testGetBoolean() throws RepositoryException {
try {
Value val = PropertyUtil.getValue(prop);
val.getBoolean();
fail("Conversion from a Path value to a Boolean value " + "should throw a ValueFormatException.");
} catch (ValueFormatException vfe) {
// ok
}
}
Aggregations