/** * Returns the element value adapted to the key's datatype. * * @param <V> data type of the key. * @param key the element key used to convert the value. * @return typed element value. */ public <V> V getTextValue(ElementKey<V, ?> key) { if (state.value != null) { try { return ObjectConverter.getValue(state.value, key.getDatatype()); } catch (ParseException e) { throw new IllegalArgumentException("Unable to convert value " + e + " to datatype " + key.getDatatype()); } } return null; }
/** * Checks that the datatype of this element allows setting the value to the * given object. Throws an {@link IllegalArgumentException} if the value * is not valid for this element. */ Object checkValue(ElementKey<?, ?> elementKey, Object newValue) { if (newValue != null) { Class<?> datatype = elementKey.getDatatype(); Preconditions.checkArgument(datatype != Void.class, "Element must not contain a text node"); Preconditions.checkArgument(datatype.isInstance(newValue), "Invalid class: %s", newValue.getClass().getCanonicalName()); } return newValue; }
/** * Checks that the datatype of this element allows setting the value to the * given object. Throws an {@link IllegalArgumentException} if the value * is not valid for this element. */ Object checkValue(ElementKey<?, ?> elementKey, Object newValue) { if (newValue != null) { Class<?> datatype = elementKey.getDatatype(); Preconditions.checkArgument(datatype != Void.class, "Element must not contain a text node"); Preconditions.checkArgument(datatype.isInstance(newValue), "Invalid class: %s", newValue.getClass().getCanonicalName()); } return newValue; }
/** * Checks that the datatype of this element allows setting the value to the * given object. Throws an {@link IllegalArgumentException} if the value * is not valid for this element. */ Object checkValue(ElementKey<?, ?> elementKey, Object newValue) { if (newValue != null) { Class<?> datatype = elementKey.getDatatype(); Preconditions.checkArgument(datatype != Void.class, "Element must not contain a text node"); Preconditions.checkArgument(datatype.isInstance(newValue), "Invalid class: %s", newValue.getClass().getCanonicalName()); } return newValue; }
@Override public void processEndElement() throws ParseException { if (value != null) { value = value.trim(); if (!value.equals("")) { ElementKey<?, ?> elementKey = element.getElementKey(); element.setTextValue( ObjectConverter.getValue(value, elementKey.getDatatype())); } } if (parentElement != null) { parentElement.addElement(element); } } }
@Override public void processEndElement() throws ParseException { if (value != null) { value = value.trim(); if (!value.equals("")) { ElementKey<?, ?> elementKey = element.getElementKey(); element.setTextValue( ObjectConverter.getValue(value, elementKey.getDatatype())); } } if (parentElement != null) { parentElement.addElement(element); } } }
@Override public void processEndElement() throws ParseException { if (value != null) { value = value.trim(); if (!value.equals("")) { ElementKey<?, ?> elementKey = element.getElementKey(); element.setTextValue( ObjectConverter.getValue(value, elementKey.getDatatype())); } } if (parentElement != null) { parentElement.addElement(element); } } }
@Override public void parseValue(Element element, ElementMetadata<?, ?> metadata, Object value) throws ParseException { if (!super.parse(element, metadata, value)) { element.setTextValue( ObjectConverter.getValue(value, elemKey.getDatatype())); } }
@Override public void parseValue(Element element, ElementMetadata<?, ?> metadata, Object value) throws ParseException { if (!super.parse(element, metadata, value)) { element.setTextValue( ObjectConverter.getValue(value, elemKey.getDatatype())); } }
@Override public void parseValue(Element element, ElementMetadata<?, ?> metadata, Object value) throws ParseException { if (!super.parse(element, metadata, value)) { element.setTextValue( ObjectConverter.getValue(value, elemKey.getDatatype())); } }
/** * Binds an element key to a specific element subclass. This guarantees that * the key on an element will always have exactly that element's type as its * element type, and not some other element type. This makes it possible to * believe an element's key without needing to check the element type when * looking up metadata. */ private static ElementKey<?, ?> bindKey(ElementKey<?, ?> key, Class<? extends Element> type) { Class<?> keyType = key.getElementType(); if (keyType == type) { return key; } return ElementKey.of(key.getId(), key.getDatatype(), type); }
/** * Calculates the actual key that should be used for adding an element. This * uses the ID and datatype of the key, but if the element types are in the * same type hierarchy the narrowest element type is used. */ private ElementKey<?, ?> calculateKey(ElementKey<?, ?> key, ElementKey<?, ?> sourceKey) { Class<?> keyType = key.getElementType(); Class<? extends Element> sourceType = sourceKey.getElementType(); // If the sourceType is a subtype of the key type, we want to use it // as the type of element that we create, because it is more specific but // still compatible. if (keyType != sourceType && keyType.isAssignableFrom(sourceType)) { key = ElementKey.of(key.getId(), key.getDatatype(), sourceType); } return key; }
/** * Calculates the actual key that should be used for adding an element. This * uses the ID and datatype of the key, but if the element types are in the * same type hierarchy the narrowest element type is used. */ private ElementKey<?, ?> calculateKey(ElementKey<?, ?> key, ElementKey<?, ?> sourceKey) { Class<?> keyType = key.getElementType(); Class<? extends Element> sourceType = sourceKey.getElementType(); // If the sourceType is a subtype of the key type, we want to use it // as the type of element that we create, because it is more specific but // still compatible. if (keyType != sourceType && keyType.isAssignableFrom(sourceType)) { key = ElementKey.of(key.getId(), key.getDatatype(), sourceType); } return key; }
/** * Add a child element with the given ID. This will add the given element to * the end of the collection of elements with the same ID. If you want to * replace any existing elements use {@link #setElement(QName, Element)} * instead. * * @param id the qualified name to use for the child * @param element child element * @return this element for chaining * @throws NullPointerException if element is null. */ public Element addElement(QName id, Element element) { Preconditions.checkNotNull(element); addElement(ElementKey.of(id, element.getElementKey().getDatatype(), element.getClass()), element); return this; }
/** * Binds an element key to a specific element subclass. This guarantees that * the key on an element will always have exactly that element's type as its * element type, and not some other element type. This makes it possible to * believe an element's key without needing to check the element type when * looking up metadata. */ private static ElementKey<?, ?> bindKey(ElementKey<?, ?> key, Class<? extends Element> type) { Class<?> keyType = key.getElementType(); if (keyType == type) { return key; } return ElementKey.of(key.getId(), key.getDatatype(), type); }
/** * Binds an element key to a specific element subclass. This guarantees that * the key on an element will always have exactly that element's type as its * element type, and not some other element type. This makes it possible to * believe an element's key without needing to check the element type when * looking up metadata. */ private static ElementKey<?, ?> bindKey(ElementKey<?, ?> key, Class<? extends Element> type) { Class<?> keyType = key.getElementType(); if (keyType == type) { return key; } return ElementKey.of(key.getId(), key.getDatatype(), type); }
/** * Add a child element with the given ID. This will add the given element to * the end of the collection of elements with the same ID. If you want to * replace any existing elements use {@link #setElement(QName, Element)} * instead. * * @param id the qualified name to use for the child * @param element child element * @return this element for chaining * @throws NullPointerException if element is null. */ public Element addElement(QName id, Element element) { Preconditions.checkNotNull(element); addElement(ElementKey.of(id, element.getElementKey().getDatatype(), element.getClass()), element); return this; }
/** * Merges the key from the metadata with the source key to get the "real" * key for a moved element. */ private static ElementKey<?, ?> mergeKeys(ElementKey<?, ?> sourceKey, ElementKey<?, ?> metadataKey) { if (!metadataKey.getId().equals(sourceKey.getId())) { return ElementKey.of(metadataKey.getId(), sourceKey.getDatatype(), sourceKey.getElementType()); } return sourceKey; } }
/** * Merges the key from the metadata with the source key to get the "real" * key for a moved element. */ private static ElementKey<?, ?> mergeKeys(ElementKey<?, ?> sourceKey, ElementKey<?, ?> metadataKey) { if (!metadataKey.getId().equals(sourceKey.getId())) { return ElementKey.of(metadataKey.getId(), sourceKey.getDatatype(), sourceKey.getElementType()); } return sourceKey; } }
/** * Merges the key from the metadata with the source key to get the "real" * key for a moved element. */ private static ElementKey<?, ?> mergeKeys(ElementKey<?, ?> sourceKey, ElementKey<?, ?> metadataKey) { if (!metadataKey.getId().equals(sourceKey.getId())) { return ElementKey.of(metadataKey.getId(), sourceKey.getDatatype(), sourceKey.getElementType()); } return sourceKey; } }