Example #1
0
 public virtual void StartPrefixMapping(string prefix, string uri)
 {
     if (_contentHandler != null)
     {
         _contentHandler.StartPrefixMapping(prefix, uri);
     }
 }
Example #2
0
        private void declarePrefix(string prefix, string uri)
        {
            int index = uri.IndexOf(':');

            // many versions of nwalsh docbook stylesheets
            // have bogus URLs; so this can't be an error...
            if (index < 1 && uri.Length != 0)
            {
                warn("relative URI for namespace: " + uri);
            }

            // FIXME:  char [0] must be ascii alpha; chars [1..index]
            // must be ascii alphanumeric or in "+-." [RFC 2396]
            uri = string.Intern(uri);
            if (namespaceSupport.DeclarePrefix(prefix, uri))
            {
                contentHandler.StartPrefixMapping(prefix, uri);
            }
        }
Example #3
0
        public virtual void Attribute(string aname, string value, bool isSpecified)
        {
            // Code changed by MHK 16 April 2001.
            // The only safe thing to do is to process all the namespace declarations
            // first, then process the ordinary attributes. So if this is a namespace
            // declaration, we deal with it now, otherwise we save it till we get the
            // startElement call.

            if (_attributeCount++ == 0)
            {
                if (_namespaces)
                {
                    _prefixStack.PushContext();
                }
            }

            // set nsTemp [0] == namespace URI (or empty)
            // set nsTemp [1] == local name (or empty)
            if (value == null)
            {
                // MHK: I think this can only happen on an error recovery path
                // MHK: I was wrong: AElfred was notifying null values of attribute
                // declared in the DTD as #IMPLIED. But I've now changed it so it doesn't.
                return;
            }

            if (_namespaces && aname.StartsWith("xmlns"))
            {
                if (aname.Length == 5)
                {
                    _prefixStack.DeclarePrefix("", value);
                    //System.err.println("Declare default prefix = "+value);
                    _contentHandler.StartPrefixMapping("", value);
                }
                else if (aname[5] == ':' && !aname.Equals("xmlns:xml"))
                {
                    if (aname.Length == 6)
                    {
                        _errorHandler.Error(new SAXParseException("Missing namespace prefix in namespace declaration: " + aname,
                                                                  this));
                        return;
                    }
                    string prefix = aname.Substring(6);

                    if (value.Length == 0)
                    {
                        _errorHandler.Error(new SAXParseException("Missing URI in namespace declaration: " + aname, this));
                        return;
                    }
                    _prefixStack.DeclarePrefix(prefix, value);
                    //System.err.println("Declare prefix " +prefix+"="+value);
                    _contentHandler.StartPrefixMapping(prefix, value);
                }

                if (!_xmlNames)
                {
                    // if xmlNames option wasn't selected,
                    // we don't report xmlns:* declarations as attributes
                    return;
                }
            }

            _attributeNames.Add(aname);
            _attributeValues.Add(value);
        }
Example #4
0
        public virtual void Parse(TextReader text)
        {
            Stack             nsstack      = new Stack();
            Locator           locator      = new Locator();
            SAXParseException saxException = new SAXParseException();
            Attributes        atts         = new Attributes();
            XmlTextReader     reader       = null;

            try
            {
                reader = new XmlTextReader(text);
                object nsuri = reader.NameTable.Add(
                    "http://www.w3.org/2000/xmlns/");
                handler.StartDocument();
                while (reader.Read())
                {
                    string prefix = "";
                    locator.LineNumber   = reader.LineNumber;
                    locator.ColumnNumber = reader.LinePosition;
                    handler.SetDocumentLocator(locator);
                    switch (reader.NodeType)
                    {
                    case XmlNodeType.Element:
                        nsstack.Push(null);     //marker
                        atts = new Attributes();
                        while (reader.MoveToNextAttribute())
                        {
                            if (reader.NamespaceURI.Equals(nsuri))
                            {
                                prefix = "";
                                if (reader.Prefix == "xmlns")
                                {
                                    prefix = reader.LocalName;
                                }
                                nsstack.Push(prefix);
                                handler.StartPrefixMapping(prefix, reader.Value);
                            }
                            else
                            {
                                atts.AddAttribute(reader.NamespaceURI, reader.Name, reader.Name, reader.GetType().ToString(), reader.Value);
                            }
                        }
                        reader.MoveToElement();
                        Handler.StartElement(reader.NamespaceURI, reader.LocalName, reader.Name, atts);
                        if (reader.IsEmptyElement)
                        {
                            handler.EndElement(reader.NamespaceURI, reader.LocalName, reader.Name);
                        }
                        break;

                    case XmlNodeType.EndElement:
                        handler.EndElement(reader.NamespaceURI, reader.LocalName, reader.Name);
                        while (prefix != null)
                        {
                            handler.EndPrefixMapping(prefix);
                            prefix = (string)nsstack.Pop();
                        }
                        break;

                    case XmlNodeType.Text:
                        handler.Characters(reader.Value.ToCharArray(), 0, reader.Value.Length);
                        break;

                    case XmlNodeType.ProcessingInstruction:
                        handler.ProcessingInstruction(reader.Name, reader.Value);
                        break;

                    case XmlNodeType.Whitespace:
                        char[] whiteSpace = reader.Value.ToCharArray();
                        handler.IgnorableWhitespace(whiteSpace, 0, 1);
                        break;

                    case XmlNodeType.Entity:
                        handler.SkippedEntity(reader.Name);
                        break;
                    }
                } //While
                handler.EndDocument();
            }     //try
            catch (Exception exception)
            {
                saxException.LineNumber = reader.LineNumber;
                saxException.SystemID   = "";
                saxException.setMessage(exception.GetBaseException().ToString());
                errorHandler.Error(saxException);
            }
            finally
            {
                if (reader.ReadState != ReadState.Closed)
                {
                    reader.Close();
                }
            }
        } //parse()