public DiscoveryClientResultCollection WriteAll(string directory, string topLevelFilename)
        {
            DiscoveryClientResultsFile results = new DiscoveryClientResultsFile();
            Hashtable filenames        = new Hashtable();
            string    topLevelFullPath = Path.Combine(directory, topLevelFilename);

            // write out each of the documents
            DictionaryEntry[] entries = new DictionaryEntry[Documents.Count];
            int i = 0;

            foreach (DictionaryEntry entry in Documents)
            {
                entries[i++] = entry;
            }
            foreach (DictionaryEntry entry in entries)
            {
                string url      = (string)entry.Key;
                object document = entry.Value;
                if (document == null)
                {
                    continue;
                }
                DiscoveryReference reference = References[url];
                string             filename  = GetUniqueFilename(filenames, Path.GetFullPath(Path.Combine(directory, reference.DefaultFilename)));
                results.Results.Add(new DiscoveryClientResult(reference.GetType(), url, GetRelativePath(filename, topLevelFullPath)));
                Stream file = File.Create(filename);
                try {
                    reference.WriteDocument(document, file);
                }
                finally {
                    file.Close();
                }
            }

            // write out the file that points to all those documents.
            XmlSerializer ser          = new XmlSerializer(typeof(DiscoveryClientResultsFile));
            Stream        topLevelFile = File.Create(topLevelFullPath);

            try {
                ser.Serialize(new StreamWriter(topLevelFile, new UTF8Encoding(false)), results);
            }
            finally {
                topLevelFile.Close();
            }

            return(results.Results);
        }
        public DiscoveryClientResultCollection WriteAll(string directory, string topLevelFilename)
        {
            DiscoveryClientResultsFile o = new DiscoveryClientResultsFile();
            Hashtable filenames          = new Hashtable();
            string    relativeTo         = Path.Combine(directory, topLevelFilename);

            DictionaryEntry[] entryArray = new DictionaryEntry[this.Documents.Count + this.InlinedSchemas.Keys.Count];
            int num = 0;

            foreach (DictionaryEntry entry in this.Documents)
            {
                entryArray[num++] = entry;
            }
            foreach (DictionaryEntry entry2 in this.InlinedSchemas)
            {
                entryArray[num++] = entry2;
            }
            foreach (DictionaryEntry entry3 in entryArray)
            {
                string key      = (string)entry3.Key;
                object document = entry3.Value;
                if (document != null)
                {
                    DiscoveryReference reference      = this.References[key];
                    string             uniqueFilename = (reference == null) ? DiscoveryReference.FilenameFromUrl(base.Url) : reference.DefaultFilename;
                    uniqueFilename = GetUniqueFilename(filenames, Path.GetFullPath(Path.Combine(directory, uniqueFilename)));
                    o.Results.Add(new DiscoveryClientResult((reference == null) ? null : reference.GetType(), key, GetRelativePath(uniqueFilename, relativeTo)));
                    Stream stream = System.IO.File.Create(uniqueFilename);
                    try
                    {
                        reference.WriteDocument(document, stream);
                    }
                    finally
                    {
                        stream.Close();
                    }
                }
            }
            XmlSerializer serializer = new XmlSerializer(typeof(DiscoveryClientResultsFile));
            Stream        stream2    = System.IO.File.Create(relativeTo);

            try
            {
                serializer.Serialize((TextWriter) new StreamWriter(stream2, new UTF8Encoding(false)), o);
            }
            finally
            {
                stream2.Close();
            }
            return(o.Results);
        }
        public DiscoveryClientResultCollection ReadAll(string topLevelFilename)
        {
            XmlSerializer serializer        = new XmlSerializer(typeof(DiscoveryClientResultsFile));
            Stream        stream            = System.IO.File.OpenRead(topLevelFilename);
            string        directoryName     = Path.GetDirectoryName(topLevelFilename);
            DiscoveryClientResultsFile file = null;

            try
            {
                file = (DiscoveryClientResultsFile)serializer.Deserialize(stream);
                for (int i = 0; i < file.Results.Count; i++)
                {
                    if (file.Results[i] == null)
                    {
                        throw new InvalidOperationException(Res.GetString("WebNullRef"));
                    }
                    string referenceTypeName = file.Results[i].ReferenceTypeName;
                    if ((referenceTypeName == null) || (referenceTypeName.Length == 0))
                    {
                        throw new InvalidOperationException(Res.GetString("WebRefInvalidAttribute", new object[] { "referenceType" }));
                    }
                    DiscoveryReference reference = (DiscoveryReference)Activator.CreateInstance(Type.GetType(referenceTypeName));
                    reference.ClientProtocol = this;
                    string url = file.Results[i].Url;
                    if ((url == null) || (url.Length == 0))
                    {
                        throw new InvalidOperationException(Res.GetString("WebRefInvalidAttribute2", new object[] { reference.GetType().FullName, "url" }));
                    }
                    reference.Url = url;
                    string filename = file.Results[i].Filename;
                    if ((filename == null) || (filename.Length == 0))
                    {
                        throw new InvalidOperationException(Res.GetString("WebRefInvalidAttribute2", new object[] { reference.GetType().FullName, "filename" }));
                    }
                    Stream stream2 = System.IO.File.OpenRead(Path.Combine(directoryName, file.Results[i].Filename));
                    try
                    {
                        this.Documents[reference.Url] = reference.ReadDocument(stream2);
                    }
                    finally
                    {
                        stream2.Close();
                    }
                    this.References[reference.Url] = reference;
                }
                this.ResolveAll();
            }
            finally
            {
                stream.Close();
            }
            return(file.Results);
        }
        /// <include file='doc\DiscoveryDocumentReference.uex' path='docs/doc[@for="DiscoveryDocumentReference.Resolve"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        protected internal override void Resolve(string contentType, Stream stream)
        {
            DiscoveryDocument document = null;

            if (ContentType.IsHtml(contentType))
            {
                string newRef = LinkGrep.SearchForLink(stream);
                if (newRef != null)
                {
                    string newUrl = UriToString(Url, newRef);
                    document = GetDocumentNoParse(ref newUrl, ClientProtocol);
                    Url      = newUrl;
                }
                else
                {
                    throw new InvalidContentTypeException(Res.GetString(Res.WebInvalidContentType, contentType), contentType);
                }
            }

            if (document == null)   // probably xml...
            {
                XmlTextReader reader = new XmlTextReader(new StreamReader(stream, RequestResponseUtils.GetEncoding(contentType)));
                reader.XmlResolver        = null;
                reader.WhitespaceHandling = WhitespaceHandling.Significant;
                reader.DtdProcessing      = DtdProcessing.Prohibit;
                if (DiscoveryDocument.CanRead(reader))
                {
                    // it's a discovery document, so just read it.
                    document = DiscoveryDocument.Read(reader);
                }
                else
                {
                    // check out the processing instructions before the first tag.  if any of them
                    // match the form specified in the DISCO spec, save the href.
                    stream.Position = 0;
                    XmlTextReader newReader = new XmlTextReader(new StreamReader(stream, RequestResponseUtils.GetEncoding(contentType)));
                    newReader.XmlResolver   = null;
                    newReader.DtdProcessing = DtdProcessing.Prohibit;
                    while (newReader.NodeType != XmlNodeType.Element)
                    {
                        if (newReader.NodeType == XmlNodeType.ProcessingInstruction)
                        {
                            // manually parse the PI contents since XmlTextReader won't automatically do it
                            StringBuilder sb = new StringBuilder("<pi ");
                            sb.Append(newReader.Value);
                            sb.Append("/>");
                            XmlTextReader piReader = new XmlTextReader(new StringReader(sb.ToString()));
                            piReader.XmlResolver   = null;
                            piReader.DtdProcessing = DtdProcessing.Prohibit;
                            piReader.Read();
                            string type      = piReader["type"];
                            string alternate = piReader["alternate"];
                            string href      = piReader["href"];
                            if (type != null && ContentType.MatchesBase(type, ContentType.TextXml) &&
                                alternate != null && string.Compare(alternate, "yes", StringComparison.OrdinalIgnoreCase) == 0 &&
                                href != null)
                            {
                                // we got a PI with the right attributes

                                // there is a link to a discovery document. follow it after fully qualifying it.
                                string newUrl = UriToString(Url, href);
                                document = GetDocumentNoParse(ref newUrl, ClientProtocol);
                                Url      = newUrl;
                                break;
                            }
                        }
                        newReader.Read();
                    }
                }
            }

            if (document == null)
            {
                // there is no discovery document at this location
                Exception exception;
                if (ContentType.IsXml(contentType))
                {
                    exception = new ArgumentException(Res.GetString(Res.WebInvalidFormat));
                }
                else
                {
                    exception = new InvalidContentTypeException(Res.GetString(Res.WebInvalidContentType, contentType), contentType);
                }
                throw new InvalidOperationException(Res.GetString(Res.WebMissingDocument, Url), exception);
            }

            ClientProtocol.References[Url] = this;
            ClientProtocol.Documents[Url]  = document;

            foreach (object o in document.References)
            {
                if (o is DiscoveryReference)
                {
                    DiscoveryReference r = (DiscoveryReference)o;
                    if (r.Url.Length == 0)
                    {
                        throw new InvalidOperationException(Res.GetString(Res.WebEmptyRef, r.GetType().FullName, Url));
                    }
                    r.Url = UriToString(Url, r.Url);
                    //All inheritors of DiscoveryReference that got URIs relative
                    //to Ref property should adjust them like ContractReference does here.
                    ContractReference cr = r as ContractReference;
                    if ((cr != null) && (cr.DocRef != null))
                    {
                        cr.DocRef = UriToString(Url, cr.DocRef);
                    }
                    r.ClientProtocol = ClientProtocol;
                    ClientProtocol.References[r.Url] = r;
                }
                else
                {
                    ClientProtocol.AdditionalInformation.Add(o);
                }
            }

            return;
        }
Example #5
0
        public DiscoveryClientResultCollection ReadAll(string topLevelFilename)
        {
            XmlSerializer ser                  = new XmlSerializer(typeof(DiscoveryClientResultsFile));
            Stream        file                 = File.OpenRead(topLevelFilename);
            string        topLevelPath         = Path.GetDirectoryName(topLevelFilename);
            DiscoveryClientResultsFile results = null;

            try {
                results = (DiscoveryClientResultsFile)ser.Deserialize(file);
                for (int i = 0; i < results.Results.Count; i++)
                {
                    if (results.Results[i] == null)
                    {
                        throw new InvalidOperationException(Res.GetString(Res.WebNullRef));
                    }
                    string typeName = results.Results[i].ReferenceTypeName;
                    if (typeName == null || typeName.Length == 0)
                    {
                        throw new InvalidOperationException(Res.GetString(Res.WebRefInvalidAttribute, "referenceType"));
                    }
                    DiscoveryReference reference = (DiscoveryReference)Activator.CreateInstance(Type.GetType(typeName));
                    reference.ClientProtocol = this;

                    string url = results.Results[i].Url;
                    if (url == null || url.Length == 0)
                    {
                        throw new InvalidOperationException(Res.GetString(Res.WebRefInvalidAttribute2, reference.GetType().FullName, "url"));
                    }
                    reference.Url = url;
                    string fileName = results.Results[i].Filename;
                    if (fileName == null || fileName.Length == 0)
                    {
                        throw new InvalidOperationException(Res.GetString(Res.WebRefInvalidAttribute2, reference.GetType().FullName, "filename"));
                    }

                    Stream docFile = File.OpenRead(Path.Combine(topLevelPath, results.Results[i].Filename));
                    try {
                        Documents[reference.Url] = reference.ReadDocument(docFile);
                        Debug.Assert(Documents[reference.Url] != null, "Couldn't deserialize file " + results.Results[i].Filename);
                    }
                    finally {
                        docFile.Close();
                    }
                    References[reference.Url] = reference;
                }
                ResolveAll();
            }
            finally {
                file.Close();
            }
            return(results.Results);
        }
        protected internal override void Resolve(string contentType, Stream stream)
        {
            DiscoveryDocument documentNoParse = null;

            if (ContentType.IsHtml(contentType))
            {
                string relUrl = LinkGrep.SearchForLink(stream);
                if (relUrl == null)
                {
                    throw new InvalidContentTypeException(System.Web.Services.Res.GetString("WebInvalidContentType", new object[] { contentType }), contentType);
                }
                string url = DiscoveryReference.UriToString(this.Url, relUrl);
                documentNoParse = GetDocumentNoParse(ref url, base.ClientProtocol);
                this.Url        = url;
            }
            if (documentNoParse == null)
            {
                XmlTextReader xmlReader = new XmlTextReader(new StreamReader(stream, RequestResponseUtils.GetEncoding(contentType)))
                {
                    XmlResolver        = null,
                    WhitespaceHandling = WhitespaceHandling.Significant,
                    DtdProcessing      = DtdProcessing.Prohibit
                };
                if (DiscoveryDocument.CanRead(xmlReader))
                {
                    documentNoParse = DiscoveryDocument.Read(xmlReader);
                }
                else
                {
                    stream.Position = 0L;
                    XmlTextReader reader2 = new XmlTextReader(new StreamReader(stream, RequestResponseUtils.GetEncoding(contentType)))
                    {
                        XmlResolver   = null,
                        DtdProcessing = DtdProcessing.Prohibit
                    };
                    while (reader2.NodeType != XmlNodeType.Element)
                    {
                        if (reader2.NodeType == XmlNodeType.ProcessingInstruction)
                        {
                            StringBuilder builder = new StringBuilder("<pi ");
                            builder.Append(reader2.Value);
                            builder.Append("/>");
                            XmlTextReader reader3 = new XmlTextReader(new StringReader(builder.ToString()))
                            {
                                XmlResolver   = null,
                                DtdProcessing = DtdProcessing.Prohibit
                            };
                            reader3.Read();
                            string str3 = reader3["type"];
                            string strA = reader3["alternate"];
                            string str5 = reader3["href"];
                            if ((((str3 != null) && ContentType.MatchesBase(str3, "text/xml")) && ((strA != null) && (string.Compare(strA, "yes", StringComparison.OrdinalIgnoreCase) == 0))) && (str5 != null))
                            {
                                string str6 = DiscoveryReference.UriToString(this.Url, str5);
                                documentNoParse = GetDocumentNoParse(ref str6, base.ClientProtocol);
                                this.Url        = str6;
                                break;
                            }
                        }
                        reader2.Read();
                    }
                }
            }
            if (documentNoParse == null)
            {
                Exception exception;
                if (ContentType.IsXml(contentType))
                {
                    exception = new ArgumentException(System.Web.Services.Res.GetString("WebInvalidFormat"));
                }
                else
                {
                    exception = new InvalidContentTypeException(System.Web.Services.Res.GetString("WebInvalidContentType", new object[] { contentType }), contentType);
                }
                throw new InvalidOperationException(System.Web.Services.Res.GetString("WebMissingDocument", new object[] { this.Url }), exception);
            }
            base.ClientProtocol.References[this.Url] = this;
            base.ClientProtocol.Documents[this.Url]  = documentNoParse;
            foreach (object obj2 in documentNoParse.References)
            {
                if (obj2 is DiscoveryReference)
                {
                    DiscoveryReference reference = (DiscoveryReference)obj2;
                    if (reference.Url.Length == 0)
                    {
                        throw new InvalidOperationException(System.Web.Services.Res.GetString("WebEmptyRef", new object[] { reference.GetType().FullName, this.Url }));
                    }
                    reference.Url = DiscoveryReference.UriToString(this.Url, reference.Url);
                    ContractReference reference2 = reference as ContractReference;
                    if ((reference2 != null) && (reference2.DocRef != null))
                    {
                        reference2.DocRef = DiscoveryReference.UriToString(this.Url, reference2.DocRef);
                    }
                    reference.ClientProtocol = base.ClientProtocol;
                    base.ClientProtocol.References[reference.Url] = reference;
                }
                else
                {
                    base.ClientProtocol.AdditionalInformation.Add(obj2);
                }
            }
        }