org.activemath.omdocjdom
Class OJElement

java.lang.Object
  extended by org.jdom.Content
      extended by org.jdom.Element
          extended by org.activemath.omdocjdom.OJElement
All Implemented Interfaces:
Serializable, Cloneable, Infoable, OJNode, Parent
Direct Known Subclasses:
Dependency, Dependency.IDElement, JDM, OJDynamicItem, OJgroup, OJItem, OJoms, OJref

public class OJElement
extends Element
implements Infoable, OJNode

Objects of these classes are any XML element of an OMdoc. It stores almost no field but has mostly utility methods that represent the informations one can retrieve from such an element. This class is not very optimized, caching of some results (like the containgTheoryIDstring) might help.

This class mostly cannot contain any write methods as it is too generic.

Future of this class might include a "comparison" utility.

Version:
See Also:
Serialized Form

Field Summary
protected static boolean DEBUG
           
protected  ID id
          A buffer to store the id of the element for the first time it is computed.
protected  Object info
           
protected static org.apache.commons.logging.Log log
           
 
Fields inherited from class org.jdom.Element
additionalNamespaces, name, namespace
 
Fields inherited from class org.jdom.Content
parent
 
Constructor Summary
protected OJElement()
          Constructs an OJElement whose namespace is the OMDoc Namespace.
  OJElement(String name)
          Constructs an OJElement whose namespace is the OMDoc Namespace and name is specified.
  OJElement(String name, Namespace namespace)
          Constructs an OJElement whose namespace and name is given.
 
Method Summary
 void addContentMaybeCloning(Comment child)
          Adds the indicated child into this element possibly Element.clone()ing it if needs be (that is if the element has a parent or has a document)
 void addContentMaybeCloning(Element child)
          Adds the indicated child into this element possibly Element.clone()ing it if needs be (that is if the element has a parent or has a document)
 void addContentMaybeCloning(EntityRef child)
          Adds the indicated child into this element possibly Element.clone()ing it if needs be (that is if the element has a parent or has a document)
 void addContentMaybeCloning(ProcessingInstruction child)
          Adds the indicated child into this element possibly Element.clone()ing it if needs be (that is if the element has a parent or has a document)
 void addContentMaybeCloning(Text child)
          Adds the indicated child into this element possibly Element.clone()ing it if needs be (that is if the element has a parent or has a document)
 OJElement addDCmetadata(String name, String role, String language, String value)
          Creates the metadata element if need be and insert the indicated Dublin-Core metadata element at the end of it.
 void addProperlyIndented(int pos, OJElement elt)
          Adds with correct indenting the element elt after the element at the pos position among the children
 void addProperlyIndented(OJElement elt)
          Adds the given element with correct indenting at the end of the this element.
 void clearExtraNamespaces()
           
 OJElement findChildWithID(OJElement elt, ID searchedID)
          A method to find back id-ed elements.
 String getAnyTitle()
           
 String getAnyTitle(String defaultValue)
          Return any title, or the supplied default string if there is none.
 List getChildrenRefs()
          Create a list with "xref" ids of all content items ("omd:ref")
 OJElement getCompleteDCMetadata()
          Walks all the parents collecting all metadata informations of namespace OJConstants.DC_NAMESPACE, returns a new node which has no parent.
 OJItem getContainingItem()
          Checks recursively the ancestors of the given node and returns the first implementing OJItem.
 OJElement getContainingTheory()
          Checks first if an attribute "theory" exists, if yes returns it, otherwise checks the ancestors of the given node and returns the first of the parents whose name is theory.
 String getContainingTheoryIDstring()
          Covers all the cases in providing the theory attached to this element.
 String getCreator()
           
 ID getEnclosingID()
          Walks till the root element to find a parent of this element that has an id attribute (including itself) and returns its id.
 OJElement getFirstDCMetadata(String metadataName)
          Climbs from here through the parents to return the first found child of the metadata element with the given name.
 ID getID()
          Returns an ID object associated to this node, the buffered one, or recomputes it from its parents, storing it in a local variable which will at later calls to this method.
 String getIndentOfElementWithNL()
          Returns the string representing the whitespace between this tag and the start of line.
 Object getInfo()
           
 String getLanguage()
          Returns the value of the xml:lang attribute of the first ancestor of the element that has one.
 String getNL()
           
 OJElement getOrMakeMetadata()
           
 OJElement getorMakeMetadataExtradata()
          A utility method to return the extradata child of the item and create it, if it is missing.
 OJElement getParentOJElement()
           
 List getRealContent()
          Deprecated. Since jdom-b8 is in, this method is not needed as all the lists and iterators are now properly behaving to removals or additions.
 String getTitle(String preferredLanguage)
          Returns the name in the given preferred-language (as displayed in the OMdoc DTD).
 Map getTitleMap()
          Returns a map of all titles with key the two letter-language code.
 Map getTitleMapAsJdomElts()
          Returns a map associating language two-characters-strings to content of Title or commonname JDOM-elements suitable for transformation for example.
 Map getTitleMapAsJdomNodeList()
           
 Map getTitleMapAsXmlString()
           
 List getUnionOfDCMetadataEntries(String metadataName)
          Climbs through the ancestors of the element and gathers the metadata children with the given name.
 Iterator listItems()
          Provides an iterator of all the OJItems in the document.
 void setID(ID id)
          Makes this element believe his id is the given one.
 void setID(String name)
           
 void setInfo(Object i)
           
 Element setNamespace(Namespace ns)
           
 String toString()
           
 
Methods inherited from class org.jdom.Element
addContent, addContent, addContent, addContent, addContent, addNamespaceDeclaration, clone, cloneContent, getAdditionalNamespaces, getAttribute, getAttribute, getAttributes, getAttributeValue, getAttributeValue, getAttributeValue, getAttributeValue, getChild, getChild, getChildren, getChildren, getChildren, getChildText, getChildText, getChildTextNormalize, getChildTextNormalize, getChildTextTrim, getChildTextTrim, getContent, getContent, getContent, getContentSize, getDescendants, getDescendants, getName, getNamespace, getNamespace, getNamespacePrefix, getNamespaceURI, getQualifiedName, getText, getTextNormalize, getTextTrim, getValue, indexOf, isAncestor, isRootElement, removeAttribute, removeAttribute, removeAttribute, removeChild, removeChild, removeChildren, removeChildren, removeContent, removeContent, removeContent, removeContent, removeNamespaceDeclaration, setAttribute, setAttribute, setAttribute, setAttributes, setAttributes, setContent, setContent, setContent, setContent, setName, setText
 
Methods inherited from class org.jdom.Content
detach, equals, getDocument, getParent, getParentElement, hashCode, setParent
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.activemath.omdocjdom.OJNode
getDocument, getParent
 
Methods inherited from interface org.jdom.Parent
getDocument, getParent
 

Field Detail

log

protected static org.apache.commons.logging.Log log

DEBUG

protected static boolean DEBUG

id

protected ID id
A buffer to store the id of the element for the first time it is computed. Beware that it is not reset by anything like a detach or setParent(), whereas there may be reasons it should


info

protected Object info
Constructor Detail

OJElement

protected OJElement()
Constructs an OJElement whose namespace is the OMDoc Namespace.


OJElement

public OJElement(String name,
                 Namespace namespace)
Constructs an OJElement whose namespace and name is given.


OJElement

public OJElement(String name)
Constructs an OJElement whose namespace is the OMDoc Namespace and name is specified.

Method Detail

setNamespace

public Element setNamespace(Namespace ns)
Overrides:
setNamespace in class Element

getParentOJElement

public OJElement getParentOJElement()

getRealContent

public List getRealContent()
Deprecated. Since jdom-b8 is in, this method is not needed as all the lists and iterators are now properly behaving to removals or additions.

Gets the content list of the element, without backing it. This allows listIterator operations to be performed. It will be deprecated once jdom goes b8 and the FilterList are through.

Please note that this content can be null when no children are in yet.


getContainingTheory

public OJElement getContainingTheory()
Checks first if an attribute "theory" exists, if yes returns it, otherwise checks the ancestors of the given node and returns the first of the parents whose name is theory.

Note that this does not cover the possible declaration theory= for a given parent. For more safety, you should use and compare getContainingTheoryIDstring().


getContainingTheoryIDstring

public String getContainingTheoryIDstring()
Covers all the cases in providing the theory attached to this element. Those cases are either that the attribute "theory=" of some parent element (or itself) in which case this value is returned, either that a theory element exists as parent of this element in which case the id of this theory is returned.


getContainingItem

public OJItem getContainingItem()
Checks recursively the ancestors of the given node and returns the first implementing OJItem. For this to work, the document must have been built with OJItem implementing object where there should be items (i.e. where the name is in the array OJConstants.ITEM_NAMES. #returns null>/code> if there is no such parent


getLanguage

public String getLanguage()
Returns the value of the xml:lang attribute of the first ancestor of the element that has one.


getTitle

public String getTitle(String preferredLanguage)
Returns the name in the given preferred-language (as displayed in the OMdoc DTD). If this is not found, it returns one that has no language and if there is no such then it returns the first one, if any, otherwise null.

This is in accordance with the possibility that the language is only given in an enclosing element, e.g. for a single-language document like the ones QMath generates.


getAnyTitle

public String getAnyTitle()

getAnyTitle

public String getAnyTitle(String defaultValue)
Return any title, or the supplied default string if there is none.


getTitleMapAsXmlString

public Map getTitleMapAsXmlString()

getTitleMapAsJdomElts

public Map getTitleMapAsJdomElts()
Returns a map associating language two-characters-strings to content of Title or commonname JDOM-elements suitable for transformation for example.

Returns:
the map

getTitleMapAsJdomNodeList

public Map getTitleMapAsJdomNodeList()

getTitleMap

public Map getTitleMap()
Returns a map of all titles with key the two letter-language code.


getChildrenRefs

public List getChildrenRefs()
Create a list with "xref" ids of all content items ("omd:ref")

Returns:
List of id strings

getUnionOfDCMetadataEntries

public List getUnionOfDCMetadataEntries(String metadataName)
Climbs through the ancestors of the element and gathers the metadata children with the given name.


getFirstDCMetadata

public OJElement getFirstDCMetadata(String metadataName)
Climbs from here through the parents to return the first found child of the metadata element with the given name.

This looks like an appropriate procedure for Date and possibly Contributor, or Title.

Note that this method performs a dirty fallback: it supposes it can happen that the elment is not in the Dublin-Core namespace and returns this element as well.


getCompleteDCMetadata

public OJElement getCompleteDCMetadata()
Walks all the parents collecting all metadata informations of namespace OJConstants.DC_NAMESPACE, returns a new node which has no parent.


setID

public void setID(ID id)
Makes this element believe his id is the given one. Further calls to getID() will return this object unless null in which case it will try to recompute it. This does not, however, set the attributes accordingly.


getID

public ID getID()
Returns an ID object associated to this node, the buffered one, or recomputes it from its parents, storing it in a local variable which will at later calls to this method. This is built using the id attribute of the given element plus the id attribute of the possibly enclosing theory element and the collection of the enclosing document. Alternatively, if the id attribute starts with mbase:// then it is considered an absolute id and is only used.


getEnclosingID

public ID getEnclosingID()
Walks till the root element to find a parent of this element that has an id attribute (including itself) and returns its id.

This method requires all parent of this element to be of class OJElement.

Returns:
the ID found or null no suitable one was found.

findChildWithID

public OJElement findChildWithID(OJElement elt,
                                 ID searchedID)
A method to find back id-ed elements. the current one performs a real search, it may be however, a time where the elements that can be IDs (as of the DTD) and have one ID register themselves so that ID manipulations is much faster.

The current method goes through the whole tree stopping at OMOBJ elements' depth until it finds the given ID. At each node, it requests the ID (using getID()) and compares it to this one.


listItems

public Iterator listItems()
Provides an iterator of all the OJItems in the document. These are read along the next() method so any change in the underlying structure might create concurrency exceptions.


addContentMaybeCloning

public void addContentMaybeCloning(Element child)
Adds the indicated child into this element possibly Element.clone()ing it if needs be (that is if the element has a parent or has a document)


addContentMaybeCloning

public void addContentMaybeCloning(Text child)
Adds the indicated child into this element possibly Element.clone()ing it if needs be (that is if the element has a parent or has a document)


addContentMaybeCloning

public void addContentMaybeCloning(ProcessingInstruction child)
Adds the indicated child into this element possibly Element.clone()ing it if needs be (that is if the element has a parent or has a document)


addContentMaybeCloning

public void addContentMaybeCloning(Comment child)
Adds the indicated child into this element possibly Element.clone()ing it if needs be (that is if the element has a parent or has a document)


addContentMaybeCloning

public void addContentMaybeCloning(EntityRef child)
Adds the indicated child into this element possibly Element.clone()ing it if needs be (that is if the element has a parent or has a document)


setInfo

public void setInfo(Object i)
Specified by:
setInfo in interface Infoable

getInfo

public Object getInfo()
Specified by:
getInfo in interface Infoable

getOrMakeMetadata

public OJElement getOrMakeMetadata()

getorMakeMetadataExtradata

public OJElement getorMakeMetadataExtradata()
A utility method to return the extradata child of the item and create it, if it is missing.


addDCmetadata

public OJElement addDCmetadata(String name,
                               String role,
                               String language,
                               String value)
Creates the metadata element if need be and insert the indicated Dublin-Core metadata element at the end of it.


addProperlyIndented

public void addProperlyIndented(OJElement elt)
Adds the given element with correct indenting at the end of the this element.

Parameters:
elt - the element to introduce

addProperlyIndented

public void addProperlyIndented(int pos,
                                OJElement elt)
Adds with correct indenting the element elt after the element at the pos position among the children

Parameters:
pos - the child index after which it needs to be inserted
elt - that needs to be inserted.

setID

public void setID(String name)

getCreator

public String getCreator()

clearExtraNamespaces

public void clearExtraNamespaces()

getNL

public String getNL()

getIndentOfElementWithNL

public String getIndentOfElementWithNL()
Returns the string representing the whitespace between this tag and the start of line.

Returns:
the string from the last EOL (included) to this start tag.

toString

public String toString()
Overrides:
toString in class Element