Class XmiCasDeserializer.XmiCasDeserializerHandler

java.lang.Object
org.xml.sax.helpers.DefaultHandler
org.apache.uima.cas.impl.XmiCasDeserializer.XmiCasDeserializerHandler
All Implemented Interfaces:
ContentHandler, DTDHandler, EntityResolver, ErrorHandler
Enclosing class:
XmiCasDeserializer

public class XmiCasDeserializer.XmiCasDeserializerHandler extends DefaultHandler
  • Field Details

    • DOC_STATE

      private static final int DOC_STATE
      See Also:
    • FS_STATE

      private static final int FS_STATE
      See Also:
    • FEAT_STATE

      private static final int FEAT_STATE
      See Also:
    • FEAT_CONTENT_STATE

      private static final int FEAT_CONTENT_STATE
      See Also:
    • IGNORING_XMI_ELEMENTS_STATE

      private static final int IGNORING_XMI_ELEMENTS_STATE
      See Also:
    • REF_FEAT_STATE

      private static final int REF_FEAT_STATE
      See Also:
    • unknownXMLSource

      private static final String unknownXMLSource
      See Also:
    • locator

      private Locator locator
    • casBeingFilled

      private CASImpl casBeingFilled
    • state

      private int state
      Next expected content Values: (*) means valid state for "startElement" * DOC_STATE - start of the XML document * FS_STATE - start of a Feature Structure, or document text element, or the end of the XML input. * FEAT_STATE - features encoded as sub-elements, or end of the Feature Structure FEAT_CONTENT_STATE - the feature value for a feature called via "characters" callback accumulates chars to "buffer" * IGNORING_XMI_ELEMENTS_STATE - REF_FEAT_STATE - inside invalid input: '<'feat href= "#1"). expect only end of element
    • buffer

      private StringBuilder buffer
    • currentFs

      private TOP currentFs
    • currentType

      private TypeImpl currentType
    • currentArrayId

      private int currentArrayId
    • currentArrayElements

      private List<String> currentArrayElements
    • multiValuedFeatures

      private Map<String,ArrayList<String>> multiValuedFeatures
    • indexRepositories

      private List<FSIndexRepository> indexRepositories
    • views

      private List<CAS> views
    • lenient

      boolean lenient
    • ignoreDepth

      private int ignoreDepth
    • nsPrefixToUriMap

      private Map<String,String> nsPrefixToUriMap
    • sharedData

      private XmiSerializationSharedData sharedData
    • nextSofaNum

      private int nextSofaNum
    • mergePoint

      private int mergePoint
    • outOfTypeSystemElement

      private XmiSerializationSharedData.OotsElementData outOfTypeSystemElement
    • deferredFSs

      This is a list of deferred FSs to be processed. It is added to when a FS which is
      • a subtype of AnnotationBase
      • has its Sofa ref to some XmiId which is unknown at the time
      is encountered. These FSs are processed later, at the end of FS processing, when all Sofas have been deserialized. It is not out-of-type-system data, but is reusing that data structure - sorry for the confusion This element is set non-null only when this mode is enabled.
    • deferredFsElement

      private XmiSerializationSharedData.OotsElementData deferredFsElement
    • processingDeferredFSs

      private boolean processingDeferredFSs
      normally false; set true when processing deferred FSs
    • isDoingDeferredChildElements

      private boolean isDoingDeferredChildElements
      normally false, set true when processing deferred FSs child elements
    • localXmiIdToFs

      private Map<Integer,TOP> localXmiIdToFs
      local map from xmi:id to FS address, used when merging multiple XMI CASes into one CAS object.
    • allowPreexistingFS

      AllowPreexistingFS allowPreexistingFS
    • featsSeen

      IntVector featsSeen
    • disallowedViewMemberEncountered

      boolean disallowedViewMemberEncountered
    • toBeAdded

      private final DeferredIndexUpdates toBeAdded
      a list by view of FSs to be added to the indexes
    • toBeRemoved

      private final DeferredIndexUpdates toBeRemoved
      a list by view of FSs to be removed from the indexes
    • fixupToDos

      private final List<Runnable_withSaxException> fixupToDos
      Deferred Set of feature value assignments to do after all FSs are deserialized,
    • uimaSerializableFixups

      private final List<Runnable> uimaSerializableFixups
  • Constructor Details

    • XmiCasDeserializerHandler

      private XmiCasDeserializerHandler(CASImpl aCAS, boolean lenient, XmiSerializationSharedData sharedData, int mergePoint, AllowPreexistingFS allowPreexistingFS)
      Creates a SAX handler used for deserializing an XMI CAS.
      Parameters:
      aCAS - CAS to deserialize into
      lenient - if true, unknown types/features result in an exception. If false, unknown types/features are ignored.
      sharedData - data structure used to allow the XmiCasSerializer and XmiCasDeserializer to share information.
      mergePoint - used to support merging multiple XMI CASes. If the mergePoint is negative, "normal" deserialization will be done, meaning the target CAS will be reset and the entire XMI content will be deserialized. If the mergePoint is nonnegative (including 0), the target CAS will not be reset, and only Feature Structures whose xmi:id is strictly greater than the mergePoint value will be deserialized.
  • Method Details

    • resetBuffer

      private final void resetBuffer()
    • startDocument

      public void startDocument() throws SAXException
      Specified by:
      startDocument in interface ContentHandler
      Overrides:
      startDocument in class DefaultHandler
      Throws:
      SAXException
    • startElement

      public void startElement(String nameSpaceURI, String localName, String qualifiedName, Attributes attrs) throws SAXException
      Specified by:
      startElement in interface ContentHandler
      Overrides:
      startElement in class DefaultHandler
      Throws:
      SAXException
    • readFS

      private void readFS(String nameSpaceURI, String localName, String qualifiedName, Attributes attrs) throws SAXException
      Read one FS, create a new FS or update an existing one
      Parameters:
      nameSpaceURI - -
      localName - -
      qualifiedName - -
      attrs - -
      Throws:
      SAXException - -
    • doDeferFsOrThrow

      private void doDeferFsOrThrow(String idStr, String nameSpaceURI, String localName, String qualifiedName, Attributes attrs) throws XCASParsingException
      Throws:
      XCASParsingException
    • processView

      private void processView(String sofa, String membersString) throws SAXParseException
      Handles the processing of a cas:View element in the XMI. The cas:View element encodes indexed FSs.
      Parameters:
      sofa - xmi:id of the sofa for this view, null indicates base CAS "view"
      membersString - whitespace-separated string of FS addresses. Each FS is to be added to the specified sofa's index repository The adding takes place after FSs are finalized, to enable checking the sofa refs are OK https://issues.apache.org/jira/browse/UIMA-4099
      Throws:
      SAXParseException
    • getIndexRepo

      private FSIndexRepositoryImpl getIndexRepo(String sofaXmiIdAsString, int sofaXmiId) throws XCASParsingException
      Parameters:
      sofaXmiIdAsString - xmiId
      sofaNum - 1 if sofa null, or the sofa Xmi Id
      Returns:
      the FS Repository associated with the sofa xmiId
      Throws:
      XCASParsingException
    • processView

      private void processView(String sofa, String addmembersString, String delmemberString, String reindexmemberString) throws SAXParseException
      Handles the processing of a cas:View element in the XMI. The cas:View element encodes indexed FSs.
      Parameters:
      sofa - xmi:id of the sofa for this view, null indicates base CAS "view"
      membersString - whitespace-separated string of FS addresses. Each FS is to be added to the specified sofa's index repository
      Throws:
      SAXParseException
    • readFS

      private void readFS(TOP fs, Attributes attrs, boolean isNewFs) throws SAXException
      Throws:
      SAXException
    • emptyVal

      private final boolean emptyVal(String val)
    • handleFeatureFromName

      private int handleFeatureFromName(TypeImpl type, TOP fs, String featName, String featVal, boolean isNewFS) throws SAXException
      Deserialize one feature called from readFS 751 called from processDeferred, to handle features specified as child elements
      Parameters:
      type - -
      fs - the FS
      featName - the feature name
      featVal - the value of the feature
      isNewFS - true if this is a new FS
      Returns:
      feature code or -1 if no feature in this type system
      Throws:
      SAXException - if Type doesn't have the feature, and we're not in lenient mode
    • handleFeatMultiValueFromName

      private int handleFeatMultiValueFromName(Type type, TOP fs, String featName, ArrayList<String> featVals) throws SAXException
      called from endElement after collecting non-byte array element instances into a string list for a particular array or list feature (excluding oots and deferred FSs)
      Parameters:
      type - -
      fs - -
      featName - -
      featVals - -
      Returns:
      the feature code of the featName arg, or -1 if feature not found (oots) and lenient
      Throws:
      SAXException
    • handleFeatSingleValue

      private void handleFeatSingleValue(TOP fs, FeatureImpl fi, String featVal) throws SAXException
      Throws:
      SAXException
    • deserializeFsRef

      private void deserializeFsRef(String featVal, FeatureImpl fi, TOP fs)
    • parseArray

      private String[] parseArray(String val)
      Parse an XMI multi-valued attribute into a String array, by splitting on whitespace.
      Parameters:
      val - XMI attribute value
      Returns:
      an array with each array value as an element
    • handleFeatMultiValue

      private void handleFeatMultiValue(TOP fs, FeatureImpl fi, List<String> featVals) throws SAXException
      Throws:
      SAXException
    • createOrUpdateList

      private CommonList createOrUpdateList(TypeImpl listType, List<String> values, int xmiId, CommonList existingList)
      Called only for non-shared lists where all the list items serialized with the feature
      Parameters:
      listType - -
      values - - guaranteed to have at least one.
      existingList - - the existing list head ref
      Returns:
      the existingList or a new list
    • createOrUpdateArray

      private CommonArrayFS createOrUpdateArray(TypeImpl arrayType, List<String> values, int xmiId, CommonArrayFS existingArray) throws XCASParsingException
      Create or update an array in the CAS If the array is an FSArray, and the elements are not yet deserialized, a lambda expression is put on a "todo" list to be executed after all the FSs are deserialized, to set the value later.
      Parameters:
      arrayType - CAS type for the array
      values - List of strings, each representing an element in the array
      xmiId - xmi:id assigned to the array object.
      existingArray - preexisting non-shared array when processing a Delta CAS.
      Returns:
      the new or updated-existing FS for the array
      Throws:
      XCASParsingException
    • createNewArray

      private CommonArrayFS createNewArray(TypeImpl type, List<String> values)
      Create an array in the CAS.
      Parameters:
      values - List of strings, each containing the value of an element of the array.
      arrayType - CAS type code for the array
      Returns:
      a reference to the array FS
    • updateExistingArray

      private void updateExistingArray(List<String> values, CommonArrayFS existingArray)
      Update existing array. The size has already been checked to be equal, but could be 0
      Parameters:
      values -
      existingArray -
      arrayType -
    • updateExistingList

      private CommonList updateExistingList(List<String> values, CommonList existingList)
      existingList guaranteed non-null, but could be EmptyList instance values could be null or empty Return the existing list or a replacement for it which might be an emptylist
    • maybeSetFsArrayElement

      private void maybeSetFsArrayElement(List<String> values, int i, FSArray fsArray)
    • maybeSetFsListHead

      private void maybeSetFsListHead(String featVal, NonEmptyFSList neNode)
    • createListFromStringValues

      CommonList createListFromStringValues(List<String> stringValues, EmptyList emptyNode)
    • createListFromStringValues

      private CommonList createListFromStringValues(List<String> stringValues, int startPos, EmptyList emptyNode)
      There are two variants - one for primitives, and one for values which are refs to FS. For both variants, the items in the list are always single-reachable from their previous nodes; that is, there are no loops or other sharing of nodes. If there were, this would not be serialized as a simple list of values. For the refs to FS case, the head references cannot be to any node in the list except the 1st one. This is because the other nodes have no xmiIds in the serialized representation. If some reference was to this, then the list would be serialized in the alternate format. For head references to Feature Structures not known (not yet serialized, or out-of-type-system), a null value is used, and a fixupToDos entry is added to fix this up after all Feature Structures have been serialized. (same as FSArray deserialization)
      Parameters:
      stringValues - - the primitive values in string representation, or the xmiIds for references
      startPos - - where in the list of values to start (in case we're adding to an existing list)
      emptyNode - - the last node in the list, the empty node that terminates it
      Returns:
      the first node in the list of nodes (could be the empty node for an empty list)
    • createOrUpdateByteArray

      private ByteArray createOrUpdateByteArray(String hexString, int xmiId, ByteArray existingArray) throws XCASParsingException
      Create a byte array in the CAS. Treatment of null and empty. The existing value can be null. - if not null, it has a size(), which may be 0. The incoming values may be: - null - a 0 length string - a string of some length (must be even number of bytes, else an error) If the incoming value is null - return null If the incoming value is 0 length string - return a 0 length ByteArray. If the incoming value's length matches the length of the existing ByteArray, - replace the value. If the incoming value's length doesn't match, return a new ByteArray. Cases: definitions: - non-shared / shared : "non-shared" - meaning it is encoded at the spot of the feature in the serialization - fs already exists, meaning we're updating the array value (delta cas update below the line, by matching xmi:id's ) case 1: the existingArray is null, or the incoming array is null or 0-length string - the incoming array replaces the existing value (if any), including setting it to null. case 1: values are encoded in place at feature reference spot (implies non-shared) - fs exists if lengths the same, update the existing byte array, else throw exception - can't update size of existing one - fs is new make a new one case 2: values are encoded as a separate feature structure - fs exists if lengths the same, update the existing byte array, else throw exception - can't update size of existing one - fs is new make a new one
      Parameters:
      hexString - value of the byte array as a hex string
      xmiId - xmiId - this will be -1 if this is a non-shared (encoded locally with the Feature) byte array FS.
      existingFs - the existing ByteArrayFS used when processing a Delta CAS.
      Returns:
      the new or updated-existing FS for the array
      Throws:
      XCASParsingException
    • hexCharToByte

      private byte hexCharToByte(char c)
    • characters

      public void characters(char[] chars, int start, int length) throws SAXException
      Specified by:
      characters in interface ContentHandler
      Overrides:
      characters in class DefaultHandler
      Throws:
      SAXException
    • endElement

      public void endElement(String nsURI, String localName, String qualifiedName) throws SAXException
      Specified by:
      endElement in interface ContentHandler
      Overrides:
      endElement in class DefaultHandler
      Throws:
      SAXException
    • endDocument

      public void endDocument() throws SAXException
      Specified by:
      endDocument in interface ContentHandler
      Overrides:
      endDocument in class DefaultHandler
      Throws:
      SAXException
    • finalizeRefValue

      private void finalizeRefValue(int xmiId, TOP fs, FeatureImpl fi) throws XCASParsingException
      Throws:
      XCASParsingException
    • finalizeFSListRefValue

      private void finalizeFSListRefValue(int xmiId, NonEmptyFSList neNode) throws XCASParsingException
      Throws:
      XCASParsingException
    • finalizeFSArrayRefValue

      private void finalizeFSArrayRefValue(int xmiId, FSArray fsArray, int index) throws XCASParsingException
      Throws:
      XCASParsingException
    • createException

      private XCASParsingException createException(int code)
    • createException

      private XCASParsingException createException(int code, String arg)
    • error

      public void error(SAXParseException e) throws SAXException
      Specified by:
      error in interface ErrorHandler
      Overrides:
      error in class DefaultHandler
      Throws:
      SAXException
    • fatalError

      public void fatalError(SAXParseException e) throws SAXException
      Specified by:
      fatalError in interface ErrorHandler
      Overrides:
      fatalError in class DefaultHandler
      Throws:
      SAXException
    • ignorableWhitespace

      public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException
      Specified by:
      ignorableWhitespace in interface ContentHandler
      Overrides:
      ignorableWhitespace in class DefaultHandler
      Throws:
      SAXException
    • setDocumentLocator

      public void setDocumentLocator(Locator loc)
      Specified by:
      setDocumentLocator in interface ContentHandler
      Overrides:
      setDocumentLocator in class DefaultHandler
    • warning

      public void warning(SAXParseException e) throws SAXException
      Specified by:
      warning in interface ErrorHandler
      Overrides:
      warning in class DefaultHandler
      Throws:
      SAXException
    • addFsToXmiId

      private void addFsToXmiId(TOP fs, int xmiId)
    • getFsForXmiId

      private TOP getFsForXmiId(int xmiId)
    • maybeGetFsForXmiId

      private TOP maybeGetFsForXmiId(int xmiId)
    • addToOutOfTypeSystemData

      private void addToOutOfTypeSystemData(XmlElementName xmlElementName, Attributes attrs) throws XCASParsingException
      Throws:
      XCASParsingException
    • isNewFS

      private boolean isNewFS(int id)
    • addNonsharedFSToEncompassingFSMapping

      private void addNonsharedFSToEncompassingFSMapping(TOP nonsharedFS, TOP encompassingFS)
    • processDeferredFSs

      private void processDeferredFSs() throws SAXException
      Throws:
      SAXException