Class XmiCasDeserializer.XmiCasDeserializerHandler

  • All Implemented Interfaces:
    org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler
    Enclosing class:
    XmiCasDeserializer

    public class XmiCasDeserializer.XmiCasDeserializerHandler
    extends org.xml.sax.helpers.DefaultHandler
    • Field Detail

      • IGNORING_XMI_ELEMENTS_STATE

        private static final int IGNORING_XMI_ELEMENTS_STATE
        See Also:
        Constant Field Values
      • unknownXMLSource

        private static final java.lang.String unknownXMLSource
        See Also:
        Constant Field Values
      • locator

        private org.xml.sax.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
      • buffer

        private java.lang.StringBuilder buffer
      • currentFs

        private TOP currentFs
      • currentType

        private TypeImpl currentType
      • currentArrayId

        private int currentArrayId
      • currentArrayElements

        private java.util.List<java.lang.String> currentArrayElements
      • multiValuedFeatures

        private java.util.Map<java.lang.String,​java.util.ArrayList<java.lang.String>> multiValuedFeatures
      • views

        private java.util.List<CAS> views
      • lenient

        boolean lenient
      • ignoreDepth

        private int ignoreDepth
      • nsPrefixToUriMap

        private java.util.Map<java.lang.String,​java.lang.String> nsPrefixToUriMap
      • nextSofaNum

        private int nextSofaNum
      • mergePoint

        private int mergePoint
      • deferredFSs

        private java.util.List<XmiSerializationSharedData.OotsElementData> 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.
      • 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 java.util.Map<java.lang.Integer,​TOP> localXmiIdToFs
        local map from xmi:id to FS address, used when merging multiple XMI CASes into one CAS object.
      • 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 java.util.List<Runnable_withSaxException> fixupToDos
        Deferred Set of feature value assignments to do after all FSs are deserialized,
      • uimaSerializableFixups

        private final java.util.List<java.lang.Runnable> uimaSerializableFixups
    • Constructor Detail

      • 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 Detail

      • resetBuffer

        private final void resetBuffer()
      • startDocument

        public void startDocument()
                           throws org.xml.sax.SAXException
        Specified by:
        startDocument in interface org.xml.sax.ContentHandler
        Overrides:
        startDocument in class org.xml.sax.helpers.DefaultHandler
        Throws:
        org.xml.sax.SAXException
      • startElement

        public void startElement​(java.lang.String nameSpaceURI,
                                 java.lang.String localName,
                                 java.lang.String qualifiedName,
                                 org.xml.sax.Attributes attrs)
                          throws org.xml.sax.SAXException
        Specified by:
        startElement in interface org.xml.sax.ContentHandler
        Overrides:
        startElement in class org.xml.sax.helpers.DefaultHandler
        Throws:
        org.xml.sax.SAXException
      • readFS

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

        private void doDeferFsOrThrow​(java.lang.String idStr,
                                      java.lang.String nameSpaceURI,
                                      java.lang.String localName,
                                      java.lang.String qualifiedName,
                                      org.xml.sax.Attributes attrs)
                               throws XCASParsingException
        Throws:
        XCASParsingException
      • processView

        private void processView​(java.lang.String sofa,
                                 java.lang.String membersString)
                          throws org.xml.sax.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:
        org.xml.sax.SAXParseException
      • getIndexRepo

        private FSIndexRepositoryImpl getIndexRepo​(java.lang.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​(java.lang.String sofa,
                                 java.lang.String addmembersString,
                                 java.lang.String delmemberString,
                                 java.lang.String reindexmemberString)
                          throws org.xml.sax.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:
        org.xml.sax.SAXParseException
      • readFS

        private void readFS​(TOP fs,
                            org.xml.sax.Attributes attrs,
                            boolean isNewFs)
                     throws org.xml.sax.SAXException
        Throws:
        org.xml.sax.SAXException
      • emptyVal

        private final boolean emptyVal​(java.lang.String val)
      • handleFeatureFromName

        private int handleFeatureFromName​(TypeImpl type,
                                          TOP fs,
                                          java.lang.String featName,
                                          java.lang.String featVal,
                                          boolean isNewFS)
                                   throws org.xml.sax.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:
        org.xml.sax.SAXException - if Type doesn't have the feature, and we're not in lenient mode
      • handleFeatMultiValueFromName

        private int handleFeatMultiValueFromName​(Type type,
                                                 TOP fs,
                                                 java.lang.String featName,
                                                 java.util.ArrayList<java.lang.String> featVals)
                                          throws org.xml.sax.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:
        org.xml.sax.SAXException
      • handleFeatSingleValue

        private void handleFeatSingleValue​(TOP fs,
                                           FeatureImpl fi,
                                           java.lang.String featVal)
                                    throws org.xml.sax.SAXException
        Throws:
        org.xml.sax.SAXException
      • deserializeFsRef

        private void deserializeFsRef​(java.lang.String featVal,
                                      FeatureImpl fi,
                                      TOP fs)
      • parseArray

        private java.lang.String[] parseArray​(java.lang.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,
                                          java.util.List<java.lang.String> featVals)
                                   throws org.xml.sax.SAXException
        Throws:
        org.xml.sax.SAXException
      • createOrUpdateList

        private CommonList createOrUpdateList​(TypeImpl listType,
                                              java.util.List<java.lang.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,
                                                  java.util.List<java.lang.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,
                                             java.util.List<java.lang.String> values)
        Create an array in the CAS.
        Parameters:
        arrayType - CAS type code for the array
        values - List of strings, each containing the value of an element of the array.
        Returns:
        a reference to the array FS
      • updateExistingArray

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

        private CommonList updateExistingList​(java.util.List<java.lang.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​(java.util.List<java.lang.String> values,
                                            int i,
                                            FSArray fsArray)
      • maybeSetFsListHead

        private void maybeSetFsListHead​(java.lang.String featVal,
                                        NonEmptyFSList neNode)
      • createListFromStringValues

        CommonList createListFromStringValues​(java.util.List<java.lang.String> stringValues,
                                              EmptyList emptyNode)
      • createListFromStringValues

        private CommonList createListFromStringValues​(java.util.List<java.lang.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​(java.lang.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 org.xml.sax.SAXException
        Specified by:
        characters in interface org.xml.sax.ContentHandler
        Overrides:
        characters in class org.xml.sax.helpers.DefaultHandler
        Throws:
        org.xml.sax.SAXException
      • endElement

        public void endElement​(java.lang.String nsURI,
                               java.lang.String localName,
                               java.lang.String qualifiedName)
                        throws org.xml.sax.SAXException
        Specified by:
        endElement in interface org.xml.sax.ContentHandler
        Overrides:
        endElement in class org.xml.sax.helpers.DefaultHandler
        Throws:
        org.xml.sax.SAXException
      • endDocument

        public void endDocument()
                         throws org.xml.sax.SAXException
        Specified by:
        endDocument in interface org.xml.sax.ContentHandler
        Overrides:
        endDocument in class org.xml.sax.helpers.DefaultHandler
        Throws:
        org.xml.sax.SAXException
      • createException

        private XCASParsingException createException​(int code,
                                                     java.lang.String arg)
      • error

        public void error​(org.xml.sax.SAXParseException e)
                   throws org.xml.sax.SAXException
        Specified by:
        error in interface org.xml.sax.ErrorHandler
        Overrides:
        error in class org.xml.sax.helpers.DefaultHandler
        Throws:
        org.xml.sax.SAXException
      • fatalError

        public void fatalError​(org.xml.sax.SAXParseException e)
                        throws org.xml.sax.SAXException
        Specified by:
        fatalError in interface org.xml.sax.ErrorHandler
        Overrides:
        fatalError in class org.xml.sax.helpers.DefaultHandler
        Throws:
        org.xml.sax.SAXException
      • ignorableWhitespace

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

        public void setDocumentLocator​(org.xml.sax.Locator loc)
        Specified by:
        setDocumentLocator in interface org.xml.sax.ContentHandler
        Overrides:
        setDocumentLocator in class org.xml.sax.helpers.DefaultHandler
      • warning

        public void warning​(org.xml.sax.SAXParseException e)
                     throws org.xml.sax.SAXException
        Specified by:
        warning in interface org.xml.sax.ErrorHandler
        Overrides:
        warning in class org.xml.sax.helpers.DefaultHandler
        Throws:
        org.xml.sax.SAXException
      • addFsToXmiId

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

        private TOP getFsForXmiId​(int xmiId)
      • maybeGetFsForXmiId

        private TOP maybeGetFsForXmiId​(int xmiId)
      • isNewFS

        private boolean isNewFS​(int id)
      • addNonsharedFSToEncompassingFSMapping

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

        private void processDeferredFSs()
                                 throws org.xml.sax.SAXException
        Throws:
        org.xml.sax.SAXException