From: Oleg T. <he...@us...> - 2004-11-07 14:50:06
|
Update of /cvsroot/mvp-xml/XInclude/v1/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8371/v1/src Modified Files: TextIncludingReader.cs XIncludingReader.cs Log Message: Caching for the acquired infosets. Index: XIncludingReader.cs =================================================================== RCS file: /cvsroot/mvp-xml/XInclude/v1/src/XIncludingReader.cs,v retrieving revision 1.27 retrieving revision 1.28 diff -u -d -r1.27 -r1.28 --- XIncludingReader.cs 3 Nov 2004 14:17:36 -0000 1.27 +++ XIncludingReader.cs 7 Nov 2004 14:49:56 -0000 1.28 @@ -65,6 +65,8 @@ private bool _exposeTextAsCDATA; //Top-level included item has different xml:lang private bool _differentLang = false; + //Acquired infosets cache + private static Hashtable _cache; #endregion #region Constructors @@ -979,7 +981,7 @@ /// <summary> /// Fetches resource by URI. /// </summary> - internal static Stream GetResource(string href, Uri includeLocation, + internal static Stream GetResource(string includeLocation, string accept, string acceptLanguage, out WebResponse response) { WebRequest wReq; @@ -989,11 +991,11 @@ } catch (NotSupportedException nse) { - throw new ResourceException(SR.GetString("URISchemaNotSupported", href), nse); + throw new ResourceException(SR.GetString("URISchemaNotSupported", includeLocation), nse); } catch (SecurityException se) { - throw new ResourceException(SR.GetString("SecurityException", href), se); + throw new ResourceException(SR.GetString("SecurityException", includeLocation), se); } //Add accept headers if this is HTTP request HttpWebRequest httpReq = wReq as HttpWebRequest; @@ -1021,7 +1023,7 @@ } catch (WebException we) { - throw new ResourceException(SR.GetString("ResourceError", href), we); + throw new ResourceException(SR.GetString("ResourceError", includeLocation), we); } return response.GetResponseStream(); } @@ -1296,12 +1298,67 @@ } /// <summary> - /// Creates a XmlReader over acquired infoset. + /// Creates acquired infoset. + /// </summary> + private string CreateAcquiredInfoset(Uri includeLocation) + { + if (_cache == null) + _cache = new Hashtable(); + WeakReference wr = (WeakReference)_cache[includeLocation.AbsoluteUri]; + if (wr != null && wr.IsAlive) + { + return (string)wr.Target; + } + else + { + //Not cached or GCollected + WebResponse wRes; + Stream stream = GetResource(includeLocation.AbsoluteUri, + _reader.GetAttribute(_keywords.Accept), + _reader.GetAttribute(_keywords.AcceptLanguage), out wRes); + XIncludingReader xir = new XIncludingReader(wRes.ResponseUri.AbsoluteUri, stream, _nameTable); + xir.Normalization = _normalization; + xir.WhitespaceHandling = _whiteSpaceHandling; + StringWriter sw = new StringWriter(); + XmlTextWriter w = new XmlTextWriter(sw); + try + { + while (xir.Read()) + w.WriteNode(xir, false); + } + finally + { + if (xir != null) + xir.Close(); + if (w != null) + w.Close(); + } + string content = sw.ToString(); + lock(_cache) + { + if (!_cache.ContainsKey(includeLocation.AbsoluteUri)) + _cache.Add(includeLocation.AbsoluteUri, new WeakReference(content)); + } + return content; + } + } + + /// <summary> + /// Creates acquired infoset. /// </summary> /// <param name="reader">Source reader</param> - private XmlReader CreateAcquiredInfoset(XmlReader reader) + private string CreateAcquiredInfoset(Uri includeLocation, TextReader reader) { - string uri = reader.BaseURI; + return CreateAcquiredInfoset( + new XmlBaseAwareXmlTextReader(includeLocation.AbsoluteUri, reader, _nameTable)); + } + + /// <summary> + /// Creates acquired infoset. + /// </summary> + /// <param name="reader">Source reader</param> + private string CreateAcquiredInfoset(XmlReader reader) + { //TODO: Try to stream out this stuff XIncludingReader xir = new XIncludingReader(reader); StringWriter sw = new StringWriter(); @@ -1318,7 +1375,7 @@ if (w != null) w.Close(); } - return new XmlTextReader(uri, new StringReader(sw.ToString())); + return sw.ToString(); } /// <summary> @@ -1336,23 +1393,24 @@ if (_xmlResolver == null) { //No custom resolver - WebResponse wRes; - Stream stream = GetResource(href, includeLocation, - _reader.GetAttribute(_keywords.Accept), - _reader.GetAttribute(_keywords.AcceptLanguage), out wRes); - //Push current reader to the stack - _readers.Push(_reader); if (xpointer != null) { + //Push current reader to the stack + _readers.Push(_reader); //XPointers should be resolved against the acquired infoset, - //not the source infoset - _reader = new XPointerReader( - CreateAcquiredInfoset( - new XIncludingReader(wRes.ResponseUri.AbsoluteUri, stream)), - xpointer); + //not the source infoset + _reader = new XPointerReader(includeLocation.AbsoluteUri, + CreateAcquiredInfoset(includeLocation), + xpointer); } else - { + { + WebResponse wRes; + Stream stream = GetResource(includeLocation.AbsoluteUri, + _reader.GetAttribute(_keywords.Accept), + _reader.GetAttribute(_keywords.AcceptLanguage), out wRes); + //Push current reader to the stack + _readers.Push(_reader); XmlTextReader r = new XmlBaseAwareXmlTextReader(wRes.ResponseUri.AbsoluteUri, stream, _nameTable); r.Normalization = _normalization; r.WhitespaceHandling = _whiteSpaceHandling; @@ -1387,15 +1445,15 @@ { //XPointers should be resolved against the acquired infoset, //not the source infoset - _reader = new XPointerReader( - CreateAcquiredInfoset( - new XIncludingReader(includeLocation.AbsoluteUri, (TextReader)resource)), - xpointer); + _reader = new XPointerReader(includeLocation.AbsoluteUri, + CreateAcquiredInfoset(includeLocation, (TextReader)resource), + xpointer); } else if (resource is XmlReader) { - _reader = new XPointerReader( - CreateAcquiredInfoset((XmlReader)resource), xpointer); + XmlReader r = (XmlReader)resource; + _reader = new XPointerReader(r.BaseURI, + CreateAcquiredInfoset(r), xpointer); } else { Index: TextIncludingReader.cs =================================================================== RCS file: /cvsroot/mvp-xml/XInclude/v1/src/TextIncludingReader.cs,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- TextIncludingReader.cs 2 Nov 2004 14:31:56 -0000 1.7 +++ TextIncludingReader.cs 7 Nov 2004 14:49:56 -0000 1.8 @@ -210,7 +210,7 @@ { WebResponse wRes; Stream stream = XIncludingReader.GetResource(_includeLocation.AbsoluteUri, - _includeLocation, _accept, _acceptLanguage, out wRes); + _accept, _acceptLanguage, out wRes); StreamReader reader; /* According to the spec, encoding should be determined as follows: * external encoding information, if available, otherwise |