Method from org.apache.jempbox.xmp.XMPSchema Detail: |
public void addBagValue(String bagName,
String bagValue) {
Element bagElement = null;
NodeList nodes = schema.getElementsByTagName(bagName);
if (nodes.getLength() > 0)
{
Element contElement = (Element) nodes.item(0);
NodeList bagList = contElement.getElementsByTagName("rdf:Bag");
if (bagList.getLength() > 0)
{
bagElement = (Element) bagList.item(0);
}
}
else
{
Element contElement = schema.getOwnerDocument().createElement(
bagName);
schema.appendChild(contElement);
bagElement = schema.getOwnerDocument().createElement("rdf:Bag");
contElement.appendChild(bagElement);
}
Element liElement = schema.getOwnerDocument().createElement("rdf:li");
XMLUtil.setStringValue(liElement, bagValue);
bagElement.appendChild(liElement);
}
Add an entry to a bag property. |
public void addSequenceDateValue(String seqName,
Calendar date) {
String dateAsString = DateConverter.toISO8601(date);
addSequenceValue(seqName, dateAsString);
}
Add a date sequence value to the list. |
public void addSequenceValue(String seqName,
String seqValue) {
Element bagElement = null;
NodeList nodes = schema.getElementsByTagName(seqName);
if (nodes.getLength() > 0)
{
Element contElement = (Element) nodes.item(0);
NodeList bagList = contElement.getElementsByTagName("rdf:Seq");
if (bagList.getLength() > 0)
{
bagElement = (Element) bagList.item(0);
}
else
{
// xml is crap discard it
schema.removeChild(nodes.item(0));
}
}
if (bagElement == null)
{
Element contElement = schema.getOwnerDocument().createElement(
seqName);
schema.appendChild(contElement);
bagElement = schema.getOwnerDocument().createElement("rdf:Seq");
contElement.appendChild(bagElement);
}
Element liElement = schema.getOwnerDocument().createElement("rdf:li");
liElement.appendChild(schema.getOwnerDocument()
.createTextNode(seqValue));
bagElement.appendChild(liElement);
}
Add a new value to a sequence property. |
public void addSequenceValue(String seqName,
Elementable seqValue) {
Element bagElement = null;
NodeList nodes = schema.getElementsByTagName(seqName);
if (nodes.getLength() > 0)
{
Element contElement = (Element) nodes.item(0);
NodeList bagList = contElement.getElementsByTagName("rdf:Seq");
if (bagList.getLength() > 0)
{
bagElement = (Element) bagList.item(0);
}
}
else
{
Element contElement = schema.getOwnerDocument().createElement(
seqName);
schema.appendChild(contElement);
bagElement = schema.getOwnerDocument().createElement("rdf:Seq");
contElement.appendChild(bagElement);
}
bagElement.appendChild(seqValue.getElement());
}
Add a new value to a sequence property. |
public String getAbout() {
return getTextProperty("rdf:about");
}
Get the RDF about attribute. |
public List<String> getBagList(String bagName) {
List< String > retval = null;
NodeList nodes = schema.getElementsByTagName(bagName);
if (nodes.getLength() > 0)
{
Element contributor = (Element) nodes.item(0);
NodeList bagList = contributor.getElementsByTagName("rdf:Bag");
if (bagList.getLength() > 0)
{
Element bag = (Element) bagList.item(0);
retval = new ArrayList< String >();
NodeList items = bag.getElementsByTagName("rdf:li");
for (int i = 0; i < items.getLength(); i++)
{
Element li = (Element) items.item(i);
retval.add(XMLUtil.getStringValue(li));
}
retval = Collections.unmodifiableList(retval);
}
}
return retval;
}
Get all the values of the bag property. This will return a list of
java.lang.String objects, this is a read-only list. |
public Boolean getBooleanProperty(String propertyName) {
Boolean value = null;
String stringValue = getTextProperty(propertyName);
if (stringValue != null)
{
value = stringValue.equals("True") ? Boolean.TRUE : Boolean.FALSE;
}
return value;
}
Get the value of the property as a boolean. |
public Calendar getDateProperty(String propertyName) throws IOException {
return DateConverter.toCalendar(getTextProperty(propertyName));
}
Get the value of the property as a date. |
public Element getElement() {
return schema;
}
Get the XML element that is represented by this schema. |
public List<ResourceEvent> getEventSequenceList(String seqName) {
List< ResourceEvent > retval = null;
NodeList nodes = schema.getElementsByTagName(seqName);
if (nodes.getLength() > 0)
{
Element contributor = (Element) nodes.item(0);
NodeList bagList = contributor.getElementsByTagName("rdf:Seq");
if (bagList.getLength() > 0)
{
Element bag = (Element) bagList.item(0);
retval = new ArrayList< ResourceEvent >();
NodeList items = bag.getElementsByTagName("rdf:li");
for (int i = 0; i < items.getLength(); i++)
{
Element li = (Element) items.item(i);
retval.add(new ResourceEvent(li));
}
retval = Collections.unmodifiableList(retval);
}
}
return retval;
}
Get a list of ResourceEvent objects. |
public Integer getIntegerProperty(String propertyName) {
Integer retval = null;
String intProperty = getTextProperty(propertyName);
if (intProperty != null && intProperty.length() > 0)
{
retval = new Integer(intProperty);
}
return retval;
}
Get the value of the property as an integer. |
public String getLanguageProperty(String propertyName,
String language) {
String retval = null;
if (language == null)
{
language = "x-default";
}
NodeList nodes = schema.getElementsByTagName(propertyName);
if (nodes.getLength() > 0)
{
Element property = (Element) nodes.item(0);
NodeList altList = property.getElementsByTagName("rdf:Alt");
if (altList.getLength() > 0)
{
Element alt = (Element) altList.item(0);
NodeList items = alt.getElementsByTagName("rdf:li");
for (int i = 0; i < items.getLength() && retval == null; i++)
{
Element li = (Element) items.item(i);
String elementLanguage = li.getAttribute("xml:lang");
if (language.equals(elementLanguage))
{
retval = XMLUtil.getStringValue(li);
}
}
}
}
return retval;
}
Get the value of a multi-lingual property. |
public List<String> getLanguagePropertyLanguages(String propertyName) {
List< String > retval = new ArrayList< String >();
NodeList nodes = schema.getElementsByTagName(propertyName);
if (nodes.getLength() > 0)
{
Element property = (Element) nodes.item(0);
NodeList altList = property.getElementsByTagName("rdf:Alt");
if (altList.getLength() > 0)
{
Element alt = (Element) altList.item(0);
NodeList items = alt.getElementsByTagName("rdf:li");
for (int i = 0; i < items.getLength(); i++)
{
Element li = (Element) items.item(i);
String elementLanguage = li.getAttribute("xml:lang");
if (elementLanguage == null)
{
retval.add("x-default");
}
else
{
retval.add(elementLanguage);
}
}
}
}
return retval;
}
Get a list of all languages that are currently defined for a specific
property. |
public List<Calendar> getSequenceDateList(String seqName) throws IOException {
List< String > strings = getSequenceList(seqName);
List< Calendar > retval = null;
if (strings != null)
{
retval = new ArrayList< Calendar >();
for (int i = 0; i < strings.size(); i++)
{
retval.add(DateConverter.toCalendar(strings.get(i)));
}
}
return retval;
}
Get all the date values in a sequence property. |
public List<String> getSequenceList(String seqName) {
List< String > retval = null;
NodeList nodes = schema.getElementsByTagName(seqName);
if (nodes.getLength() > 0)
{
Element contributor = (Element) nodes.item(0);
NodeList bagList = contributor.getElementsByTagName("rdf:Seq");
if (bagList.getLength() > 0)
{
Element bag = (Element) bagList.item(0);
retval = new ArrayList< String >();
NodeList items = bag.getElementsByTagName("rdf:li");
for (int i = 0; i < items.getLength(); i++)
{
Element li = (Element) items.item(i);
retval.add(XMLUtil.getStringValue(li));
}
retval = Collections.unmodifiableList(retval);
}
}
return retval;
}
Get all the values in a sequence property. |
public String getTextProperty(String propertyName) {
// propertyValue == null does not work, since getAttribute returns the
// empty string if the attribute is not found
if (schema.hasAttribute(propertyName))
{
return schema.getAttribute(propertyName);
}
else
{
NodeList nodes = schema.getElementsByTagName(propertyName);
if (nodes.getLength() > 0)
{
Element node = (Element) nodes.item(0);
return XMLUtil.getStringValue(node);
}
return null;
}
}
Get the value of a simple text property. |
public Thumbnail getThumbnailProperty(String propertyName,
String language) {
Thumbnail retval = null;
if (language == null)
{
language = "x-default";
}
NodeList nodes = schema.getElementsByTagName(propertyName);
if (nodes.getLength() > 0)
{
Element property = (Element) nodes.item(0);
NodeList altList = property.getElementsByTagName("rdf:Alt");
if (altList.getLength() > 0)
{
Element alt = (Element) altList.item(0);
NodeList items = alt.getElementsByTagName("rdf:li");
for (int i = 0; i < items.getLength() && retval == null; i++)
{
Element li = (Element) items.item(i);
String elementLanguage = li.getAttribute("xml:lang");
if (language.equals(elementLanguage))
{
retval = new Thumbnail(li);
}
}
}
}
return retval;
}
Get the value of a multi-lingual property. |
public void merge(XMPSchema xmpSchema) throws IOException {
if (!xmpSchema.getClass().equals(this.getClass()))
{
throw new IOException("Can only merge schemas of the same type.");
}
NamedNodeMap attributes = xmpSchema.getElement().getAttributes();
for (int i = 0; i < attributes.getLength(); i++)
{
Node a = attributes.item(i);
String name = a.getNodeName();
if (name.startsWith(prefix))
{
String newValue = xmpSchema.getTextProperty(name);
setTextProperty(name, newValue);
}
}
NodeList nodes = xmpSchema.getElement().getChildNodes();
for (int i = 0; i < nodes.getLength(); i++)
{
Node a = nodes.item(i);
String name = a.getNodeName();
if (name.startsWith(prefix))
{
if (a instanceof Element)
{
Element e = (Element) a;
if (nodes.getLength() > 0)
{
NodeList seqList = e.getElementsByTagName("rdf:Seq");
if (seqList.getLength() > 0)
{
List< String > newList = xmpSchema.getSequenceList(name);
List< String > oldList = getSequenceList(name);
Iterator< String > it = newList.iterator();
while (it.hasNext())
{
String object = it.next();
if (oldList == null
|| !oldList.contains(object))
{
addSequenceValue(name, object);
}
}
continue;
}
NodeList bagList = e.getElementsByTagName("rdf:Bag");
if (bagList.getLength() > 0)
{
List< String > newList = xmpSchema.getBagList(name);
List< String > oldList = getBagList(name);
Iterator< String > it = newList.iterator();
while (it.hasNext())
{
String object = it.next();
if (oldList == null
|| !oldList.contains(object))
{
addBagValue(name, object);
}
}
continue;
}
}
}
String newValue = xmpSchema.getTextProperty(name);
setTextProperty(name, newValue);
}
}
}
A basic schema merge, it merges bags and sequences and replace everything
else. |
public void removeBagValue(String bagName,
String bagValue) {
Element bagElement = null;
NodeList nodes = schema.getElementsByTagName(bagName);
if (nodes.getLength() > 0)
{
Element contElement = (Element) nodes.item(0);
NodeList bagList = contElement.getElementsByTagName("rdf:Bag");
if (bagList.getLength() > 0)
{
bagElement = (Element) bagList.item(0);
NodeList items = bagElement.getElementsByTagName("rdf:li");
for (int i = items.getLength() - 1; i >= 0; i--)
{
Element li = (Element) items.item(i);
String value = XMLUtil.getStringValue(li);
if (value.equals(bagValue))
{
bagElement.removeChild(li);
}
}
}
}
}
Remove all matching entries with the given value from the bag. |
public void removeSequenceDateValue(String seqName,
Calendar date) {
String dateAsString = DateConverter.toISO8601(date);
removeSequenceValue(seqName, dateAsString);
}
Remove a date sequence value from the list. |
public void removeSequenceValue(String seqName,
String seqValue) {
Element bagElement = null;
NodeList nodes = schema.getElementsByTagName(seqName);
if (nodes.getLength() > 0)
{
Element contElement = (Element) nodes.item(0);
NodeList bagList = contElement.getElementsByTagName("rdf:Seq");
if (bagList.getLength() > 0)
{
bagElement = (Element) bagList.item(0);
NodeList items = bagElement.getElementsByTagName("rdf:li");
for (int i = items.getLength() - 1; i >= 0; i--)
{
Element li = (Element) items.item(i);
String value = XMLUtil.getStringValue(li);
if (value.equals(seqValue))
{
bagElement.removeChild(li);
}
}
}
}
}
Remove all matching values from a sequence property. |
public void removeSequenceValue(String seqName,
Elementable seqValue) {
Element bagElement = null;
NodeList nodes = schema.getElementsByTagName(seqName);
if (nodes.getLength() > 0)
{
Element contElement = (Element) nodes.item(0);
NodeList bagList = contElement.getElementsByTagName("rdf:Seq");
if (bagList.getLength() > 0)
{
bagElement = (Element) bagList.item(0);
NodeList items = bagElement.getElementsByTagName("rdf:li");
for (int i = 0; i < items.getLength(); i++)
{
Element li = (Element) items.item(i);
if (li == seqValue.getElement())
{
bagElement.removeChild(li);
}
}
}
}
}
Remove a value from a sequence property. This will remove all entries
from the list. |
public void setAbout(String about) {
if (about == null)
{
schema.removeAttribute("rdf:about");
}
else
{
schema.setAttribute("rdf:about", about);
}
}
Set the RDF 'about' attribute. Passing in null will clear this attribute. |
public void setBooleanProperty(String propertyName,
Boolean bool) {
String value = null;
if (bool != null)
{
value = bool.booleanValue() ? "True" : "False";
}
setTextProperty(propertyName, value);
}
Set the value of the property as a boolean. |
public void setDateProperty(String propertyName,
Calendar date) {
setTextProperty(propertyName, DateConverter.toISO8601(date));
}
Set the value of the property as a date. |
public void setIntegerProperty(String propertyName,
Integer intValue) {
String textValue = null;
if (intValue != null)
{
textValue = intValue.toString();
}
setTextProperty(propertyName, textValue);
}
Set the value of the property as an integer. |
public void setLanguageProperty(String propertyName,
String language,
String value) {
NodeList nodes = schema.getElementsByTagName(propertyName);
Element property = null;
if (nodes.getLength() == 0)
{
if (value == null)
{
// value is null, it doesn't already exist so there
// is nothing to do.
return;
}
property = schema.getOwnerDocument().createElement(propertyName);
schema.appendChild(property);
}
else
{
property = (Element) nodes.item(0);
}
Element alt = null;
NodeList altList = property.getElementsByTagName("rdf:Alt");
if (altList.getLength() == 0)
{
if (value == null)
{
// value is null, it doesn't already exist so there
// is nothing to do.
return;
}
alt = schema.getOwnerDocument().createElement("rdf:Alt");
property.appendChild(alt);
}
else
{
alt = (Element) altList.item(0);
}
NodeList items = alt.getElementsByTagName("rdf:li");
if (language == null)
{
language = "x-default";
}
boolean foundValue = false;
for (int i = 0; i < items.getLength(); i++)
{
Element li = (Element) items.item(i);
if (value == null)
{
alt.removeChild(li);
}
else if (language.equals(li.getAttribute("xml:lang")))
{
foundValue = true;
XMLUtil.setStringValue(li, value);
}
}
if (value != null && !foundValue)
{
Element li = schema.getOwnerDocument().createElement("rdf:li");
li.setAttribute("xml:lang", language);
XMLUtil.setStringValue(li, value);
if (language.equals("x-default"))
{
// default should be first element, see XMP spec
alt.insertBefore(li, alt.getFirstChild());
}
else
{
alt.appendChild(li);
}
}
}
Set the value of a multi-lingual property. |
public void setTextProperty(String propertyName,
String propertyValue) {
if (propertyValue == null)
{
schema.removeAttribute(propertyName);
NodeList keywordList = schema.getElementsByTagName(propertyName);
for (int i = 0; i < keywordList.getLength(); i++)
{
schema.removeChild(keywordList.item(i));
}
}
else
{
if (schema.hasAttribute(propertyName))
{
schema.setAttribute(propertyName, propertyValue);
}
else
{
if (schema.hasChildNodes())
{
NodeList nodeList = schema
.getElementsByTagName(propertyName);
if (nodeList.getLength() > 0)
{
Element node = (Element) nodeList.item(0);
node.setNodeValue(propertyValue);
}
else
{
Element textNode = schema.getOwnerDocument()
.createElement(propertyName);
XMLUtil.setStringValue(textNode, propertyValue);
schema.appendChild(textNode);
}
}
else
{
schema.setAttribute(propertyName, propertyValue);
}
}
}
}
Set a simple text property on the schema. |
public void setThumbnailProperty(String propertyName,
String language,
Thumbnail value) {
NodeList nodes = schema.getElementsByTagName(propertyName);
Element property = null;
if (nodes.getLength() == 0)
{
if (value == null)
{
// value is null, it doesn't already exist so there
// is nothing to do.
return;
}
property = schema.getOwnerDocument().createElement(propertyName);
schema.appendChild(property);
}
else
{
property = (Element) nodes.item(0);
}
Element alt = null;
NodeList altList = property.getElementsByTagName("rdf:Alt");
if (altList.getLength() == 0)
{
if (value == null)
{
// value is null, it doesn't already exist so there
// is nothing to do.
return;
}
alt = schema.getOwnerDocument().createElement("rdf:Alt");
property.appendChild(alt);
}
else
{
alt = (Element) altList.item(0);
}
NodeList items = alt.getElementsByTagName("rdf:li");
if (language == null)
{
language = "x-default";
}
boolean foundValue = false;
for (int i = 0; i < items.getLength(); i++)
{
Element li = (Element) items.item(i);
if (value == null)
{
alt.removeChild(li);
}
else if (language.equals(li.getAttribute("xml:lang")))
{
foundValue = true;
alt.replaceChild(li, value.getElement());
}
}
if (value != null && !foundValue)
{
Element li = value.getElement();
li.setAttribute("xml:lang", language);
if (language.equals("x-default"))
{
// default should be first element, see XMP spec
alt.insertBefore(li, alt.getFirstChild());
}
else
{
alt.appendChild(li);
}
}
}
Set the value of a multi-lingual property. |