Exemplo n.º 1
0
        private string SystemProperty(string qname)
        {
            string result = string.Empty;

            string prefix;
            string local;

            PrefixQName.ParseQualifiedName(qname, out prefix, out local);

            // verify the prefix corresponds to the Xslt namespace
            string urn = LookupNamespace(prefix);

            if (urn == XmlReservedNs.NsXslt)
            {
                if (local == "version")
                {
                    result = "1";
                }
                else if (local == "vendor")
                {
                    result = "Microsoft";
                }
                else if (local == "vendor-url")
                {
                    result = "http://www.microsoft.com";
                }
            }
            else
            {
                if (urn == null && prefix != null)
                {
                    // if prefix exist it has to be mapped to namespace.
                    // Can it be "" here ?
                    throw XsltException.Create(SR.Xslt_InvalidPrefix, prefix);
                }
                return(string.Empty);
            }

            return(result);
        }
Exemplo n.º 2
0
        internal XPathNavigator GetNavigator(Uri ruri)
        {
            XPathNavigator?result = null;

            if (_documentCache != null)
            {
                result = _documentCache[ruri] as XPathNavigator;
                if (result != null)
                {
                    return(result.Clone());
                }
            }
            else
            {
                _documentCache = new Hashtable();
            }

            object?input = _resolver.GetEntity(ruri, null, null);

            if (input is Stream)
            {
                XmlTextReaderImpl tr = new XmlTextReaderImpl(ruri.ToString(), (Stream)input);
                {
                    tr.XmlResolver = _resolver;
                }
                // reader is closed by Compiler.LoadDocument()
                result = ((IXPathNavigable)Compiler.LoadDocument(tr)).CreateNavigator();
            }
            else if (input is XPathNavigator)
            {
                result = (XPathNavigator)input;
            }
            else
            {
                throw XsltException.Create(SR.Xslt_CantResolve, ruri.ToString());
            }

            _documentCache[ruri] = result !.Clone();
            return(result);
        }
Exemplo n.º 3
0
        /*
         * CompileTopLevelElements
         */
        protected void CompileDocument(Compiler compiler, bool inInclude)
        {
            NavigatorInput input = compiler.Input;

            // SkipToElement :
            while (input.NodeType != XPathNodeType.Element)
            {
                if (!compiler.Advance())
                {
                    throw XsltException.Create(SR.Xslt_WrongStylesheetElement);
                }
            }

            Debug.Assert(compiler.Input.NodeType == XPathNodeType.Element);
            if (Ref.Equal(input.NamespaceURI, input.Atoms.UriXsl))
            {
                if (
                    !Ref.Equal(input.LocalName, input.Atoms.Stylesheet) &&
                    !Ref.Equal(input.LocalName, input.Atoms.Transform)
                    )
                {
                    throw XsltException.Create(SR.Xslt_WrongStylesheetElement);
                }
                compiler.PushNamespaceScope();
                CompileStylesheetAttributes(compiler);
                CompileTopLevelElements(compiler);
                if (!inInclude)
                {
                    CompileImports(compiler);
                }
            }
            else
            {
                // single template
                compiler.PushLiteralScope();
                CompileSingleTemplate(compiler);
            }

            compiler.PopScope();
        }
Exemplo n.º 4
0
        internal string GetSingleAttribute(string attributeAtom)
        {
            NavigatorInput input   = Input;
            string         element = input.LocalName;
            string         value   = null;

            if (input.MoveToFirstAttribute())
            {
                do
                {
                    string nspace = input.NamespaceURI;
                    string name   = input.LocalName;

                    if (nspace.Length != 0)
                    {
                        continue;
                    }

                    if (Ref.Equal(name, attributeAtom))
                    {
                        value = input.Value;
                    }
                    else
                    {
                        if (!this.ForwardCompatibility)
                        {
                            throw XsltException.Create(SR.Xslt_InvalidAttribute, name, element);
                        }
                    }
                }while (input.MoveToNextAttribute());
                input.ToParent();
            }

            if (value == null)
            {
                throw XsltException.Create(SR.Xslt_MissingAttribute, attributeAtom);
            }
            return(value);
        }
        internal object GetVariableValue(VariableAction variable)
        {
            int variablekey = variable.VarKey;

            if (variable.IsGlobal)
            {
                ActionFrame rootFrame = (ActionFrame)this.actionStack[0];
                object      result    = rootFrame.GetVariable(variablekey);
                if (result != null)
                {
                    return(result);
                }
                // Variable wasn't evaluated yet
                if (variable.Stylesheetid == -1)
                {
                    throw XsltException.Create(Res.Xslt_CircularReference, variable.NameStr);
                }
                int         saveStackSize = this.actionStack.Length;
                ActionFrame varFrame      = PushNewFrame();
                varFrame.Inherit(rootFrame);
                varFrame.Init(variable, rootFrame.NodeSet);
                do
                {
                    bool endOfFrame = ((ActionFrame)this.actionStack.Peek()).Execute(this);
                    if (endOfFrame)
                    {
                        this.actionStack.Pop();
                    }
                } while (saveStackSize < this.actionStack.Length);
                Debug.Assert(saveStackSize == this.actionStack.Length);
                result = rootFrame.GetVariable(variablekey);
                Debug.Assert(result != null, "Variable was just calculated and result can't be null");
                return(result);
            }
            else
            {
                return(((ActionFrame)this.actionStack.Peek()).GetVariable(variablekey));
            }
        }
Exemplo n.º 6
0
        private static double NameTest(string name)
        {
            if (name == "*")
            {
                return(-0.5);
            }
            int idx = name.Length - 2;

            if (0 <= idx && name[idx] == ':' && name[idx + 1] == '*')
            {
                if (!PrefixQName.ValidatePrefix(name.Substring(0, idx)))
                {
                    throw XsltException.Create(SR.Xslt_InvalidAttrValue, "elements", name);
                }
                return(-0.25);
            }
            else
            {
                PrefixQName.ParseQualifiedName(name, out _, out _);
                return(0);
            }
        }
Exemplo n.º 7
0
        private DecimalFormat ResolveFormatName(string formatName)
        {
            string ns = string.Empty, local = string.Empty;

            if (formatName != null)
            {
                string prefix;
                PrefixQName.ParseQualifiedName(formatName, out prefix, out local);
                ns = LookupNamespace(prefix);
            }
            DecimalFormat formatInfo = this.processor.RootAction.GetDecimalFormat(new XmlQualifiedName(local, ns));

            if (formatInfo == null)
            {
                if (formatName != null)
                {
                    throw XsltException.Create(Res.Xslt_NoDecimalFormat, formatName);
                }
                formatInfo = new DecimalFormat(new NumberFormatInfo(), '#', '0', ';');
            }
            return(formatInfo);
        }
Exemplo n.º 8
0
        internal void CompileSingleTemplate(Compiler compiler)
        {
            NavigatorInput input = compiler.Input;

            //
            // find mandatory version attribute and launch compilation of single template
            //

            string?version = null;

            if (input.MoveToFirstAttribute())
            {
                do
                {
                    string nspace = input.NamespaceURI;
                    string name   = input.LocalName;

                    if (Ref.Equal(nspace, input.Atoms.UriXsl) &&
                        Ref.Equal(name, input.Atoms.Version))
                    {
                        version = input.Value;
                    }
                }while (input.MoveToNextAttribute());
                input.ToParent();
            }

            if (version == null)
            {
                if (Ref.Equal(input.LocalName, input.Atoms.Stylesheet) &&
                    input.NamespaceURI == XmlReservedNs.NsWdXsl)
                {
                    throw XsltException.Create(SR.Xslt_WdXslNamespace);
                }
                throw XsltException.Create(SR.Xslt_WrongStylesheetElement);
            }

            compiler.AddTemplate(compiler.CreateSingleTemplateAction());
        }
Exemplo n.º 9
0
        /// <summary>
        /// This is used to get the message strings from an exception and any of its inner exceptions
        /// </summary>
        /// <param name="exception">The exception from which to get the message</param>
        /// <returns>The exception message along with any inner exception messages</returns>
        /// <remarks><see cref="XmlException"/> and <see cref="XsltException"/> messages will be returned with
        /// line number, line position, and source URI information.</remarks>
        /// <exception cref="ArgumentNullException">This is thrown if the <paramref name="exception"/> argument
        /// is null</exception>
        public static string GetExceptionMessage(this Exception exception)
        {
            if (exception == null)
            {
                throw new ArgumentNullException("exception");
            }

            if (exception is AggregateException)
            {
                exception = exception.InnerException;
            }

            string message = exception.Message;

            XmlException xmlE = exception as XmlException;

            if (xmlE != null)
            {
                message = String.Format(CultureInfo.CurrentCulture, "{0} (Line Number: {1}; Line Position: " +
                                        "{2}; Source URI: '{3}')", message, xmlE.LineNumber, xmlE.LinePosition, xmlE.SourceUri);
            }

            XsltException xslE = exception as XsltException;

            if (xslE != null)
            {
                message = String.Format(CultureInfo.CurrentCulture, "{0} (Line Number: {1}; Line Position: " +
                                        "{2}; Source URI: '{3}')", message, xslE.LineNumber, xslE.LinePosition, xslE.SourceUri);
            }

            if (exception.InnerException != null)
            {
                message = String.Format(CultureInfo.CurrentCulture, "{0}\r\nInner Exception: {1}", message,
                                        exception.InnerException.GetExceptionMessage());
            }

            return(message);
        }
Exemplo n.º 10
0
        //
        // Parsing qualified names
        //

        public static void ParseQualifiedName(string qname, out string prefix, out string local)
        {
            Debug.Assert(qname != null);
            prefix = string.Empty;
            local  = string.Empty;

            // parse first NCName (prefix or local name)
            int position = ValidateNames.ParseNCName(qname);

            if (position == 0)
            {
                throw XsltException.Create(SR.Xslt_InvalidQName, qname);
            }
            local = qname.Substring(0, position);

            // not at the end -> parse ':' and the second NCName (local name)
            if (position < qname.Length)
            {
                if (qname[position] == ':')
                {
                    int startLocalNamePos = ++position;
                    prefix = local;
                    int len = ValidateNames.ParseNCName(qname, position);
                    position += len;
                    if (len == 0)
                    {
                        throw XsltException.Create(SR.Xslt_InvalidQName, qname);
                    }
                    local = qname.Substring(startLocalNamePos, len);
                }

                // still not at the end -> error
                if (position < qname.Length)
                {
                    throw XsltException.Create(SR.Xslt_InvalidQName, qname);
                }
            }
        }
Exemplo n.º 11
0
        internal NavigatorInput ResolveDocument(Uri absoluteUri)
        {
            Debug.Assert(this.xmlResolver != null);
            object input    = this.xmlResolver.GetEntity(absoluteUri, null, null);
            string resolved = absoluteUri.ToString();

            if (input is Stream)
            {
                XmlTextReaderImpl tr = new XmlTextReaderImpl(resolved, (Stream)input); {
                    tr.XmlResolver = this.xmlResolver;
                }
                // reader is closed by Compiler.LoadDocument()
                return(new NavigatorInput(Compiler.LoadDocument(tr).CreateNavigator(), resolved, rootScope));
            }
            else if (input is XPathNavigator)
            {
                return(new NavigatorInput((XPathNavigator)input, resolved, rootScope));
            }
            else
            {
                throw XsltException.Create(Res.Xslt_CantResolve, resolved);
            }
        }
Exemplo n.º 12
0
        public void CheckEmpty(Compiler compiler)
        {
            // Really EMPTY means no content at all, but the sake of compatibility with MSXML we allow whitespaces
            string elementName = compiler.Input.Name;

            if (compiler.Recurse())
            {
                do
                {
                    // Note: <![CDATA[ ]]> will be reported as XPathNodeType.Text
                    XPathNodeType nodeType = compiler.Input.NodeType;
                    if (
                        nodeType != XPathNodeType.Whitespace &&
                        nodeType != XPathNodeType.Comment &&
                        nodeType != XPathNodeType.ProcessingInstruction
                        )
                    {
                        throw XsltException.Create(Res.Xslt_NotEmptyContents, elementName);
                    }
                }while (compiler.Advance());
                compiler.ToParent();
            }
        }
Exemplo n.º 13
0
        private string ResolveNonEmptyPrefix(string prefix)
        {
            Debug.Assert(_scopeStack != null, "PushScope wasn't called");
            Debug.Assert(!string.IsNullOrEmpty(prefix));
            if (prefix == "xml")
            {
                return(XmlReservedNs.NsXml);
            }
            else if (prefix == "xmlns")
            {
                return(XmlReservedNs.NsXmlNs);
            }

            for (InputScope inputScope = _scopeStack; inputScope != null; inputScope = inputScope.Parent)
            {
                string nspace = inputScope.ResolveNonAtom(prefix);
                if (nspace != null)
                {
                    return(nspace);
                }
            }
            throw XsltException.Create(SR.Xslt_InvalidPrefix, prefix);
        }
        double NameTest(String name)
        {
            if (name == "*")
            {
                return(-0.5);
            }
            int idx = name.Length - 2;

            if (0 <= idx && name[idx] == ':' && name[idx + 1] == '*')
            {
                if (!PrefixQName.ValidatePrefix(name.Substring(0, idx)))
                {
                    throw XsltException.Create(Res.Xslt_InvalidAttrValue, Keywords.s_Elements, name);
                }
                return(-0.25);
            }
            else
            {
                string prefix, localname;
                PrefixQName.ParseQualifiedName(name, out prefix, out localname);
                return(0);
            }
        }
Exemplo n.º 15
0
 public virtual void Evaluate(XslTransformProcessor p, Hashtable withParams)
 {
     if (XslTransform.TemplateStackFrameError)
     {
         try {
             EvaluateCore(p, withParams);
         } catch (XsltException ex) {
             AppendTemplateFrame(ex);
             throw ex;
         } catch (Exception ex) {
             // Note that this catch causes different
             // type of error to be thrown (esp.
             // this causes NUnit test regression).
             XsltException e = new XsltException("Error during XSLT processing: ", null, p.CurrentNode);
             AppendTemplateFrame(e);
             throw e;
         }
     }
     else
     {
         EvaluateCore(p, withParams);
     }
 }
Exemplo n.º 16
0
        private string ResolveNonEmptyPrefix(string prefix)
        {
            Debug.Assert(this.scopeStack != null, "PushScope wasn't called");
            Debug.Assert(prefix != null || prefix.Length != 0);
            if (prefix == Keywords.s_Xml)
            {
                return(Keywords.s_XmlNamespace);
            }
            else if (prefix == Keywords.s_Xmlns)
            {
                return(Keywords.s_XmlnsNamespace);
            }

            for (InputScope inputScope = this.scopeStack; inputScope != null; inputScope = inputScope.Parent)
            {
                string nspace = inputScope.ResolveNonAtom(prefix);
                if (nspace != null)
                {
                    return(nspace);
                }
            }
            throw XsltException.Create(Res.Xslt_InvalidPrefix, prefix);
        }
        //
        // Parsing qualified names
        //

        private static string ParseNCName(string qname, ref int position)
        {
            int         qnameLength = qname.Length;
            int         nameStart   = position;
            XmlCharType xmlCharType = XmlCharType.Instance;

            if (
                qnameLength == position ||                           // Zero length ncname
                !xmlCharType.IsStartNCNameChar(qname[position])      // Start from invalid char
                )
            {
                throw XsltException.Create(Res.Xslt_InvalidQName, qname);
            }

            position++;

            while (position < qnameLength && xmlCharType.IsNCNameChar(qname[position]))
            {
                position++;
            }

            return(qname.Substring(nameStart, position - nameStart));
        }
Exemplo n.º 18
0
        private string ParseLang(string value)
        {
            if (value == null)
            { // Avt is not constant, or attribute wasn't defined
                return(null);
            }
            // XmlComplianceUtil.IsValidLanguageID uses the outdated RFC 1766. It would be
            // better to remove this method completely and not call it here, but that may
            // change exception types for some stylesheets.
            CultureInfo cultInfo = new CultureInfo(value);

            if (!XmlComplianceUtil.IsValidLanguageID(value.ToCharArray(), 0, value.Length) &&
                (value.Length == 0 || cultInfo == null)
                )
            {
                if (_forwardCompatibility)
                {
                    return(null);
                }
                throw XsltException.Create(SR.Xslt_InvalidAttrValue, "lang", value);
            }
            return(value);
        }
Exemplo n.º 19
0
        public void CompileAttributes(Compiler compiler)
        {
            NavigatorInput input   = compiler.Input;
            string         element = input.LocalName;

            if (input.MoveToFirstAttribute())
            {
                do
                {
                    if (input.NamespaceURI.Length != 0)
                    {
                        continue;
                    }

                    try
                    {
                        if (CompileAttribute(compiler) == false)
                        {
                            throw XsltException.Create(SR.Xslt_InvalidAttribute, input.LocalName, element);
                        }
                    }
                    catch
                    {
                        if (!compiler.ForwardCompatibility)
                        {
                            throw;
                        }
                        else
                        {
                            // In ForwardCompatibility mode we ignoreing all unknown or incorrect attributes
                            // If it's mandatory attribute we'll notice it absence later.
                        }
                    }
                }while (input.MoveToNextAttribute());
                input.ToParent();
            }
        }
Exemplo n.º 20
0
        internal override void Execute(Processor processor, ActionFrame frame)
        {
            Debug.Assert(processor != null && frame != null);
            switch (frame.State)
            {
            case Initialized:
                processor.ResetParams();
                if (this.containedActions != null && this.containedActions.Count > 0)
                {
                    processor.PushActionFrame(frame);
                    frame.State = ProcessedChildren;
                    break;
                }
                goto case ProcessedChildren;

            case ProcessedChildren:
                TemplateAction action = processor.Stylesheet.FindTemplate(this.name);
                if (action != null)
                {
                    frame.State = ProcessedTemplate;
                    processor.PushActionFrame(action, frame.NodeSet);
                    break;
                }
                else
                {
                    throw XsltException.Create(Res.Xslt_InvalidCallTemplate, this.name.ToString());
                }

            case ProcessedTemplate:
                frame.Finished();
                break;

            default:
                Debug.Fail("Invalid CallTemplateAction execution state");
                break;
            }
        }
Exemplo n.º 21
0
        private XmlDataType ParseDataType(string value, InputScopeManager manager)
        {
            if (value == null)
            { // Avt is not constant, or attribute wasn't defined
                return(XmlDataType.Text);
            }
            if (value == "text")
            {
                return(XmlDataType.Text);
            }
            if (value == "number")
            {
                return(XmlDataType.Number);
            }
            String prefix, localname;

            PrefixQName.ParseQualifiedName(value, out prefix, out localname);
            manager.ResolveXmlNamespace(prefix);
            if (prefix.Length == 0 && !_forwardCompatibility)
            {
                throw XsltException.Create(SR.Xslt_InvalidAttrValue, "data-type", value);
            }
            return(XmlDataType.Text);
        }
Exemplo n.º 22
0
        internal override bool CompileAttribute(Compiler compiler)
        {
            string name  = compiler.Input.LocalName;
            string value = compiler.Input.Value;

            if (Ref.Equal(name, compiler.Atoms.Match))
            {
                Debug.Assert(_matchKey == Compiler.InvalidQueryKey);
                _matchKey = compiler.AddQuery(value, /*allowVars:*/ false, /*allowKey:*/ true, /*pattern*/ true);
            }
            else if (Ref.Equal(name, compiler.Atoms.Name))
            {
                Debug.Assert(_name == null);
                _name = compiler.CreateXPathQName(value);
            }
            else if (Ref.Equal(name, compiler.Atoms.Priority))
            {
                Debug.Assert(double.IsNaN(_priority));
                _priority = XmlConvert.ToXPathDouble(value);
                if (double.IsNaN(_priority) && !compiler.ForwardCompatibility)
                {
                    throw XsltException.Create(SR.Xslt_InvalidAttrValue, "priority", value);
                }
            }
            else if (Ref.Equal(name, compiler.Atoms.Mode))
            {
                Debug.Assert(_mode == null);
                _mode = compiler.CreateXPathQName(value);
            }
            else
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 23
0
        // AVT's compilation.
        // CompileAvt() returns ArrayList of TextEvent & AvtEvent

        private static void getTextLex(string avt, ref int start, StringBuilder lex)
        {
            Debug.Assert(avt.Length != start, "Empty string not supposed here");
            Debug.Assert(lex.Length == 0, "Builder shoud to be reset here");
            int avtLength = avt.Length;
            int i;

            for (i = start; i < avtLength; i++)
            {
                char ch = avt[i];
                if (ch == '{')
                {
                    if (i + 1 < avtLength && avt[i + 1] == '{')
                    { // "{{"
                        i++;
                    }
                    else
                    {
                        break;
                    }
                }
                else if (ch == '}')
                {
                    if (i + 1 < avtLength && avt[i + 1] == '}')
                    { // "}}"
                        i++;
                    }
                    else
                    {
                        throw XsltException.Create(SR.Xslt_SingleRightAvt, avt);
                    }
                }
                lex.Append(ch);
            }
            start = i;
        }
        public static void ParseQualifiedName(string qname, out string prefix, out string local)
        {
            Debug.Assert(qname != null);
            prefix = string.Empty;
            local  = string.Empty;
            int position = 0;

            local = ParseNCName(qname, ref position);

            if (position < qname.Length)
            {
                if (qname[position] == ':')
                {
                    position++;
                    prefix = local;
                    local  = ParseNCName(qname, ref position);
                }

                if (position < qname.Length)
                {
                    throw XsltException.Create(Res.Xslt_InvalidQName, qname);
                }
            }
        }
Exemplo n.º 25
0
        public DecimalFormatter(string formatPicture, DecimalFormat decimalFormat)
        {
            Debug.Assert(formatPicture != null && decimalFormat != null);
            if (formatPicture.Length == 0)
            {
                throw XsltException.Create(SR.Xslt_InvalidFormat);
            }

            _zeroDigit     = decimalFormat.zeroDigit;
            _posFormatInfo = (NumberFormatInfo)decimalFormat.info.Clone();
            StringBuilder temp = new StringBuilder();

            bool integer = true;
            bool sawPattern = false, sawZeroDigit = false, sawDigit = false, sawDecimalSeparator = false;
            bool digitOrZeroDigit = false;
            char decimalSeparator = _posFormatInfo.NumberDecimalSeparator[0];
            char groupSeparator   = _posFormatInfo.NumberGroupSeparator[0];
            char percentSymbol    = _posFormatInfo.PercentSymbol[0];
            char perMilleSymbol   = _posFormatInfo.PerMilleSymbol[0];

            int commaIndex     = 0;
            int groupingSize   = 0;
            int decimalIndex   = -1;
            int lastDigitIndex = -1;

            for (int i = 0; i < formatPicture.Length; i++)
            {
                char ch = formatPicture[i];

                if (ch == decimalFormat.digit)
                {
                    if (sawZeroDigit && integer)
                    {
                        throw XsltException.Create(SR.Xslt_InvalidFormat1, formatPicture);
                    }
                    lastDigitIndex = temp.Length;
                    sawDigit       = digitOrZeroDigit = true;
                    temp.Append('#');
                    continue;
                }
                if (ch == decimalFormat.zeroDigit)
                {
                    if (sawDigit && !integer)
                    {
                        throw XsltException.Create(SR.Xslt_InvalidFormat2, formatPicture);
                    }
                    lastDigitIndex = temp.Length;
                    sawZeroDigit   = digitOrZeroDigit = true;
                    temp.Append('0');
                    continue;
                }
                if (ch == decimalFormat.patternSeparator)
                {
                    if (!digitOrZeroDigit)
                    {
                        throw XsltException.Create(SR.Xslt_InvalidFormat8);
                    }
                    if (sawPattern)
                    {
                        throw XsltException.Create(SR.Xslt_InvalidFormat3, formatPicture);
                    }
                    sawPattern = true;

                    if (decimalIndex < 0)
                    {
                        decimalIndex = lastDigitIndex + 1;
                    }
                    groupingSize = RemoveTrailingComma(temp, commaIndex, decimalIndex);

                    if (groupingSize > 9)
                    {
                        groupingSize = 0;
                    }
                    _posFormatInfo.NumberGroupSizes = new int[] { groupingSize };
                    if (!sawDecimalSeparator)
                    {
                        _posFormatInfo.NumberDecimalDigits = 0;
                    }

                    _posFormat = temp.ToString();

                    temp.Length                 = 0;
                    decimalIndex                = -1;
                    lastDigitIndex              = -1;
                    commaIndex                  = 0;
                    sawDigit                    = sawZeroDigit = digitOrZeroDigit = false;
                    sawDecimalSeparator         = false;
                    integer                     = true;
                    _negFormatInfo              = (NumberFormatInfo)decimalFormat.info.Clone();
                    _negFormatInfo.NegativeSign = string.Empty;
                    continue;
                }
                if (ch == decimalSeparator)
                {
                    if (sawDecimalSeparator)
                    {
                        throw XsltException.Create(SR.Xslt_InvalidFormat5, formatPicture);
                    }
                    decimalIndex        = temp.Length;
                    sawDecimalSeparator = true;
                    sawDigit            = sawZeroDigit = integer = false;
                    temp.Append('.');
                    continue;
                }
                if (ch == groupSeparator)
                {
                    commaIndex     = temp.Length;
                    lastDigitIndex = commaIndex;
                    temp.Append(',');
                    continue;
                }
                if (ch == percentSymbol)
                {
                    temp.Append('%');
                    continue;
                }
                if (ch == perMilleSymbol)
                {
                    temp.Append('\u2030');
                    continue;
                }
                if (ch == '\'')
                {
                    int pos = formatPicture.IndexOf('\'', i + 1);
                    if (pos < 0)
                    {
                        pos = formatPicture.Length - 1;
                    }
                    temp.Append(formatPicture, i, pos - i + 1);
                    i = pos;
                    continue;
                }
                // Escape literal digits with EscChar, double literal EscChar
                if ('0' <= ch && ch <= '9' || ch == EscChar)
                {
                    if (decimalFormat.zeroDigit != '0')
                    {
                        temp.Append(EscChar);
                    }
                }
                // Escape characters having special meaning for CLR
                if (ClrSpecialChars.IndexOf(ch) >= 0)
                {
                    temp.Append('\\');
                }
                temp.Append(ch);
            }

            if (!digitOrZeroDigit)
            {
                throw XsltException.Create(SR.Xslt_InvalidFormat8);
            }
            NumberFormatInfo formatInfo = sawPattern ? _negFormatInfo : _posFormatInfo;

            if (decimalIndex < 0)
            {
                decimalIndex = lastDigitIndex + 1;
            }
            groupingSize = RemoveTrailingComma(temp, commaIndex, decimalIndex);
            if (groupingSize > 9)
            {
                groupingSize = 0;
            }
            formatInfo.NumberGroupSizes = new int[] { groupingSize };
            if (!sawDecimalSeparator)
            {
                formatInfo.NumberDecimalDigits = 0;
            }

            if (sawPattern)
            {
                _negFormat = temp.ToString();
            }
            else
            {
                _posFormat = temp.ToString();
            }
        }
Exemplo n.º 26
0
 /// <summary>
 /// Constructor from an XSLT exception
 /// </summary>
 /// <param name="exception">The exception.</param>
 public CCPAPIResult(XsltException exception)
     : this(exception as Exception)
 {
     m_error = APIErrorType.Xml;
 }
Exemplo n.º 27
0
        private void AddScript(Compiler compiler)
        {
            NavigatorInput input = compiler.Input;

            ScriptingLanguage lang = ScriptingLanguage.JScript;
            string?           implementsNamespace = null;

            if (input.MoveToFirstAttribute())
            {
                do
                {
                    if (input.LocalName == input.Atoms.Language)
                    {
                        string langName = input.Value;
                        if (
                            string.Equals(langName, "jscript", StringComparison.OrdinalIgnoreCase) ||
                            string.Equals(langName, "javascript", StringComparison.OrdinalIgnoreCase)
                            )
                        {
                            lang = ScriptingLanguage.JScript;
                        }
                        else if (
                            string.Equals(langName, "c#", StringComparison.OrdinalIgnoreCase) ||
                            string.Equals(langName, "csharp", StringComparison.OrdinalIgnoreCase)
                            )
                        {
                            lang = ScriptingLanguage.CSharp;
                        }
                        else if (
                            string.Equals(langName, "vb", StringComparison.OrdinalIgnoreCase) ||
                            string.Equals(langName, "visualbasic", StringComparison.OrdinalIgnoreCase)
                            )
                        {
                            lang = ScriptingLanguage.VisualBasic;
                        }
                        else
                        {
                            throw XsltException.Create(SR.Xslt_ScriptInvalidLanguage, langName);
                        }
                    }
                    else if (input.LocalName == input.Atoms.ImplementsPrefix)
                    {
                        if (!PrefixQName.ValidatePrefix(input.Value))
                        {
                            throw XsltException.Create(SR.Xslt_InvalidAttrValue, input.LocalName, input.Value);
                        }
                        implementsNamespace = compiler.ResolveXmlNamespace(input.Value);
                    }
                }while (input.MoveToNextAttribute());
                input.ToParent();
            }
            if (implementsNamespace == null)
            {
                throw XsltException.Create(SR.Xslt_MissingAttribute, input.Atoms.ImplementsPrefix);
            }
            if (!input.Recurse() || input.NodeType != XPathNodeType.Text)
            {
                throw XsltException.Create(SR.Xslt_ScriptEmpty);
            }
            compiler.AddScript(input.Value, lang, implementsNamespace, input.BaseURI, input.LineNumber);
            input.ToParent();
        }
Exemplo n.º 28
0
        // SxS: This method does not take any resource name and does not expose any resources to the caller.
        // It's OK to suppress the SxS warning.
        protected void CompileTopLevelElements(Compiler compiler)
        {
            // Navigator positioned at parent root, need to move to child and then back
            if (compiler.Recurse() == false)
            {
                return;
            }

            NavigatorInput input           = compiler.Input;
            bool           notFirstElement = false;

            do
            {
                switch (input.NodeType)
                {
                case XPathNodeType.Element:
                    string name   = input.LocalName;
                    string nspace = input.NamespaceURI;

                    if (Ref.Equal(nspace, input.Atoms.UriXsl))
                    {
                        if (Ref.Equal(name, input.Atoms.Import))
                        {
                            if (notFirstElement)
                            {
                                throw XsltException.Create(SR.Xslt_NotFirstImport);
                            }
                            // We should compile imports in reverse order after all toplevel elements.
                            // remember it now and return to it in CompileImpoorts();
                            Uri    uri      = compiler.ResolveUri(compiler.GetSingleAttribute(compiler.Input.Atoms.Href));
                            string resolved = uri.ToString();
                            if (compiler.IsCircularReference(resolved))
                            {
                                throw XsltException.Create(SR.Xslt_CircularInclude, resolved);
                            }
                            compiler.CompiledStylesheet !.Imports.Add(uri);
                            CheckEmpty(compiler);
                        }
                        else if (Ref.Equal(name, input.Atoms.Include))
                        {
                            notFirstElement = true;
                            CompileInclude(compiler);
                        }
                        else
                        {
                            notFirstElement = true;
                            compiler.PushNamespaceScope();
                            if (Ref.Equal(name, input.Atoms.StripSpace))
                            {
                                CompileSpace(compiler, false);
                            }
                            else if (Ref.Equal(name, input.Atoms.PreserveSpace))
                            {
                                CompileSpace(compiler, true);
                            }
                            else if (Ref.Equal(name, input.Atoms.Output))
                            {
                                CompileOutput(compiler);
                            }
                            else if (Ref.Equal(name, input.Atoms.Key))
                            {
                                CompileKey(compiler);
                            }
                            else if (Ref.Equal(name, input.Atoms.DecimalFormat))
                            {
                                CompileDecimalFormat(compiler);
                            }
                            else if (Ref.Equal(name, input.Atoms.NamespaceAlias))
                            {
                                CompileNamespaceAlias(compiler);
                            }
                            else if (Ref.Equal(name, input.Atoms.AttributeSet))
                            {
                                compiler.AddAttributeSet(compiler.CreateAttributeSetAction());
                            }
                            else if (Ref.Equal(name, input.Atoms.Variable))
                            {
                                VariableAction?action = compiler.CreateVariableAction(VariableType.GlobalVariable);
                                if (action != null)
                                {
                                    AddAction(action);
                                }
                            }
                            else if (Ref.Equal(name, input.Atoms.Param))
                            {
                                VariableAction?action = compiler.CreateVariableAction(VariableType.GlobalParameter);
                                if (action != null)
                                {
                                    AddAction(action);
                                }
                            }
                            else if (Ref.Equal(name, input.Atoms.Template))
                            {
                                compiler.AddTemplate(compiler.CreateTemplateAction());
                            }
                            else
                            {
                                if (!compiler.ForwardCompatibility)
                                {
                                    throw compiler.UnexpectedKeyword();
                                }
                            }
                            compiler.PopScope();
                        }
                    }
                    else if (nspace == input.Atoms.UrnMsxsl && name == input.Atoms.Script)
                    {
                        AddScript(compiler);
                    }
                    else
                    {
                        if (nspace.Length == 0)
                        {
                            throw XsltException.Create(SR.Xslt_NullNsAtTopLevel, input.Name);
                        }
                        // Ignoring non-recognized namespace per XSLT spec 2.2
                    }
                    break;

                case XPathNodeType.ProcessingInstruction:
                case XPathNodeType.Comment:
                case XPathNodeType.Whitespace:
                case XPathNodeType.SignificantWhitespace:
                    break;

                default:
                    throw XsltException.Create(SR.Xslt_InvalidContents, "stylesheet");
                }
            }while (compiler.Advance());

            compiler.ToParent();
        }
Exemplo n.º 29
0
        internal void CompileStylesheetAttributes(Compiler compiler)
        {
            NavigatorInput input        = compiler.Input;
            string         element      = input.LocalName;
            string?        badAttribute = null;
            string?        version      = null;

            if (input.MoveToFirstAttribute())
            {
                do
                {
                    string nspace = input.NamespaceURI;
                    string name   = input.LocalName;

                    if (nspace.Length != 0)
                    {
                        continue;
                    }

                    if (Ref.Equal(name, input.Atoms.Version))
                    {
                        version = input.Value;
                        if (1 <= XmlConvert.ToXPathDouble(version))
                        {
                            compiler.ForwardCompatibility = (version != "1.0");
                        }
                        else
                        {
                            // XmlConvert.ToXPathDouble(version) an be NaN!
                            if (!compiler.ForwardCompatibility)
                            {
                                throw XsltException.Create(SR.Xslt_InvalidAttrValue, "version", version);
                            }
                        }
                    }
                    else if (Ref.Equal(name, input.Atoms.ExtensionElementPrefixes))
                    {
                        compiler.InsertExtensionNamespace(input.Value);
                    }
                    else if (Ref.Equal(name, input.Atoms.ExcludeResultPrefixes))
                    {
                        compiler.InsertExcludedNamespace(input.Value);
                    }
                    else if (Ref.Equal(name, input.Atoms.Id))
                    {
                        // Do nothing here.
                    }
                    else
                    {
                        // We can have version attribute later. For now remember this attribute and continue
                        badAttribute = name;
                    }
                }while (input.MoveToNextAttribute());
                input.ToParent();
            }

            if (version == null)
            {
                throw XsltException.Create(SR.Xslt_MissingAttribute, "version");
            }

            if (badAttribute != null && !compiler.ForwardCompatibility)
            {
                throw XsltException.Create(SR.Xslt_InvalidAttribute, badAttribute, element);
            }
        }
Exemplo n.º 30
0
        private static void getXPathLex(string avt, ref int start, StringBuilder lex)
        {
            // AVT parser states
            const int InExp      = 0; // Inside AVT expression
            const int InLiteralA = 1; // Inside literal in expression, apostrophe delimited
            const int InLiteralQ = 2; // Inside literal in expression, quote delimited

            Debug.Assert(avt.Length != start, "Empty string not supposed here");
            Debug.Assert(lex.Length == 0, "Builder shoud to be reset here");
            Debug.Assert(avt[start] == '{', "We calling getXPathLex() only after we realy meet {");
            int avtLength = avt.Length;
            int state     = InExp;

            for (int i = start + 1; i < avtLength; i++)
            {
                char ch = avt[i];
                switch (state)
                {
                case InExp:
                    switch (ch)
                    {
                    case '{':
                        throw XsltException.Create(SR.Xslt_NestedAvt, avt);

                    case '}':
                        i++;      // include '}'
                        if (i == start + 2)
                        {         // empty XPathExpresion
                            throw XsltException.Create(SR.Xslt_EmptyAvtExpr, avt);
                        }
                        lex.Append(avt, start + 1, i - start - 2);         // avt without {}
                        start = i;
                        return;

                    case '\'':
                        state = InLiteralA;
                        break;

                    case '"':
                        state = InLiteralQ;
                        break;
                    }
                    break;

                case InLiteralA:
                    if (ch == '\'')
                    {
                        state = InExp;
                    }
                    break;

                case InLiteralQ:
                    if (ch == '"')
                    {
                        state = InExp;
                    }
                    break;
                }
            }

            // if we meet end of string before } we have an error
            throw XsltException.Create(state == InExp ? SR.Xslt_OpenBracesAvt : SR.Xslt_OpenLiteralAvt, avt);
        }