예제 #1
0
        /// <summary> Performs a clone of all {@link AttributeImpl} instances returned in a new
        /// AttributeSource instance. This method can be used to e.g. create another TokenStream
        /// with exactly the same attributes (using {@link #AttributeSource(AttributeSource)})
        /// </summary>
        public virtual AttributeSource CloneAttributes()
        {
            AttributeSource clone = new AttributeSource(this.factory);

            // first clone the impls
            if (HasAttributes())
            {
                for (State state = GetCurrentState(); state != null; state = state.next)
                {
                    AttributeImpl impl = (AttributeImpl)state.attribute.Clone();

                    if (!clone.attributeImpls.ContainsKey(impl.GetType()))
                    {
                        clone.attributeImpls.Add(new Support.AttributeImplItem(impl.GetType(), impl));
                    }
                }
            }

            // now the interfaces
            foreach (Support.AttributeImplItem att in this.attributes)
            {
                clone.attributes.Add(new Support.AttributeImplItem(att.Key, clone.attributeImpls[att.Value.GetType()].Value));
            }

            return(clone);
        }
        public SingleTokenTokenStream(Token token)
        {
            Debug.Assert(token != null, "Token was null!");
            _singleToken = (Token) token.Clone();

            // ReSharper disable DoNotCallOverridableMethodsInConstructor
            _tokenAtt = (AttributeImpl) AddAttribute(typeof (TermAttribute));
            // ReSharper restore DoNotCallOverridableMethodsInConstructor

            Debug.Assert(_tokenAtt is Token || _tokenAtt.GetType().Name.Equals(typeof (TokenWrapper).Name),
                         "Token Attribute is the wrong type! Type was: " + _tokenAtt.GetType().Name + " but expected " +
                         typeof (TokenWrapper).Name);
        }
예제 #3
0
        // {{Aroush-2.9 Port issue, need to mimic java's IdentityHashMap

        /*
         * From Java docs:
         * This class implements the Map interface with a hash table, using
         * reference-equality in place of object-equality when comparing keys
         * (and values). In other words, in an IdentityHashMap, two keys k1 and k2
         * are considered equal if and only if (k1==k2). (In normal Map
         * implementations (like HashMap) two keys k1 and k2 are considered
         * equal if and only if (k1==null ? k2==null : k1.equals(k2)).)
         */
        // Aroush-2.9}}

        /// <summary>Adds a custom AttributeImpl instance with one or more Attribute interfaces. </summary>
        public virtual void  AddAttributeImpl(AttributeImpl att)
        {
            System.Type clazz = att.GetType();
            if (attributeImpls.Contains(clazz))
            {
                return;
            }
            System.Collections.ArrayList foundInterfaces;
            lock (knownImplClasses)
            {
                foundInterfaces = (System.Collections.ArrayList)knownImplClasses[clazz];
                if (foundInterfaces == null)
                {
                    // we have a strong reference to the class instance holding all interfaces in the list (parameter "att"),
                    // so all WeakReferences are never evicted by GC
                    knownImplClasses.Add(clazz, foundInterfaces = new System.Collections.ArrayList());
                    // find all interfaces that this attribute instance implements
                    // and that extend the Attribute interface
                    System.Type actClazz = clazz;
                    do
                    {
                        System.Type[] interfaces = actClazz.GetInterfaces();
                        for (int i = 0; i < interfaces.Length; i++)
                        {
                            System.Type curInterface = interfaces[i];
                            if (curInterface != typeof(Attribute) && typeof(Attribute).IsAssignableFrom(curInterface))
                            {
                                foundInterfaces.Add(new WeakReference(curInterface));
                            }
                        }
                        actClazz = actClazz.BaseType;
                    }while (actClazz != null);
                }
            }

            // add all interfaces of this AttributeImpl to the maps
            for (System.Collections.IEnumerator it = foundInterfaces.GetEnumerator(); it.MoveNext();)
            {
                WeakReference curInterfaceRef = (WeakReference)it.Current;
                System.Type   curInterface    = (System.Type)curInterfaceRef.Target;
                System.Diagnostics.Debug.Assert(curInterface != null, "We have a strong reference on the class holding the interfaces, so they should never get evicted");
                // Attribute is a superclass of this interface
                if (!attributes.ContainsKey(curInterface))
                {
                    // invalidate state to force recomputation in captureState()
                    this.currentState = null;
                    attributes.Add(new SupportClass.AttributeImplItem(curInterface, att));
                    if (!attributeImpls.ContainsKey(clazz))
                    {
                        attributeImpls.Add(new SupportClass.AttributeImplItem(clazz, att));
                    }
                }
            }
        }
예제 #4
0
        // {{Aroush-2.9 Port issue, need to mimic java's IdentityHashMap
        /*
         * From Java docs:
         * This class implements the Map interface with a hash table, using 
         * reference-equality in place of object-equality when comparing keys 
         * (and values). In other words, in an IdentityHashMap, two keys k1 and k2 
         * are considered equal if and only if (k1==k2). (In normal Map 
         * implementations (like HashMap) two keys k1 and k2 are considered 
         * equal if and only if (k1==null ? k2==null : k1.equals(k2)).) 
         */
        // Aroush-2.9}}
		
		/// <summary>Adds a custom AttributeImpl instance with one or more Attribute interfaces. </summary>
		public virtual void  AddAttributeImpl(AttributeImpl att)
		{
			System.Type clazz = att.GetType();
			if (attributeImpls.Contains(clazz))
				return ;
			System.Collections.ArrayList foundInterfaces;
			lock (knownImplClasses)
			{
				foundInterfaces = (System.Collections.ArrayList) knownImplClasses[clazz];
				if (foundInterfaces == null)
				{
                    // we have a strong reference to the class instance holding all interfaces in the list (parameter "att"),
                    // so all WeakReferences are never evicted by GC
					knownImplClasses.Add(clazz, foundInterfaces = new System.Collections.ArrayList());
					// find all interfaces that this attribute instance implements
					// and that extend the Attribute interface
					System.Type actClazz = clazz;
					do 
					{
						System.Type[] interfaces = actClazz.GetInterfaces();
						for (int i = 0; i < interfaces.Length; i++)
						{
							System.Type curInterface = interfaces[i];
							if (curInterface != typeof(Attribute) && typeof(Attribute).IsAssignableFrom(curInterface))
							{
								foundInterfaces.Add(new WeakReference(curInterface));
							}
						}
						actClazz = actClazz.BaseType;
					}
					while (actClazz != null);
				}
			}
			
			// add all interfaces of this AttributeImpl to the maps
			for (System.Collections.IEnumerator it = foundInterfaces.GetEnumerator(); it.MoveNext(); )
			{
                WeakReference curInterfaceRef = (WeakReference)it.Current;
				System.Type curInterface = (System.Type) curInterfaceRef.Target;
                System.Diagnostics.Debug.Assert(curInterface != null,"We have a strong reference on the class holding the interfaces, so they should never get evicted");
				// Attribute is a superclass of this interface
				if (!attributes.ContainsKey(curInterface))
				{
					// invalidate state to force recomputation in captureState()
					this.currentState = null;
                    attributes.Add(new SupportClass.AttributeImplItem(curInterface, att));
                    if (!attributeImpls.ContainsKey(clazz))
                    {
                        attributeImpls.Add(new SupportClass.AttributeImplItem(clazz, att));
                    }
				}
			}
		}
예제 #5
0
        // {{Aroush-2.9 Port issue, need to mimic java's IdentityHashMap
        /*
         * From Java docs:
         * This class implements the Map interface with a hash table, using
         * reference-equality in place of object-equality when comparing keys
         * (and values). In other words, in an IdentityHashMap, two keys k1 and k2
         * are considered equal if and only if (k1==k2). (In normal Map
         * implementations (like HashMap) two keys k1 and k2 are considered
         * equal if and only if (k1==null ? k2==null : k1.equals(k2)).)
         */
        // Aroush-2.9}}
        /// <summary>Adds a custom AttributeImpl instance with one or more Attribute interfaces. </summary>
        public virtual void AddAttributeImpl(AttributeImpl att)
        {
            System.Type clazz = att.GetType();
            if (attributeImpls.Contains(clazz))
                return ;
            System.Collections.ArrayList foundInterfaces;
            lock (knownImplClasses)
            {
                foundInterfaces = (System.Collections.ArrayList) knownImplClasses[clazz];
                if (foundInterfaces == null)
                {
                    knownImplClasses.Add(clazz, foundInterfaces = new System.Collections.ArrayList());
                    // find all interfaces that this attribute instance implements
                    // and that extend the Attribute interface
                    System.Type actClazz = clazz;
                    do
                    {
                        System.Type[] interfaces = actClazz.GetInterfaces();
                        for (int i = 0; i < interfaces.Length; i++)
                        {
                            System.Type curInterface = interfaces[i];
                            if (curInterface != typeof(Attribute) && typeof(Attribute).IsAssignableFrom(curInterface))
                            {
                                foundInterfaces.Add(curInterface);
                            }
                        }
                        actClazz = actClazz.BaseType;
                    }
                    while (actClazz != null);
                }
            }

            // add all interfaces of this AttributeImpl to the maps
            for (System.Collections.IEnumerator it = foundInterfaces.GetEnumerator(); it.MoveNext(); )
            {
                System.Type curInterface = (System.Type) it.Current;
                // Attribute is a superclass of this interface
                if (!attributes.ContainsKey(curInterface))
                {
                    // invalidate state to force recomputation in captureState()
                    this.currentState = null;
                    attributes.Add(new SupportClass.AttributeImplItem(curInterface, att));
                    if (!attributeImpls.ContainsKey(clazz))
                    {
                        attributeImpls.Add(new SupportClass.AttributeImplItem(clazz, att));
                    }
                }
            }
        }