org.netbeans.modules.xml.xam.dom
Class AbstractDocumentComponent<C extends DocumentComponent<C>>

java.lang.Object
  extended by org.netbeans.modules.xml.xam.AbstractComponent<C>
      extended by org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent<C>
All Implemented Interfaces:
Component<C>, DocumentComponent<C>, DocumentModelAccess.NodeUpdater
Direct Known Subclasses:
AnotherVersionBpelProcessImpl, BpelEntityImpl, org.netbeans.modules.xml.wsdl.model.spi.WSDLComponentBase

public abstract class AbstractDocumentComponent<C extends DocumentComponent<C>>
extends AbstractComponent<C>
implements DocumentComponent<C>, DocumentModelAccess.NodeUpdater

Author:
rico, Vidhya Narayanan, Chris Webster, Nam Nguyen

Nested Class Summary
static class AbstractDocumentComponent.PrefixAttribute
           
 
Field Summary
 
Fields inherited from interface org.netbeans.modules.xml.xam.dom.DocumentComponent
TEXT_CONTENT_PROPERTY
 
Constructor Summary
AbstractDocumentComponent(AbstractDocumentModel model, org.w3c.dom.Element e)
           
 
Method Summary
 void addPrefix(java.lang.String prefix, java.lang.String namespace)
          Declare prefix for given namespace (without any refactoring action).
protected  void appendChildQuietly(C component, java.util.List<C> children)
           
 DocumentComponent copy(C parent)
          Returns a copy of this component for adding into the given parent component.
protected  Attribute createPrefixAttribute(java.lang.String prefix)
           
protected  java.lang.String ensureUnique(java.lang.String prefix, java.lang.String namespace)
          Returns a unique prefix for the given namespace by appending number from 1 to 100.
protected  void ensureValueNamespaceDeclared(java.lang.String newNamespace, java.lang.String oldNamespace, java.lang.String preferredPrefix)
           
 int findAttributePosition(java.lang.String attributeName)
          Returns position of the attribute by the given name, or -1 if not found.
 C findChildComponent(org.w3c.dom.Element e)
          Returns child component backed by given element node.
 C findChildComponentByIdentity(org.w3c.dom.Element e)
           
protected  int findDomainIndex(org.w3c.dom.Element e)
           
 int findPosition()
          Returns the position of this component in the schema document, expressed as an offset from the start of the document.
protected  void fireChildAdded()
           
protected  void fireChildRemoved()
           
protected  void firePropertyChange(java.lang.String propName, java.lang.Object oldValue, java.lang.Object newValue)
           
protected  void fireValueChanged()
           
protected  DocumentModelAccess getAccess()
           
 java.lang.String getAnyAttribute(javax.xml.namespace.QName attr)
          Returns string value of the attribute from different namespace.
 java.lang.String getAttribute(Attribute attr)
           
 java.util.Map<javax.xml.namespace.QName,java.lang.String> getAttributeMap()
           
protected abstract  java.lang.Object getAttributeValueOf(Attribute attr, java.lang.String stringValue)
           
protected  org.w3c.dom.Element getChildElement(javax.xml.namespace.QName qname)
           
protected  java.lang.String getChildElementText(javax.xml.namespace.QName qname)
          Returns value of all text nodes from the child element with given QName.
protected  AbstractDocumentComponent getEffectiveParent()
           
protected  java.lang.String getLeadingText(C child)
          Returns leading text for the child component of the given index.
 AbstractDocumentModel getModel()
           
protected  java.lang.String getNamespaceURI()
           
protected  int getNodeIndexOf(org.w3c.dom.Node parent, org.w3c.dom.Node child)
           
 org.w3c.dom.Element getPeer()
          Returns the DOM element corresponding to this component.
protected  java.lang.String getPrefixedName(javax.xml.namespace.QName q, boolean declarePrefix)
           
protected  java.lang.String getPrefixedName(java.lang.String namespace, java.lang.String localName)
           
protected  java.lang.String getPrefixedName(java.lang.String namespace, java.lang.String name, java.lang.String prefix, boolean declarePrefix)
           
 java.util.Map<java.lang.String,java.lang.String> getPrefixes()
           
 javax.xml.namespace.QName getQName()
          Returns QName of the component.
static javax.xml.namespace.QName getQName(org.w3c.dom.Node n)
           
protected  java.lang.String getText()
          Return text value of this component.
protected  java.lang.String getText(C child, boolean leading, boolean includeComments)
           
static java.lang.String getText(org.w3c.dom.Element e)
           
protected  java.lang.String getTrailingText(C child)
          Returns trailing text for the child component of the given index.
protected  java.lang.String getXmlFragment()
          Get the XML fragment text that make up the children the component peer node.
 java.lang.String getXmlFragmentInclusive()
          Get the XML fragment text that make up the peer node.
protected  void insertAtIndexQuietly(C newComponent, java.util.List<C> children, int index)
           
 boolean isInDocumentModel()
          Returns true if the component is part of the document model.
 java.lang.String lookupNamespaceURI(java.lang.String prefix)
           
 java.lang.String lookupNamespaceURI(java.lang.String prefix, boolean optimized)
          Returns namespace for the given prefix.
 java.lang.String lookupPrefix(java.lang.String namespace)
           
protected abstract  void populateChildren(java.util.List<C> children)
           
 boolean referencesSameNode(org.w3c.dom.Node n)
          Returns true if the node referenced by this component is n.
protected  void removeAttributeQuietly(org.w3c.dom.Element element, java.lang.String name)
           
protected  void removeChildQuietly(C component, java.util.List<C> children)
           
 void removePrefix(java.lang.String prefix)
          Remove declared prefix (without refactoring).
protected  ModelSource resolveModel(java.lang.String hint)
          Resolves reference to external models using location hint.
 void setAnyAttribute(javax.xml.namespace.QName attr, java.lang.String value)
          Set string value of the attribute identified by given QName.
 void setAttribute(java.lang.String eventPropertyName, Attribute attr, java.lang.Object value)
          Sets the component attribute String value and fire property change event with the given property name.
protected  void setAttributeQuietly(Attribute type, java.lang.Object newVal)
           
protected  void setChildElementText(java.lang.String propertyName, java.lang.String text, javax.xml.namespace.QName qname)
          Set the value of the text node from the child element with given QName.
protected  void setLeadingText(java.lang.String propName, java.lang.String text, C child)
          Set leading text for the child component which position is the given index.
protected  void setQNameAttribute(java.lang.String propertyName, javax.xml.namespace.QName attr, java.lang.String value)
           
protected  void setText(java.lang.String propertyName, java.lang.String text)
          Set text value of the component.
protected  void setText(java.lang.String propName, java.lang.String value, C child, boolean leading, boolean includeComments)
           
protected  void setTrailingText(java.lang.String propName, java.lang.String text, C child)
          Set trailing text for the child component which position is the given index.
protected  void setXmlFragment(java.lang.String propertyName, java.lang.String text)
          Set text as XML fragment children the component peer node.
protected  void updatePeer(java.lang.String propertyName, org.w3c.dom.Element newPeer)
          Shared utility for implementation to replace the current peer and ensure the document tree also get update properly.
 void updateReference(org.w3c.dom.Element element)
           
protected
<N extends org.w3c.dom.Node>
void
updateReference(org.w3c.dom.Element peer, java.util.List<N> updatingPath)
          Updates peer node with given peer and the path for context of the update.
<N extends org.w3c.dom.Node>
void
updateReference(java.util.List<N> pathToRoot)
          Update all parents with fresh nodes.
protected  void verifyWrite()
           
 
Methods inherited from class org.netbeans.modules.xml.xam.AbstractComponent
addAfter, addBefore, addPropertyChangeListener, appendChild, canPaste, checkNullOrDuplicateChild, getChild, getChildren, getChildren, getChildren, getParent, insertAtIndex, insertAtIndex, isChildrenInitialized, removeChild, removeComponentListener, removePropertyChangeListener, setChild, setChild, setChildAfter, setChildBefore, setModel, setParent
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.netbeans.modules.xml.xam.Component
canPaste, getChildren, getChildren, getChildren, getParent
 

Constructor Detail

AbstractDocumentComponent

public AbstractDocumentComponent(AbstractDocumentModel model,
                                 org.w3c.dom.Element e)
Method Detail

populateChildren

protected abstract void populateChildren(java.util.List<C> children)
Specified by:
populateChildren in class AbstractComponent<C extends DocumentComponent<C>>

getPeer

public org.w3c.dom.Element getPeer()
Description copied from interface: DocumentComponent
Returns the DOM element corresponding to this component.

Specified by:
getPeer in interface DocumentComponent<C extends DocumentComponent<C>>

getAttribute

public java.lang.String getAttribute(Attribute attr)
Specified by:
getAttribute in interface DocumentComponent<C extends DocumentComponent<C>>
Returns:
attribute value or null if the attribute is currently undefined

setAttribute

public void setAttribute(java.lang.String eventPropertyName,
                         Attribute attr,
                         java.lang.Object value)
Sets the component attribute String value and fire property change event with the given property name.

Specified by:
setAttribute in interface DocumentComponent<C extends DocumentComponent<C>>
Parameters:
eventPropertyName - property name to be used in firing property change event.
attr - attribute name
value - for the attribute.

getAttributeValueOf

protected abstract java.lang.Object getAttributeValueOf(Attribute attr,
                                                        java.lang.String stringValue)

getAnyAttribute

public java.lang.String getAnyAttribute(javax.xml.namespace.QName attr)
Returns string value of the attribute from different namespace. If given QName has prefix, it will be ignored.

Parameters:
attr - non-null QName represents the attribute name.
Returns:
attribute value

setAnyAttribute

public void setAnyAttribute(javax.xml.namespace.QName attr,
                            java.lang.String value)
Set string value of the attribute identified by given QName. This will fire property change event using attribute local name.

Parameters:
attr - non-null QName represents the attribute name.
value - string value for the attribute.

setQNameAttribute

protected void setQNameAttribute(java.lang.String propertyName,
                                 javax.xml.namespace.QName attr,
                                 java.lang.String value)

getPrefixedName

protected java.lang.String getPrefixedName(javax.xml.namespace.QName q,
                                           boolean declarePrefix)

getPrefixedName

protected java.lang.String getPrefixedName(java.lang.String namespace,
                                           java.lang.String localName)

getPrefixedName

protected java.lang.String getPrefixedName(java.lang.String namespace,
                                           java.lang.String name,
                                           java.lang.String prefix,
                                           boolean declarePrefix)

ensureUnique

protected java.lang.String ensureUnique(java.lang.String prefix,
                                        java.lang.String namespace)
Returns a unique prefix for the given namespace by appending number from 1 to 100.


setAttributeQuietly

protected void setAttributeQuietly(Attribute type,
                                   java.lang.Object newVal)

removeAttributeQuietly

protected void removeAttributeQuietly(org.w3c.dom.Element element,
                                      java.lang.String name)

appendChildQuietly

protected void appendChildQuietly(C component,
                                  java.util.List<C> children)
Specified by:
appendChildQuietly in class AbstractComponent<C extends DocumentComponent<C>>

insertAtIndexQuietly

protected void insertAtIndexQuietly(C newComponent,
                                    java.util.List<C> children,
                                    int index)
Specified by:
insertAtIndexQuietly in class AbstractComponent<C extends DocumentComponent<C>>

removeChildQuietly

protected void removeChildQuietly(C component,
                                  java.util.List<C> children)
Specified by:
removeChildQuietly in class AbstractComponent<C extends DocumentComponent<C>>

getNamespaceURI

protected java.lang.String getNamespaceURI()

lookupNamespaceURI

public java.lang.String lookupNamespaceURI(java.lang.String prefix,
                                           boolean optimized)
Returns namespace for the given prefix. If optimized is specified, lookup will use component tree hierarchy instead of the underlying DOM tree hiearchy.


lookupNamespaceURI

public java.lang.String lookupNamespaceURI(java.lang.String prefix)

lookupPrefix

public java.lang.String lookupPrefix(java.lang.String namespace)

getXmlFragment

protected java.lang.String getXmlFragment()
Get the XML fragment text that make up the children the component peer node.


getXmlFragmentInclusive

public java.lang.String getXmlFragmentInclusive()
Get the XML fragment text that make up the peer node.


setXmlFragment

protected void setXmlFragment(java.lang.String propertyName,
                              java.lang.String text)
                       throws java.io.IOException
Set text as XML fragment children the component peer node. The children of peer node will be replaced with nodes resulted from parsing given text. This method will not fire child component added or removed events. This method should only be exposed on leaf component.

Parameters:
propertyName - name of property event to fire
text - text value to set to.
Throws:
java.io.IOException - if text is not well-formed.

setText

protected void setText(java.lang.String propertyName,
                       java.lang.String text)
Set text value of the component. This is for pure text-usage by documentation components. The children of peer node will be replaced with single text node having given text.

Parameters:
propertyName - name of property event to fire
text - text value to set to.

getText

protected java.lang.String getText()
Return text value of this component. This is for text-usage by doucmentation components. Non-text children node are ignored.

Returns:
aggregated text string of all child text nodes.

getText

public static java.lang.String getText(org.w3c.dom.Element e)

getModel

public AbstractDocumentModel getModel()
Specified by:
getModel in interface Component<C extends DocumentComponent<C>>
Overrides:
getModel in class AbstractComponent<C extends DocumentComponent<C>>
Returns:
the model where this element is being used or null if not currently part of a model.

referencesSameNode

public boolean referencesSameNode(org.w3c.dom.Node n)
Description copied from interface: DocumentComponent
Returns true if the node referenced by this component is n.

Specified by:
referencesSameNode in interface DocumentComponent<C extends DocumentComponent<C>>

updateReference

public void updateReference(org.w3c.dom.Element element)
Specified by:
updateReference in interface DocumentModelAccess.NodeUpdater

updateReference

public <N extends org.w3c.dom.Node> void updateReference(java.util.List<N> pathToRoot)
Update all parents with fresh nodes. This implemenation assume optimal case where the length of pathToRootNode is same as or longer than path to root component. The update will skip uncorrelated nodes in pathToRootNode. Individual will need to override this implementation if it has special needs.

Specified by:
updateReference in interface DocumentModelAccess.NodeUpdater

updateReference

protected <N extends org.w3c.dom.Node> void updateReference(org.w3c.dom.Element peer,
                                                            java.util.List<N> updatingPath)
Updates peer node with given peer and the path for context of the update. The default behavior just call #updateReference(Element peer). Subclass with special need for auxiliary update needs to override.

Parameters:
peer - the peer node to update with
updatingPath - full path for context of the update

getAccess

protected DocumentModelAccess getAccess()

findPosition

public int findPosition()
Description copied from interface: DocumentComponent
Returns the position of this component in the schema document, expressed as an offset from the start of the document.

Specified by:
findPosition in interface DocumentComponent<C extends DocumentComponent<C>>
Returns:
the position of this component in the document

updatePeer

protected void updatePeer(java.lang.String propertyName,
                          org.w3c.dom.Element newPeer)
Shared utility for implementation to replace the current peer and ensure the document tree also get update properly.


createPrefixAttribute

protected Attribute createPrefixAttribute(java.lang.String prefix)

addPrefix

public void addPrefix(java.lang.String prefix,
                      java.lang.String namespace)
Declare prefix for given namespace (without any refactoring action).


removePrefix

public void removePrefix(java.lang.String prefix)
Remove declared prefix (without refactoring).


getPrefixes

public java.util.Map<java.lang.String,java.lang.String> getPrefixes()
Returns:
mapping from prefix to namespace.

ensureValueNamespaceDeclared

protected void ensureValueNamespaceDeclared(java.lang.String newNamespace,
                                            java.lang.String oldNamespace,
                                            java.lang.String preferredPrefix)

findChildComponent

public C findChildComponent(org.w3c.dom.Element e)
Description copied from interface: DocumentComponent
Returns child component backed by given element node.

Specified by:
findChildComponent in interface DocumentComponent<C extends DocumentComponent<C>>

findChildComponentByIdentity

public C findChildComponentByIdentity(org.w3c.dom.Element e)

copy

public DocumentComponent copy(C parent)
Description copied from interface: Component
Returns a copy of this component for adding into the given parent component.

Specified by:
copy in interface Component<C extends DocumentComponent<C>>

verifyWrite

protected void verifyWrite()
Overrides:
verifyWrite in class AbstractComponent<C extends DocumentComponent<C>>

firePropertyChange

protected void firePropertyChange(java.lang.String propName,
                                  java.lang.Object oldValue,
                                  java.lang.Object newValue)
Overrides:
firePropertyChange in class AbstractComponent<C extends DocumentComponent<C>>

fireValueChanged

protected void fireValueChanged()
Overrides:
fireValueChanged in class AbstractComponent<C extends DocumentComponent<C>>

fireChildRemoved

protected void fireChildRemoved()
Overrides:
fireChildRemoved in class AbstractComponent<C extends DocumentComponent<C>>

fireChildAdded

protected void fireChildAdded()
Overrides:
fireChildAdded in class AbstractComponent<C extends DocumentComponent<C>>

isInDocumentModel

public boolean isInDocumentModel()
Returns true if the component is part of the document model.

Specified by:
isInDocumentModel in interface DocumentComponent<C extends DocumentComponent<C>>

findAttributePosition

public int findAttributePosition(java.lang.String attributeName)
Description copied from interface: DocumentComponent
Returns position of the attribute by the given name, or -1 if not found.

Specified by:
findAttributePosition in interface DocumentComponent<C extends DocumentComponent<C>>

getQName

public javax.xml.namespace.QName getQName()
Returns QName of the component.


getQName

public static javax.xml.namespace.QName getQName(org.w3c.dom.Node n)

resolveModel

protected ModelSource resolveModel(java.lang.String hint)
                            throws CatalogModelException
Resolves reference to external models using location hint.

Parameters:
hint - on location of where external model reference could reside.
Returns:
the model source of the referenced model if found.
Throws:
CatalogModelException - if the model cannot be located or the hint is not well-formed URI

getAttributeMap

public java.util.Map<javax.xml.namespace.QName,java.lang.String> getAttributeMap()

findDomainIndex

protected int findDomainIndex(org.w3c.dom.Element e)

getEffectiveParent

protected AbstractDocumentComponent getEffectiveParent()

getChildElement

protected org.w3c.dom.Element getChildElement(javax.xml.namespace.QName qname)

getChildElementText

protected java.lang.String getChildElementText(javax.xml.namespace.QName qname)
Returns value of all text nodes from the child element with given QName. This method is use to implement mapping of "property" as component attribute.

Parameters:
qname - QName of the child element to get text from.

setChildElementText

protected void setChildElementText(java.lang.String propertyName,
                                   java.lang.String text,
                                   javax.xml.namespace.QName qname)
Set the value of the text node from the child element with given QName. This method is use to implement mapping of "property" as component attribute.

Parameters:
propertyName - property change event name
text - the string to set value of the child element text node.
qname - QName of the child element to get text from.

getLeadingText

protected java.lang.String getLeadingText(C child)
Returns leading text for the child component of the given index.

Parameters:
child - the child to get associated text from
Returns:
value of the leading text node, or null the indexed component peer does not have leading text nodes.

setLeadingText

protected void setLeadingText(java.lang.String propName,
                              java.lang.String text,
                              C child)
Set leading text for the child component which position is the given index.

Parameters:
child - the child to set associated text
text - value of the leading text node, or null to remove the leading text node.

getTrailingText

protected java.lang.String getTrailingText(C child)
Returns trailing text for the child component of the given index.

Parameters:
child - the child to get associated text from
Returns:
value of the leading text node, or null the indexed component peer does not have trailing text nodes.

setTrailingText

protected void setTrailingText(java.lang.String propName,
                               java.lang.String text,
                               C child)
Set trailing text for the child component which position is the given index.

Parameters:
child - the child to get associated text from
text - value of the trailing text node, or null to remove the trailing text node.

getText

protected java.lang.String getText(C child,
                                   boolean leading,
                                   boolean includeComments)

setText

protected void setText(java.lang.String propName,
                       java.lang.String value,
                       C child,
                       boolean leading,
                       boolean includeComments)

getNodeIndexOf

protected int getNodeIndexOf(org.w3c.dom.Node parent,
                             org.w3c.dom.Node child)