org.eclipse.emf.ecore.xmi.impl
Class XMLHelperImpl

java.lang.Object
  extended byorg.eclipse.emf.ecore.xmi.impl.XMLHelperImpl
All Implemented Interfaces:
XMLHelper
Direct Known Subclasses:
EMOFHelperImpl, XMIHelperImpl

public class XMLHelperImpl
extends Object
implements XMLHelper

This class handles the package to use when there is no XML namespace in an XML file.


Nested Class Summary
protected static class XMLHelperImpl.NamespaceSupport
          A helper to encode namespace prefix mappings.
 
Nested classes inherited from class org.eclipse.emf.ecore.xmi.XMLHelper
XMLHelper.ManyReference
 
Field Summary
protected  List allPrefixToURI
           
protected  Map anyPrefixesToURIs
           
protected  EClass anySimpleType
           
protected  boolean checkForDuplicates
           
protected  DanglingHREFException danglingHREFException
           
protected  boolean deresolve
           
protected  ExtendedMetaData extendedMetaData
           
protected  Map featuresToKinds
           
protected static Integer INTEGER_DATATYPE_IS_MANY
           
protected static Integer INTEGER_DATATYPE_SINGLE
           
protected static Integer INTEGER_IS_MANY_ADD
           
protected static Integer INTEGER_IS_MANY_MOVE
           
protected static Integer INTEGER_OTHER
           
protected  boolean laxFeatureProcessing
           
protected  boolean mustHavePrefix
           
protected  XMLHelperImpl.NamespaceSupport namespaceSupport
           
protected  EPackage noNamespacePackage
           
protected  EPackage.Registry packageRegistry
           
protected  Map packages
           
protected  EMap prefixesToURIs
           
protected  String processDanglingHREF
           
protected  XMLResource resource
           
protected  URI resourceURI
           
protected  boolean seenEmptyStringMapping
           
protected  Map urisToPrefixes
           
protected  XMLResource.XMLMap xmlMap
           
protected  EPackage xmlSchemaTypePackage
           
 
Fields inherited from interface org.eclipse.emf.ecore.xmi.XMLHelper
DATATYPE_IS_MANY, DATATYPE_SINGLE, IS_MANY_ADD, IS_MANY_MOVE, OTHER
 
Constructor Summary
XMLHelperImpl()
           
XMLHelperImpl(XMLResource resource)
           
 
Method Summary
protected  void addNSDeclaration(String prefix, String uri)
           
 void addPrefix(String prefix, String uri)
          Records the prefix to URI mapping while loading an XML document.
protected  void computeFeatureKind(EStructuralFeature feature)
           
 String convertToString(EFactory factory, EDataType dataType, Object value)
          Converts the given valuevalue to string while saving an XML document.
protected  Object createFromString(EFactory eFactory, EDataType eDataType, String value)
           
 EObject createObject(EFactory eFactory, EClassifier type)
          Create an object given an EFactory and a type
 EObject createObject(EFactory eFactory, String classXMIName)
          Create an object given an EFactory and a type name.
 URI deresolve(URI uri)
          Returns a relative URI if necessary and if possible.
 Map getAnyContentPrefixToURIMapping()
          Returns a map of prefixes to URI mapping for elements with any content
 DanglingHREFException getDanglingHREFException()
           
 ExtendedMetaData getExtendedMetaData()
          Gets the ExtendedMetaData to use when serializing an XML file.
 EStructuralFeature getFeature(EClass eClass, String namespaceURI, String name)
          Returns the structural feature for the XML element/attribute with the given namespaceURI and name.
 EStructuralFeature getFeature(EClass eClass, String namespaceURI, String name, boolean isElement)
          Returns the structural feature for the XML element/attribute with the given namespaceURI and name.
 int getFeatureKind(EStructuralFeature feature)
          Return the kind of feature.
protected  EStructuralFeature getFeatureWithoutMap(EClass eClass, String name)
           
 String getHREF(EObject obj)
          Returns an HREF to this object from this resource
protected  URI getHREF(Resource otherResource, EObject obj)
           
 String getID(EObject obj)
          Returns the xmi:id or null to supress
 String getIDREF(EObject obj)
          Returns an IDREF to this object
 String getJavaEncoding(String xmlEncoding)
          Return a Java encoding corresponding to the given XML encoding.
 String getName(ENamedElement obj)
          Returns the XML name of the ENamedElement
 String getNamespaceURI(String prefix)
          Returns the namespaceURI in scope for the prefix.
 EPackage getNoNamespacePackage()
          Gets the package to use when there is no XML namespace in an XML file.
 String getPrefix(EPackage ePackage)
          Return the prefix used for this package.
protected  String getPrefix(EPackage ePackage, boolean mustHavePrefix)
           
 String getPrefix(String namespaceURI)
           
 List getPrefixes(EPackage ePackage)
          Return all the prefixes used for this package.
 EMap getPrefixToNamespaceMap()
           
 String getQName(EClass c)
          Returns the nsName:name c.ePackage().nsName() : c.eName() If there is an XMLMap, and the target namespace is null for this eClass, just the name is returned.
 String getQName(EDataType c)
          Returns the nsName:name c.ePackage().nsName() : c.eName() If there is an XMLMap, and the target namespace is null for this eClass, just the name is returned.
protected  String getQName(EPackage ePackage, String name)
           
protected  String getQName(EPackage ePackage, String name, boolean mustHavePrefix)
           
 String getQName(EStructuralFeature feature)
          By default, this method returns the name of the feature.
protected  String getQName(NameInfo nameInfo, EPackage ePackage, String name)
           
protected  String getQName(NameInfo nameInfo, EPackage ePackage, String name, boolean mustHavePrefix)
           
protected  String getQName(String uri, String name)
           
 XMLResource getResource()
          Returns the resource
 EClassifier getType(EFactory eFactory, String typeName)
          Given an EFactory and a type name, find and return the type
 String getURI(String prefix)
          Returns the namespace prefix that's in effect while loading an XML document.
protected  String getURIFragment(Resource containingResource, EObject object)
           
protected  String getURIFragmentQuery(Resource containingResource, EObject object)
           
 Object getValue(EObject obj, EStructuralFeature f)
          Returns the XMI value of the EObject's feature
 String getXMLEncoding(String javaEncoding)
          Return an XML encoding corresponding to the given Java encoding.
 XMLResource.XMLMap getXMLMap()
          Gets the XMLMap to use when serializing an XML file.
protected  URI handleDanglingHREF(EObject object)
           
 EPackage[] packages()
          Returns the packages in getQName()
 void popContext()
          Reverts to the previous namespace context while loading XML document.
 void popContext(Map prefixesToFactories)
          Reverts to the previous namespace context while loading XML document and removes from the map, prefixes that have gone out of scope.
 void populateNameInfo(NameInfo nameInfo, EClass c)
          This method populates the given NameInfo with name information, resetting all the current values in the nameInfo
 void populateNameInfo(NameInfo nameInfo, EDataType eDataType)
          This method populates the given NameInfo with name information, resetting all the current values in the nameInfo
 void populateNameInfo(NameInfo nameInfo, EStructuralFeature feature)
          This method populates the given NameInfo with name information, resetting all the current values in the nameInfo
 void pushContext()
          Starts a new namespace context while loading an XML document.
 void recordPrefixToURIMapping()
          Must be called during endDocument to record all the prefix to URI mappings that have been seen in the document during loading
 URI resolve(URI relative, URI base)
           
static String saveString(Map options, List contents, String encoding, XMLHelper helper)
           
 void setAnySimpleType(EClass type)
          Sets the any simple type class.
 void setCheckForDuplicates(boolean checkForDuplicates)
           
 void setExtendedMetaData(ExtendedMetaData extendedMetaData)
          Sets the ExtendedMetaData to use when serializing an XML file.
 List setManyReference(XMLHelper.ManyReference reference, String location)
           
 void setMustHavePrefix(boolean mustHavePrefix)
          Specify if qualified names must have a prefix or could use default namespace (if possible)
 void setNoNamespacePackage(EPackage pkg)
          Sets the package to use when there is no XML namespace in an XML file.
 void setOptions(Map options)
          Sets various resource options on the helper
 void setPrefixToNamespaceMap(EMap prefixToNamespaceMap)
           
 void setProcessDanglingHREF(String value)
           
 void setResource(XMLResource resource)
           
 void setValue(EObject object, EStructuralFeature feature, Object value, int position)
          Set the value of the feature for the object.
 void setXMLMap(XMLResource.XMLMap map)
          Sets the XMLMap to use when serializing an XML file.
protected  String updateQNamePrefix(EFactory factory, EDataType dataType, Object value, boolean list)
           
protected  void updateQNameURI(Object value)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INTEGER_DATATYPE_IS_MANY

protected static final Integer INTEGER_DATATYPE_IS_MANY

INTEGER_DATATYPE_SINGLE

protected static final Integer INTEGER_DATATYPE_SINGLE

INTEGER_IS_MANY_ADD

protected static final Integer INTEGER_IS_MANY_ADD

INTEGER_IS_MANY_MOVE

protected static final Integer INTEGER_IS_MANY_MOVE

INTEGER_OTHER

protected static final Integer INTEGER_OTHER

noNamespacePackage

protected EPackage noNamespacePackage

xmlMap

protected XMLResource.XMLMap xmlMap

extendedMetaData

protected ExtendedMetaData extendedMetaData

laxFeatureProcessing

protected boolean laxFeatureProcessing

packageRegistry

protected EPackage.Registry packageRegistry

resource

protected XMLResource resource

resourceURI

protected URI resourceURI

deresolve

protected boolean deresolve

packages

protected Map packages

featuresToKinds

protected Map featuresToKinds

processDanglingHREF

protected String processDanglingHREF

danglingHREFException

protected DanglingHREFException danglingHREFException

prefixesToURIs

protected EMap prefixesToURIs

urisToPrefixes

protected Map urisToPrefixes

anyPrefixesToURIs

protected Map anyPrefixesToURIs

namespaceSupport

protected XMLHelperImpl.NamespaceSupport namespaceSupport

anySimpleType

protected EClass anySimpleType

seenEmptyStringMapping

protected boolean seenEmptyStringMapping

xmlSchemaTypePackage

protected EPackage xmlSchemaTypePackage

allPrefixToURI

protected List allPrefixToURI

checkForDuplicates

protected boolean checkForDuplicates

mustHavePrefix

protected boolean mustHavePrefix
Constructor Detail

XMLHelperImpl

public XMLHelperImpl()

XMLHelperImpl

public XMLHelperImpl(XMLResource resource)
Method Detail

saveString

public static String saveString(Map options,
                                List contents,
                                String encoding,
                                XMLHelper helper)
                         throws Exception
Throws:
Exception

setOptions

public void setOptions(Map options)
Description copied from interface: XMLHelper
Sets various resource options on the helper

Specified by:
setOptions in interface XMLHelper

setNoNamespacePackage

public void setNoNamespacePackage(EPackage pkg)
Description copied from interface: XMLHelper
Sets the package to use when there is no XML namespace in an XML file.

Specified by:
setNoNamespacePackage in interface XMLHelper

getNoNamespacePackage

public EPackage getNoNamespacePackage()
Description copied from interface: XMLHelper
Gets the package to use when there is no XML namespace in an XML file.

Specified by:
getNoNamespacePackage in interface XMLHelper

setXMLMap

public void setXMLMap(XMLResource.XMLMap map)
Description copied from interface: XMLHelper
Sets the XMLMap to use when serializing an XML file.

Specified by:
setXMLMap in interface XMLHelper

getXMLMap

public XMLResource.XMLMap getXMLMap()
Description copied from interface: XMLHelper
Gets the XMLMap to use when serializing an XML file.

Specified by:
getXMLMap in interface XMLHelper

setExtendedMetaData

public void setExtendedMetaData(ExtendedMetaData extendedMetaData)
Description copied from interface: XMLHelper
Sets the ExtendedMetaData to use when serializing an XML file.

Specified by:
setExtendedMetaData in interface XMLHelper

getExtendedMetaData

public ExtendedMetaData getExtendedMetaData()
Description copied from interface: XMLHelper
Gets the ExtendedMetaData to use when serializing an XML file.

Specified by:
getExtendedMetaData in interface XMLHelper

getResource

public XMLResource getResource()
Description copied from interface: XMLHelper
Returns the resource

Specified by:
getResource in interface XMLHelper

setResource

public void setResource(XMLResource resource)

getValue

public Object getValue(EObject obj,
                       EStructuralFeature f)
Description copied from interface: XMLHelper
Returns the XMI value of the EObject's feature

Specified by:
getValue in interface XMLHelper

getQName

public String getQName(EClass c)
Description copied from interface: XMLHelper
Returns the nsName:name c.ePackage().nsName() : c.eName() If there is an XMLMap, and the target namespace is null for this eClass, just the name is returned. If the map has a target namespace specified, that target namespace is used rather than the namespace from the EPackage.

Specified by:
getQName in interface XMLHelper

populateNameInfo

public void populateNameInfo(NameInfo nameInfo,
                             EClass c)
Description copied from interface: XMLHelper
This method populates the given NameInfo with name information, resetting all the current values in the nameInfo

Specified by:
populateNameInfo in interface XMLHelper
Parameters:
nameInfo - the name information to populate
c - the EClass for which name has to be computed
See Also:
XMLHelper.getQName(EClass)

getQName

public String getQName(EDataType c)
Description copied from interface: XMLHelper
Returns the nsName:name c.ePackage().nsName() : c.eName() If there is an XMLMap, and the target namespace is null for this eClass, just the name is returned. If the map has a target namespace specified, that target namespace is used rather than the namespace from the EPackage.

Specified by:
getQName in interface XMLHelper

populateNameInfo

public void populateNameInfo(NameInfo nameInfo,
                             EDataType eDataType)
Description copied from interface: XMLHelper
This method populates the given NameInfo with name information, resetting all the current values in the nameInfo

Specified by:
populateNameInfo in interface XMLHelper
Parameters:
nameInfo - the name information to fill in
eDataType - the EDataType for which name has to be computed
See Also:
XMLHelper.getQName(EDataType)

getQName

public String getQName(EStructuralFeature feature)
Description copied from interface: XMLHelper
By default, this method returns the name of the feature. However, if there is an XMLMap, and the map specifies a target namespace, the nsPrefix:name is returned.

Specified by:
getQName in interface XMLHelper

populateNameInfo

public void populateNameInfo(NameInfo nameInfo,
                             EStructuralFeature feature)
Description copied from interface: XMLHelper
This method populates the given NameInfo with name information, resetting all the current values in the nameInfo

Specified by:
populateNameInfo in interface XMLHelper
Parameters:
nameInfo - the name information to fill in
feature - the EStructuralFeature for which name has to be computed
See Also:
XMLHelper.getQName(EStructuralFeature)

getQName

protected String getQName(NameInfo nameInfo,
                          EPackage ePackage,
                          String name)

getQName

protected String getQName(NameInfo nameInfo,
                          EPackage ePackage,
                          String name,
                          boolean mustHavePrefix)

getQName

protected String getQName(EPackage ePackage,
                          String name)

getQName

protected String getQName(EPackage ePackage,
                          String name,
                          boolean mustHavePrefix)

getPrefix

public String getPrefix(EPackage ePackage)
Description copied from interface: XMLHelper
Return the prefix used for this package.

Specified by:
getPrefix in interface XMLHelper

getNamespaceURI

public String getNamespaceURI(String prefix)
Description copied from interface: XMLHelper
Returns the namespaceURI in scope for the prefix.

Specified by:
getNamespaceURI in interface XMLHelper

getPrefix

protected String getPrefix(EPackage ePackage,
                           boolean mustHavePrefix)

getPrefixes

public List getPrefixes(EPackage ePackage)
Description copied from interface: XMLHelper
Return all the prefixes used for this package.

Specified by:
getPrefixes in interface XMLHelper

getQName

protected String getQName(String uri,
                          String name)

getName

public String getName(ENamedElement obj)
Description copied from interface: XMLHelper
Returns the XML name of the ENamedElement

Specified by:
getName in interface XMLHelper

getID

public String getID(EObject obj)
Description copied from interface: XMLHelper
Returns the xmi:id or null to supress

Specified by:
getID in interface XMLHelper

getURIFragmentQuery

protected String getURIFragmentQuery(Resource containingResource,
                                     EObject object)

getURIFragment

protected String getURIFragment(Resource containingResource,
                                EObject object)

getIDREF

public String getIDREF(EObject obj)
Description copied from interface: XMLHelper
Returns an IDREF to this object

Specified by:
getIDREF in interface XMLHelper

handleDanglingHREF

protected URI handleDanglingHREF(EObject object)

getHREF

public String getHREF(EObject obj)
Description copied from interface: XMLHelper
Returns an HREF to this object from this resource

Specified by:
getHREF in interface XMLHelper

getHREF

protected URI getHREF(Resource otherResource,
                      EObject obj)

deresolve

public URI deresolve(URI uri)
Description copied from interface: XMLHelper
Returns a relative URI if necessary and if possible.

Specified by:
deresolve in interface XMLHelper

getFeatureKind

public int getFeatureKind(EStructuralFeature feature)
Description copied from interface: XMLHelper
Return the kind of feature.

Specified by:
getFeatureKind in interface XMLHelper

createObject

public EObject createObject(EFactory eFactory,
                            EClassifier type)
Description copied from interface: XMLHelper
Create an object given an EFactory and a type

Specified by:
createObject in interface XMLHelper
Parameters:
eFactory -
type -
Returns:
See Also:
XMLHelper.createObject(EFactory, String)

getType

public EClassifier getType(EFactory eFactory,
                           String typeName)
Description copied from interface: XMLHelper
Given an EFactory and a type name, find and return the type

Specified by:
getType in interface XMLHelper
Parameters:
eFactory -
typeName -
See Also:
XMLHelper.createObject(EFactory, String)

createObject

public EObject createObject(EFactory eFactory,
                            String classXMIName)
Deprecated. since 2.2

Description copied from interface: XMLHelper
Create an object given an EFactory and a type name.

Specified by:
createObject in interface XMLHelper

getFeature

public EStructuralFeature getFeature(EClass eClass,
                                     String namespaceURI,
                                     String name)
Description copied from interface: XMLHelper
Returns the structural feature for the XML element/attribute with the given namespaceURI and name.

Specified by:
getFeature in interface XMLHelper

getFeature

public EStructuralFeature getFeature(EClass eClass,
                                     String namespaceURI,
                                     String name,
                                     boolean isElement)
Description copied from interface: XMLHelper
Returns the structural feature for the XML element/attribute with the given namespaceURI and name.

Specified by:
getFeature in interface XMLHelper

getFeatureWithoutMap

protected EStructuralFeature getFeatureWithoutMap(EClass eClass,
                                                  String name)

computeFeatureKind

protected void computeFeatureKind(EStructuralFeature feature)

getJavaEncoding

public String getJavaEncoding(String xmlEncoding)
Description copied from interface: XMLHelper
Return a Java encoding corresponding to the given XML encoding. By default, the XML encoding is returned.

Specified by:
getJavaEncoding in interface XMLHelper

getXMLEncoding

public String getXMLEncoding(String javaEncoding)
Description copied from interface: XMLHelper
Return an XML encoding corresponding to the given Java encoding. By default, the Java encoding is returned.

Specified by:
getXMLEncoding in interface XMLHelper

packages

public EPackage[] packages()
Description copied from interface: XMLHelper
Returns the packages in getQName()

Specified by:
packages in interface XMLHelper

setValue

public void setValue(EObject object,
                     EStructuralFeature feature,
                     Object value,
                     int position)
Description copied from interface: XMLHelper
Set the value of the feature for the object.

Specified by:
setValue in interface XMLHelper

setManyReference

public List setManyReference(XMLHelper.ManyReference reference,
                             String location)
Specified by:
setManyReference in interface XMLHelper

setCheckForDuplicates

public void setCheckForDuplicates(boolean checkForDuplicates)
Specified by:
setCheckForDuplicates in interface XMLHelper

setProcessDanglingHREF

public void setProcessDanglingHREF(String value)
Specified by:
setProcessDanglingHREF in interface XMLHelper

getDanglingHREFException

public DanglingHREFException getDanglingHREFException()
Specified by:
getDanglingHREFException in interface XMLHelper

resolve

public URI resolve(URI relative,
                   URI base)
Specified by:
resolve in interface XMLHelper

pushContext

public void pushContext()
Description copied from interface: XMLHelper
Starts a new namespace context while loading an XML document.

A new context should be pushed at the beginning of each XML element. The new context will automatically inherit the declarations of its parent context, but it will also keep track of which declarations were made within this context.

Specified by:
pushContext in interface XMLHelper

popContext

public void popContext()
Description copied from interface: XMLHelper
Reverts to the previous namespace context while loading XML document.

The context should be popped at the end of each XML element. After popping the context, all namespace prefix mappings that were previously in scope are restored.

Specified by:
popContext in interface XMLHelper

popContext

public void popContext(Map prefixesToFactories)
Description copied from interface: XMLHelper
Reverts to the previous namespace context while loading XML document and removes from the map, prefixes that have gone out of scope.

The context should be popped at the end of each XML element. After popping the context, all namespace prefix mappings that were previously in scope are restored.

Specified by:
popContext in interface XMLHelper

addPrefix

public void addPrefix(String prefix,
                      String uri)
Description copied from interface: XMLHelper
Records the prefix to URI mapping while loading an XML document.

Specified by:
addPrefix in interface XMLHelper

getPrefix

public String getPrefix(String namespaceURI)
Specified by:
getPrefix in interface XMLHelper

getAnyContentPrefixToURIMapping

public Map getAnyContentPrefixToURIMapping()
Description copied from interface: XMLHelper
Returns a map of prefixes to URI mapping for elements with any content

Specified by:
getAnyContentPrefixToURIMapping in interface XMLHelper

getURI

public String getURI(String prefix)
Description copied from interface: XMLHelper
Returns the namespace prefix that's in effect while loading an XML document.

Specified by:
getURI in interface XMLHelper

getPrefixToNamespaceMap

public EMap getPrefixToNamespaceMap()
Specified by:
getPrefixToNamespaceMap in interface XMLHelper

recordPrefixToURIMapping

public void recordPrefixToURIMapping()
Description copied from interface: XMLHelper
Must be called during endDocument to record all the prefix to URI mappings that have been seen in the document during loading

Specified by:
recordPrefixToURIMapping in interface XMLHelper

setPrefixToNamespaceMap

public void setPrefixToNamespaceMap(EMap prefixToNamespaceMap)
Specified by:
setPrefixToNamespaceMap in interface XMLHelper

setAnySimpleType

public void setAnySimpleType(EClass type)
Description copied from interface: XMLHelper
Sets the any simple type class.

Specified by:
setAnySimpleType in interface XMLHelper
See Also:
XMLResource.OPTION_ANY_SIMPLE_TYPE

convertToString

public String convertToString(EFactory factory,
                              EDataType dataType,
                              Object value)
Description copied from interface: XMLHelper
Converts the given valuevalue to string while saving an XML document.

Specified by:
convertToString in interface XMLHelper

createFromString

protected Object createFromString(EFactory eFactory,
                                  EDataType eDataType,
                                  String value)

updateQNameURI

protected void updateQNameURI(Object value)

updateQNamePrefix

protected String updateQNamePrefix(EFactory factory,
                                   EDataType dataType,
                                   Object value,
                                   boolean list)
Parameters:
factory -
dataType -
value - a data value to be converted to string
list - if the value is part of the list of values
Returns:
if the value is not part of the list, return string corresponding to value, otherwise return null

addNSDeclaration

protected void addNSDeclaration(String prefix,
                                String uri)

setMustHavePrefix

public void setMustHavePrefix(boolean mustHavePrefix)
Description copied from interface: XMLHelper
Specify if qualified names must have a prefix or could use default namespace (if possible)

Specified by:
setMustHavePrefix in interface XMLHelper
Parameters:
mustHavePrefix -

Copyright 2001-2006 IBM Corporation and others.
All Rights Reserved.