Пример #1
0
 public void Parse(InputSource input) {
   Setup();
   TextReader r = GetReader(input);
   _contentHandler.StartDocument();
   _scanner.ResetDocumentLocator(input.PublicId, input.SystemId);
   var locator = _scanner as ILocator;
   if (locator != null) {
     _contentHandler.SetDocumentLocator(locator);
   }
   if (!(_schema.Uri.Equals(""))) {
     _contentHandler.StartPrefixMapping(_schema.Prefix, _schema.Uri);
   }
   _scanner.Scan(r, this);
 }
Пример #2
0
 /// <summary>
 ///   Return a Reader based on the contents of an InputSource
 ///   Buffer the Stream
 /// </summary>
 /// <param name="s"></param>
 /// <returns></returns>
 private TextReader GetReader(InputSource s) {
   TextReader r = s.Reader;
   Stream i = s.Stream;
   Encoding encoding = s.Encoding;
   string publicid = s.PublicId;
   string systemid = s.SystemId;
   if (r == null) {
     if (i == null) {
       i = GetInputStream(publicid, systemid);
     }
     if (!(i is BufferedStream)) {
       i = new BufferedStream(i);
     }
     if (encoding == null) {
       r = _autoDetector.AutoDetectingReader(i);
     } else {
       //try {
       //TODO: Safe?
       r = new StreamReader(i, encoding);
       //  }
       //catch (UnsupportedEncodingException e) {
       //  r = new StreamReader(i);
       //  }
     }
   }
   //		r = new BufferedReader(r);
   return r;
 }
Пример #3
0
    /// <summary>
    ///   Auxiliary API to parse an XML document, used mostly
    ///   when no URI is available.
    ///   If you want anything useful to happen, you should set
    ///   at least one type of handler.
    /// </summary>
    /// <param name="source">
    ///   The XML input source.  Don't set 'encoding' unless
    ///   you know for a fact that it's correct.
    /// </param>
    /// <seealso cref="EntityResolver" />
    /// <seealso cref="DTDHandler" />
    /// <seealso cref="ContentHandler" />
    /// <seealso cref="ErrorHandler" />
    /// <exception cref="SAXException">
    ///   The handlers may throw any SAXException,
    ///   and the parser normally throws SAXParseException objects.
    /// </exception>
    /// <exception cref="IOException">
    ///   IOExceptions are normally through through
    ///   the parser if there are problems reading the source document.
    /// </exception>
    public virtual void Parse(InputSource source) {
      lock (_base) {
        _parser = new XmlParser();
        _parser.setHandler(this);

        try {
          string systemId = source.SystemId;
          // MHK addition. SAX2 says the systemId supplied must be absolute.
          // But often it isn't. This code tries, if necessary, to expand it
          // relative to the current working directory

          systemId = TryToExpand(systemId);

          // duplicate first entry, in case startDocument handler
          // needs to use Locator.getSystemId(), before entities
          // start to get reported by the parser

          //if (systemId != null)
          _entityStack.Push(systemId);
          //else    // can't happen after tryToExpand()
          //    entityStack.push ("illegal:unknown system ID");

          _parser.DoParse(systemId, source.PublicId, source.Reader, source.Stream, source.Encoding);
        } catch (SAXException e) {
          throw;
        } catch (IOException e) {
          throw;
        } catch (Exception e) {
          throw new SAXException(e.Message, e);
        } finally {
          _contentHandler.EndDocument();
          _entityStack.Clear();
        }
      }
    }