Exemplo n.º 1
0
        private static IDictionary <Type, MethodInfo> NewDispatchCache(IDomNodeVisitor v)
        {
            var dispatch = new Dictionary <Type, MethodInfo>();

            foreach (var face in v.GetType().GetInterfaces())
            {
                if (!typeof(IDomNodeVisitor).IsAssignableFrom(face))
                {
                    continue;
                }
                if (typeof(IDomNodeVisitor) == face)
                {
                    continue;
                }
                var mapping = v.GetType().GetInterfaceMap(face);
                for (int i = 0; i < mapping.InterfaceMethods.Length; i++)
                {
                    if (mapping.InterfaceMethods[i].Name == "Visit")
                    {
                        var elementType = mapping.InterfaceMethods[i].GetParameters()[0].ParameterType;
                        dispatch[elementType] = mapping.TargetMethods[i];
                    }
                }
            }
            return(dispatch);
        }
Exemplo n.º 2
0
        public DomNodeVisitDispatcher(IDomNodeVisitor visitor)
        {
            if (visitor == null)
            {
                throw new ArgumentNullException(nameof(visitor));
            }

            _visitor = visitor;
        }
Exemplo n.º 3
0
 public static void Visit(DomObject obj, IDomNodeVisitor visitor)
 {
     if (obj == null)
     {
         throw new ArgumentNullException(nameof(obj));
     }
     if (visitor == null)
     {
         throw new ArgumentNullException(nameof(visitor));
     }
     DomNodeVisitDispatcher.Create(visitor).Dispatch(obj);
 }
Exemplo n.º 4
0
        private static void FastDispatch(IDomNodeVisitor v, DomObject obj)
        {
            switch (obj)
            {
            case DomElement _:
                v.Visit((DomElement)obj);
                return;

            case DomAttribute _:
                v.Visit((DomAttribute)obj);
                return;

            case DomText _:
                v.Visit((DomText)obj);
                return;

            case DomCDataSection _:
                v.Visit((DomCDataSection)obj);
                return;

            case DomEntityReference _:
                v.Visit((DomEntityReference)obj);
                return;

            case DomEntity _:
                v.Visit((DomEntity)obj);
                return;

            case DomProcessingInstruction _:
                v.Visit((DomProcessingInstruction)obj);
                return;

            case DomComment _:
                v.Visit((DomComment)obj);
                return;

            case DomDocument _:
                v.Visit((DomDocument)obj);
                return;

            case DomDocumentType _:
                v.Visit((DomDocumentType)obj);
                return;

            case DomDocumentFragment _:
                v.Visit((DomDocumentFragment)obj);
                return;

            case DomNotation _:
                v.Visit((DomNotation)obj);
                return;
            }
        }
Exemplo n.º 5
0
        public static void VisitAll(IEnumerable <DomObject> objs, IDomNodeVisitor visitor)
        {
            if (objs == null || visitor == null)
            {
                return;
            }
            var d = DomNodeVisitDispatcher.Create(visitor);

            foreach (var obj in objs)
            {
                d.Dispatch(obj);
            }
        }
Exemplo n.º 6
0
        public static IDomNodeVisitDispatcher Create(IDomNodeVisitor visitor)
        {
            if (visitor is IDomNodeVisitDispatcher d)
            {
                return(d);
            }
            var result = new DomNodeVisitDispatcher(visitor);

            if (result.DispatchCache.IsEmpty)
            {
                return(new FastDispatcher(visitor));
            }
            return(result);
        }
Exemplo n.º 7
0
 public FastDispatcher(IDomNodeVisitor visitor)
 {
     _visitor = visitor;
 }