///	<summary>
        ///	Parses command line	arguments into NXsltOptions collection.
        ///	</summary>
        ///	<param name="args">Command line	arguments.</param>
        ///	<returns>Parsed	collection of options.</returns>
        public NXsltOptions ParseArguments(string[] args)
        {
            NXsltOptions options = new NXsltOptions();

            ParseArguments(args, options);
            return(options);
        }
Exemple #2
0
        /// <summary>
        /// Pretty prints XML document using XmlWriter's formatting functionality.
        /// </summary>
        /// <param name="reader">Source XML reader</param>
        /// <param name="options">Parsed command line options</param>
        public static void PrettyPrint(XmlReader reader, NXsltOptions options)
        {
            XmlWriterSettings writerSettings = new XmlWriterSettings();

            writerSettings.Indent = true;
            writerSettings.NewLineOnAttributes = true;
            writerSettings.Encoding            = new UTF8Encoding(false);
            XmlWriter writer;

            if (options.OutFile != null)
            {
                //Pretty print to a file
                writer = XmlWriter.Create(options.OutFile, writerSettings);
            }
            else
            {
                //Pretty print to the console
                writer = XmlWriter.Create(Console.Out, writerSettings);
            }
            while (reader.ReadState != ReadState.EndOfFile)
            {
                writer.WriteNode(reader, false);
            }
            writer.Close();
            reader.Close();
        }
Exemple #3
0
        /// <summary>
        /// Gets XmlResolver - default or custom, with user credentials or not.
        /// </summary>
        /// <param name="credentials">User credentials</param>
        /// <param name="options">Parsed command line options</param>
        public static XmlResolver GetXmlResolver(NetworkCredential credentials, NXsltOptions options, bool prohibitDTD)
        {
            XmlResolver resolver;
            Type        resolverType;

            if (options.ResolverTypeName != null)
            {
                //Custom resolver
                try
                {
                    resolverType = TypeUtils.FindType(options, options.ResolverTypeName);
                }
                catch (Exception e)
                {
                    throw new NXsltException(NXsltStrings.ErrorCreateResolver, options.ResolverTypeName, e.Message);
                }
                if (!typeof(XmlResolver).IsAssignableFrom(resolverType))
                {
                    //Type is not XmlResolver
                    throw new NXsltException(NXsltStrings.ErrorTypeNotXmlResolver, options.ResolverTypeName);
                }
                try
                {
                    resolver = (XmlResolver)Activator.CreateInstance(resolverType);
                }
                catch (Exception e)
                {
                    throw new NXsltException(NXsltStrings.ErrorCreateResolver, options.ResolverTypeName, e.Message);
                }
            }
            else
            {
                if (prohibitDTD)
                {
                    resolver = new XmlUrlResolver();
                }
                else
                {
                    resolver = new DTDAllowingResolver();
                }
            }
            //Set credentials if any
            if (credentials != null)
            {
                resolver.Credentials = credentials;
            }
            return(resolver);
        }
Exemple #4
0
 /// <summary>
 /// Gets XmlResolver - default or custom, with user credentials or not.
 /// </summary>
 /// <param name="credentials">User credentials</param>    
 /// <param name="options">Parsed command line options</param>
 public static XmlResolver GetXmlResolver(NetworkCredential credentials, NXsltOptions options, bool prohibitDTD)
 {
     XmlResolver resolver;
     Type resolverType;
     if (options.ResolverTypeName != null)
     {
         //Custom resolver
         try
         {
             resolverType = TypeUtils.FindType(options, options.ResolverTypeName);
         }
         catch (Exception e)
         {
             throw new NXsltException(NXsltStrings.ErrorCreateResolver, options.ResolverTypeName, e.Message);
         }
         if (!typeof(XmlResolver).IsAssignableFrom(resolverType))
         {
             //Type is not XmlResolver
             throw new NXsltException(NXsltStrings.ErrorTypeNotXmlResolver, options.ResolverTypeName);
         }
         try
         {
             resolver = (XmlResolver)Activator.CreateInstance(resolverType);
         }
         catch (Exception e)
         {
             throw new NXsltException(NXsltStrings.ErrorCreateResolver, options.ResolverTypeName, e.Message);
         }
     }
     else
     {
         if (prohibitDTD)
         {
             resolver = new XmlUrlResolver();
         }
         else
         {
             resolver = new DTDAllowingResolver();
         }
     }
     //Set credentials if any
     if (credentials != null)
     {
         resolver.Credentials = credentials;
     }
     return resolver;
 }
        }// ParseNamespaceDeclaration

        /// <summary>
        /// Resolves enqueued parameters.
        /// </summary>
        /// <param name="options">nxslt options collection</param>
        private void ResolveParameters(NXsltOptions options)
        {
            if (paramQueue != null)
            {
                options.XslArgList = new XsltArgumentList();
                foreach (string param in paramQueue)
                {
                    int eqIndex = param.IndexOf('=');
                    //qname is prefix:localname or localname
                    string qname      = param.Substring(0, eqIndex);
                    int    colonIndex = qname.IndexOf(':');
                    if (colonIndex != -1)
                    {
                        //prefix:localname="value" case
                        string prefix = qname.Substring(0, colonIndex);
                        string uri;
                        //Resolve prefix
                        if (namespaceManager == null ||
                            (uri = namespaceManager.LookupNamespace(nameTable.Get(prefix))) == null)
                        {
                            throw new NXsltCommandLineParsingException(NXsltStrings.ErrorUnboundedPrefix, prefix);
                        }
                        else
                        {
                            options.XslArgList.AddParam(param.Substring(colonIndex + 1,
                                                                        eqIndex - colonIndex - 1), uri, param.Substring(eqIndex + 1));
                        }
                    }
                    else
                    {
                        //localname="value"	case - possible default namespace
                        options.XslArgList.AddParam(qname, namespaceManager == null ? String.Empty : namespaceManager.DefaultNamespace,
                                                    param.Substring(eqIndex + 1));
                    }
                }
            }
        }// ResolveParameters
Exemple #6
0
 /// <summary>
 /// Pretty prints XML document using XmlWriter's formatting functionality.
 /// </summary>
 /// <param name="reader">Source XML reader</param>
 /// <param name="options">Parsed command line options</param>
 public static void PrettyPrint(XmlReader reader, NXsltOptions options)
 {
     XmlWriterSettings writerSettings = new XmlWriterSettings();            
     writerSettings.Indent = true;
     writerSettings.NewLineOnAttributes = true;
     writerSettings.Encoding = new UTF8Encoding(false);            
     XmlWriter writer;
     if (options.OutFile != null)
     {
         //Pretty print to a file                
         writer = XmlWriter.Create(options.OutFile, writerSettings);
     }
     else
     {
         //Pretty print to the console                                
         writer = XmlWriter.Create(Console.Out, writerSettings);
     }
     while (reader.ReadState != ReadState.EndOfFile)
     {               
         writer.WriteNode(reader, false);
     }
     writer.Close();
     reader.Close();
 }
        }// ResolveParameters

        /// <summary>
        /// Parses and instantiates extention objects.
        /// </summary>
        /// <param name="options">nxslt options collection</param>
        private void ParseExtObjects(NXsltOptions options)
        {
            if (options.ExtClasses != null)
            {
                if (options.XslArgList == null)
                {
                    options.XslArgList = new XsltArgumentList();
                }
                foreach (string typeDecl in options.ExtClasses)
                {
                    string[] parts = typeDecl.Split(new char[] { ':' }, 2);
                    if (parts.Length == 1)
                    {
                        Type extObjType = TypeUtils.FindType(options, parts[0]);
                        try
                        {
                            object o = Activator.CreateInstance(extObjType);
                            if (namespaceManager == null || namespaceManager.DefaultNamespace == String.Empty)
                            {
                                //Extension object not bound to a namespace
                                throw new NXsltException(NXsltStrings.ErrorExtNoNamespace, parts[0]);
                            }
                            string ns = namespaceManager.DefaultNamespace;
                            if (options.XslArgList.GetExtensionObject(ns) != null)
                            {
                                //More than one extension object in the same namespace URI
                                throw new NXsltException(NXsltStrings.ErrorExtNamespaceClash, ns);
                            }
                            options.XslArgList.AddExtensionObject(ns, o);
                        }
                        catch (Exception e)
                        {
                            //Type cannot be instantiated
                            throw new NXsltException(NXsltStrings.ErrorCreateResolver, parts[0], e.Message);
                        }
                    }
                    else
                    {
                        string prefix = parts[0];
                        string uri;
                        //Resolve prefix
                        if (namespaceManager == null ||
                            (uri = namespaceManager.LookupNamespace(nameTable.Get(prefix))) == null)
                        {
                            throw new NXsltCommandLineParsingException(NXsltStrings.ErrorUnboundedPrefix, prefix);
                        }

                        Type extObjType = TypeUtils.FindType(options, parts[1]);
                        try
                        {
                            object o = Activator.CreateInstance(extObjType);
                            if (options.XslArgList.GetExtensionObject(uri) != null)
                            {
                                //More than one extension object in the same namespace URI
                                throw new NXsltCommandLineParsingException(NXsltStrings.ErrorExtNamespaceClash, uri);
                            }
                            options.XslArgList.AddExtensionObject(uri, o);
                        }
                        catch (Exception e)
                        {
                            //Type cannot be instantiated
                            throw new NXsltCommandLineParsingException(NXsltStrings.ErrorCreateResolver, parts[1], e.Message);
                        }
                    }
                }
            }
        } // ParseExtObjects
        ///	<summary>
        ///	Parses command line	arguments into NXsltOptions collection.
        ///	</summary>
        ///	<param name="args">Command line	arguments.</param>
        ///	<returns>Parsed	collection of options.</returns>
        public NXsltOptions ParseArguments(string[] args)
        {
            NXsltOptions options = new NXsltOptions();

            for (int i = 0; i < args.Length; i++)
            {
                string arg = args[i];
                switch (arg)
                {
                //Show help
                case "-?":
                    options.ShowHelp = true;
                    break;

                //Strip whitespace
                case "-xw":
                    options.StripWhiteSpace = true;
                    break;

                //Output filename
                case "-o":
                    if (i == args.Length - 1)
                    {
                        //Absent file name
                        throw new NXsltCommandLineParsingException(NXsltStrings.ErrorMissingOutFileName);
                    }
                    else
                    {
                        //Next argument must be filename
                        options.OutFile = args[++i];
                        break;
                    }

                //Show timings
                case "-t":
                    options.ShowTiming = true;
                    break;

                //No source XML
                case "-xs":
                    if (options.GetStylesheetFromPI)
                    {
                        //Both -xs and -pi cannot be specified
                        throw new NXsltCommandLineParsingException(NXsltStrings.ErrorBothNoSourceAndPI);
                    }
                    else if ((options.Source != null && options.Stylesheet != null) ||
                             options.LoadSourceFromStdin)
                    {
                        //When using -xs no source shoold be specified
                        throw new NXsltCommandLineParsingException(NXsltStrings.ErrorBothNoSourceAndSource);
                    }
                    else if (options.Source != null && options.Stylesheet == null)
                    {
                        //That was stylesheet, not source
                        options.Stylesheet = options.Source;
                        options.Source     = null;
                    }
                    options.NoSourceXml = true;
                    break;

                //Get stylesheet URI from xml-stylesheet PI
                case "-pi":
                    if (options.Stylesheet != null || options.LoadStylesheetFromStdin)
                    {
                        //Both -pi and stylesheet cannot be specified
                        throw new NXsltCommandLineParsingException(NXsltStrings.ErrorBothStylesheetAndPI);
                    }
                    else if (options.NoSourceXml)
                    {
                        //Both -xs and -pi cannot be specified
                        throw new NXsltCommandLineParsingException(NXsltStrings.ErrorBothNoSourceAndPI);
                    }
                    else
                    {
                        options.GetStylesheetFromPI = true;
                    }
                    break;

                //Network credentials for source XML
                case "-xmlc":
                    if (i == args.Length - 1)
                    {
                        //Absent credentials
                        throw new NXsltCommandLineParsingException(NXsltStrings.ErrorMissingXMLCredentials);
                    }
                    else
                    {
                        //Next argument must be credentials
                        string credstring = args[++i];
                        if (options.SourceCredential != null)
                        {
                            //Duplicate credentials
                            throw new NXsltCommandLineParsingException(NXsltStrings.ErrorDuplicateCredentials);
                        }
                        options.SourceCredential = ParseCredentials(credstring);
                        break;
                    }

                //Network credentials for stylesheet
                case "-xslc":
                    if (i == args.Length - 1)
                    {
                        //Absent credentials
                        throw new NXsltCommandLineParsingException(NXsltStrings.ErrorMissingXSLTCredentials);
                    }
                    else
                    {
                        //Next argument must be credentials
                        string credstring = args[++i];
                        if (options.XSLTCredential != null)
                        {
                            //Duplicate credentials
                            throw new NXsltCommandLineParsingException(NXsltStrings.ErrorDuplicateCredentials);
                        }
                        options.XSLTCredential = ParseCredentials(credstring);
                        break;
                    }

                //Use named URI resolver type
                case "-r":
                    if (i == args.Length - 1)
                    {
                        //Absent resolver type name
                        throw new NXsltCommandLineParsingException(NXsltStrings.ErrorMissingResolverTypeName);
                    }
                    else
                    {
                        //Next argument must be resolver type
                        options.ResolverTypeName = args[++i];
                        break;
                    }

                //Assembly file name
                case "-af":
                    if (options.AssemblyName != null)
                    {
                        throw new NXsltCommandLineParsingException(NXsltStrings.ErrorBothAssemblyFileNameAndName);
                    }
                    if (i == args.Length - 1)
                    {
                        //Absent assembly file name
                        throw new NXsltCommandLineParsingException(NXsltStrings.ErrorMissingAssemblyFileName);
                    }
                    else
                    {
                        //Next argument must be assembly file name
                        options.AssemblyFileName = args[++i];
                        break;
                    }

                //Assembly name
                case "-an":
                    if (options.AssemblyFileName != null)
                    {
                        throw new NXsltCommandLineParsingException(NXsltStrings.ErrorBothAssemblyFileNameAndName);
                    }
                    if (i == args.Length - 1)
                    {
                        //Absent assembly name
                        throw new NXsltCommandLineParsingException(NXsltStrings.ErrorMissingAssemblyName);
                    }
                    else
                    {
                        //Next argument must be assembly name
                        options.AssemblyName = args[++i];
                        break;
                    }

                //Allow multiple output documents
                case "-mo":
                    options.MultiOutput = true;
                    break;

                //Validate documents
                case "-v":
                    options.ValidateDocs = true;
                    break;

                //Do not resolve externals
                case "-xe":
                    options.ResolveExternals = false;
                    break;

                //Do not process XInclude
                case "-xi":
                    options.ProcessXInclude = false;
                    break;

                //Pretty print source XML
                case "-pp":
                    options.PrettyPrintMode = true;
                    break;

                //Extension class names
                case "-ext":
                    if (i == args.Length - 1)
                    {
                        //Absent class names
                        throw new NXsltCommandLineParsingException(NXsltStrings.ErrorMissingExtClassNames);
                    }
                    else
                    {
                        //Next argument must be ext class names list
                        options.ExtClasses = args[++i].Split(',');
                        break;
                    }

                //Source or Stylesheet to be read from stdin
                case "-":
                    if (options.Source == null && !options.LoadSourceFromStdin)
                    {
                        options.LoadSourceFromStdin = true;
                    }
                    else if (options.Stylesheet == null && !options.LoadStylesheetFromStdin)
                    {
                        //Check out that both source and stylesheet are not "-"
                        if (options.LoadSourceFromStdin)
                        {
                            //Both source and stylesheet cannot be read from stdin
                            throw new NXsltCommandLineParsingException(NXsltStrings.ErrorBothStdin);
                        }
                        else
                        {
                            options.LoadStylesheetFromStdin = true;
                        }
                    }
                    else
                    {
                        //Unrecognized "-"
                        throw new NXsltCommandLineParsingException(NXsltStrings.ErrorUnrecognizedOption, arg);
                    }
                    break;

                //Other argment - source URI, stylesheet URI, parameter or namespace declaration
                default:
                    //namespace declaration?
                    if ((arg.StartsWith("xmlns:") || arg.StartsWith("xmlns=")) && arg.Contains("="))
                    {
                        ParseNamespaceDeclaration(arg);
                    }
                    //Parameter?
                    else if (arg.Contains("="))
                    {
                        //Parameter - put to the queue till all namespaces are not processed
                        if (arg.StartsWith("="))
                        {
                            throw new NXsltCommandLineParsingException(NXsltStrings.ErrorMissingName);
                        }
                        else if (arg.EndsWith("="))
                        {
                            throw new NXsltCommandLineParsingException(NXsltStrings.ErrorMissingValue, arg.Remove(arg.Length - 1, 1));
                        }
                        //Enqueue param till all namespace declarations parsed
                        EnqueueParameter(args, arg);
                    }
                    else if (arg.StartsWith("-"))
                    {
                        //Unrecognized option
                        throw new NXsltCommandLineParsingException(NXsltStrings.ErrorUnrecognizedOption, arg);
                    }
                    //Source URI?
                    else if (options.Source == null && !options.LoadSourceFromStdin && !options.NoSourceXml)
                    {
                        options.Source = arg;
                    }
                    //Stylesheet URI?
                    else if (options.Stylesheet == null && !options.LoadStylesheetFromStdin && !options.GetStylesheetFromPI)
                    {
                        options.Stylesheet = arg;
                    }
                    //Unrecognized argument
                    else
                    {
                        throw new NXsltCommandLineParsingException(NXsltStrings.ErrorUnrecognizedOption, arg);
                    }
                    break;
                }
            }
            //Resolve parameters
            ResolveParameters(options);
            //Instantiate specified extension objects
            ParseExtObjects(options);
            return(options);
        }
Exemple #9
0
 public static XmlReader CreateReader(Stream stream, XmlReaderSettings settings, NXsltOptions options)
 {
     if (options.ProcessXInclude)
     {
         return(XmlReader.Create(new XIncludingReader(stream), settings));
     }
     else
     {
         return(XmlReader.Create(stream, settings));
     }
 }
Exemple #10
0
 public static XmlReader CreateReader(Stream stream, XmlReaderSettings settings, NXsltOptions options, XmlResolver resolver)
 {
     if (options.ProcessXInclude)
     {
         XIncludingReader xir = new XIncludingReader(Directory.GetCurrentDirectory(), stream, resolver);
         xir.XmlResolver = resolver;
         return(XmlReader.Create(xir, settings));
     }
     else
     {
         return(XmlReader.Create(stream, settings));
     }
 }
        }// ParseNamespaceDeclaration

        /// <summary>
        /// Resolves enqueued parameters.
        /// </summary>
        /// <param name="options">nxslt options collection</param>
        private void ResolveParameters(NXsltOptions options)
        {
            if (paramQueue != null)
            {
                options.XslArgList = new XsltArgumentList();
                foreach (string param in paramQueue)
                {
                    int eqIndex = param.IndexOf('=');
                    //qname is prefix:localname or localname
                    string qname = param.Substring(0, eqIndex);
                    int colonIndex = qname.IndexOf(':');
                    if (colonIndex != -1)
                    {
                        //prefix:localname="value" case
                        string prefix = qname.Substring(0, colonIndex);
                        string uri;
                        //Resolve prefix
                        if (namespaceManager == null ||
                          (uri = namespaceManager.LookupNamespace(nameTable.Get(prefix))) == null)
                        {
                            throw new NXsltCommandLineParsingException(NXsltStrings.ErrorUnboundedPrefix, prefix);
                        }
                        else
                            options.XslArgList.AddParam(param.Substring(colonIndex + 1,
                              eqIndex - colonIndex - 1), uri, param.Substring(eqIndex + 1));
                    }
                    else
                        //localname="value"	case - possible default namespace
                        options.XslArgList.AddParam(qname, namespaceManager == null ? String.Empty : namespaceManager.DefaultNamespace,
                          param.Substring(eqIndex + 1));
                }
            }
        }// ResolveParameters
Exemple #12
0
 public static Type FindType(NXsltOptions options, string typeName)
 {
   Type type;
   Assembly assembly;
   if (options.AssemblyFileName != null)
   {
     //
     //Load assembly from a file
     //                
     try
     {
       assembly = Assembly.LoadFrom(options.AssemblyFileName);
     }
     catch
     {
       //Assembly cannot be loaded
       throw new NXsltException(NXsltStrings.ErrorLoadAssembly, options.AssemblyFileName);          
     }
     try
     {
       type = assembly.GetType(typeName, true);
     }
     catch
     {
       //Type not found in the specified assembly
       throw new NXsltException(NXsltStrings.ErrorGetTypeFromAssembly, typeName, options.AssemblyFileName);          
     }
   }
   else if (options.AssemblyName != null)
   {
     //
     //Load assembly by name
     //                
     try
     {
       assembly = Assembly.LoadWithPartialName(options.AssemblyName);          
     }
     catch
     {
       //Assembly cannot be loaded
       throw new NXsltException(NXsltStrings.ErrorLoadAssembly, options.AssemblyName);          
     }
     try
     {
       type = assembly.GetType(typeName, true);
     }
     catch
     {
       throw new NXsltException(NXsltStrings.ErrorGetType, typeName);          
     }
   }
   else
   {
     //
     //Assembly not specified                        
     //
     try
     {
       type = Type.GetType(typeName, true);
     }
     catch
     {
       throw new NXsltException(NXsltStrings.ErrorGetType, typeName);          
     }
   }
   return type;
 } // FindType()        
Exemple #13
0
 public static XmlReader CreateReader(Stream stream, XmlReaderSettings settings, NXsltOptions options, XmlResolver resolver)
 {
     if (options.ProcessXInclude)
     {
         XIncludingReader xir = new XIncludingReader(Directory.GetCurrentDirectory(), stream, resolver);
         xir.XmlResolver = resolver;
         return XmlReader.Create(xir, settings);
     }
     else
     {
         return XmlReader.Create(stream, settings);
     }
 }
Exemple #14
0
 public static XmlReader CreateReader(string filename, XmlReaderSettings settings, NXsltOptions options, XmlResolver resolver)
 {
     if (options.ProcessXInclude)
     {
         XIncludingReader xir = new XIncludingReader(filename, (resolver != null && resolver is DTDAllowingResolver)? new XmlUrlResolver() : resolver);
         xir.XmlResolver = resolver;
         return XmlReader.Create(xir, settings);
     }
     else
     {
         return XmlReader.Create(filename, settings);
     }
 }
Exemple #15
0
        public static Type FindType(NXsltOptions options, string typeName)
        {
            Type     type;
            Assembly assembly;

            if (options.AssemblyFileName != null)
            {
                //
                //Load assembly from a file
                //
                try
                {
                    assembly = Assembly.LoadFrom(options.AssemblyFileName);
                }
                catch
                {
                    //Assembly cannot be loaded
                    throw new NXsltException(NXsltStrings.ErrorLoadAssembly, options.AssemblyFileName);
                }
                try
                {
                    type = assembly.GetType(typeName, true);
                }
                catch
                {
                    //Type not found in the specified assembly
                    throw new NXsltException(NXsltStrings.ErrorGetTypeFromAssembly, typeName, options.AssemblyFileName);
                }
            }
            else if (options.AssemblyName != null)
            {
                //
                //Load assembly by name
                //
                try
                {
                    assembly = Assembly.LoadWithPartialName(options.AssemblyName);
                }
                catch
                {
                    //Assembly cannot be loaded
                    throw new NXsltException(NXsltStrings.ErrorLoadAssembly, options.AssemblyName);
                }
                try
                {
                    type = assembly.GetType(typeName, true);
                }
                catch
                {
                    throw new NXsltException(NXsltStrings.ErrorGetType, typeName);
                }
            }
            else
            {
                //
                //Assembly not specified
                //
                try
                {
                    type = Type.GetType(typeName, true);
                }
                catch
                {
                    throw new NXsltException(NXsltStrings.ErrorGetType, typeName);
                }
            }
            return(type);
        } // FindType()
 ///	<summary>
 ///	Parses command line	arguments into NXsltOptions collection.
 ///	</summary>
 ///	<param name="args">Command line	arguments.</param>        
 ///	<returns>Parsed	collection of options.</returns>
 public NXsltOptions ParseArguments(string[] args)
 {
     NXsltOptions options = new NXsltOptions();
     ParseArguments(args, options);
     return options;
 }
        ///	<summary>
        ///	Parses command line	arguments into NXsltOptions collection.
        ///	</summary>    
        public void ParseArguments(string[] args, NXsltOptions options)
        {
            for (int i = 0; i < args.Length; i++)
            {
                string arg = args[i];
                if (arg.StartsWith("@"))
                {
                    //File with more options
                    string responseFile = arg.Substring(1);
                    if (!string.IsNullOrEmpty(responseFile))
                    {
                        string responseFileFullName = Path.Combine(Directory.GetCurrentDirectory(), responseFile);
                        if (File.Exists(responseFileFullName))
                        {
                            using (StreamReader r = new StreamReader(responseFileFullName))
                            {
                                string line;
                                while ((line = r.ReadLine()) != null)
                                {
                                    if (!line.StartsWith("#"))
                                    {
                                        if (!string.IsNullOrEmpty(line.Trim()))
                                        {
                                            ParseArguments(line.Trim().Split(' '), options);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    switch (arg)
                    {
                        //Show help
                        case "-?":
                            options.ShowHelp = true;
                            break;
                        //Strip whitespace
                        case "-xw":
                            options.StripWhiteSpace = true;
                            break;
                        //Do not process character maps
                        case "-cm":
                            options.ProcessCharacterMaps = true;
                            break;
                        //XHTML output mode
                        case "-xhtml":
                            options.EnforceXhtmlOutput = true;
                            break;
                        //Output filename
                        case "-o":
                            if (i == args.Length - 1)
                            {
                                //Absent file name
                                throw new NXsltCommandLineParsingException(NXsltStrings.ErrorMissingOutFileName);
                            }
                            else
                            {
                                //Next argument must be filename
                                options.OutFile = args[++i];
                                break;
                            }
                        //Show timings
                        case "-t":
                            options.ShowTiming = true;
                            break;
                        //No source XML
                        case "-xs":
                            if (options.GetStylesheetFromPI)
                            {
                                //Both -xs and -pi cannot be specified
                                throw new NXsltCommandLineParsingException(NXsltStrings.ErrorBothNoSourceAndPI);
                            }
                            else if ((options.Source != null && options.Stylesheet != null) ||
                                    options.LoadSourceFromStdin)
                            {
                                //When using -xs no source shoold be specified
                                throw new NXsltCommandLineParsingException(NXsltStrings.ErrorBothNoSourceAndSource);
                            }
                            else if (options.Source != null && options.Stylesheet == null)
                            {
                                //That was stylesheet, not source
                                options.Stylesheet = options.Source;
                                options.Source = null;
                            }
                            options.NoSourceXml = true;
                            break;
                        //Get stylesheet URI from xml-stylesheet PI
                        case "-pi":
                            if (options.Stylesheet != null || options.LoadStylesheetFromStdin)
                            {
                                //Both -pi and stylesheet cannot be specified
                                throw new NXsltCommandLineParsingException(NXsltStrings.ErrorBothStylesheetAndPI);
                            }
                            else if (options.NoSourceXml)
                            {
                                //Both -xs and -pi cannot be specified
                                throw new NXsltCommandLineParsingException(NXsltStrings.ErrorBothNoSourceAndPI);

                            }
                            else
                                options.GetStylesheetFromPI = true;
                            break;
                        //Network credentials for source XML
                        case "-xmlc":
                            if (i == args.Length - 1)
                            {
                                //Absent credentials
                                throw new NXsltCommandLineParsingException(NXsltStrings.ErrorMissingXMLCredentials);
                            }
                            else
                            {
                                //Next argument must be credentials              
                                string credstring = args[++i];
                                if (options.SourceCredential != null)
                                {
                                    //Duplicate credentials
                                    throw new NXsltCommandLineParsingException(NXsltStrings.ErrorDuplicateCredentials);
                                }
                                options.SourceCredential = ParseCredentials(credstring);
                                break;
                            }
                        //Network credentials for stylesheet
                        case "-xslc":
                            if (i == args.Length - 1)
                            {
                                //Absent credentials
                                throw new NXsltCommandLineParsingException(NXsltStrings.ErrorMissingXSLTCredentials);
                            }
                            else
                            {
                                //Next argument must be credentials              
                                string credstring = args[++i];
                                if (options.XSLTCredential != null)
                                {
                                    //Duplicate credentials
                                    throw new NXsltCommandLineParsingException(NXsltStrings.ErrorDuplicateCredentials);
                                }
                                options.XSLTCredential = ParseCredentials(credstring);
                                break;
                            }
                        //Use named URI resolver type
                        case "-r":
                            if (i == args.Length - 1)
                            {
                                //Absent resolver type name
                                throw new NXsltCommandLineParsingException(NXsltStrings.ErrorMissingResolverTypeName);
                            }
                            else
                            {
                                //Next argument must be resolver type
                                options.ResolverTypeName = args[++i];
                                break;
                            }
                        //Assembly file name
                        case "-af":
                            if (options.AssemblyName != null)
                            {
                                throw new NXsltCommandLineParsingException(NXsltStrings.ErrorBothAssemblyFileNameAndName);
                            }
                            if (i == args.Length - 1)
                            {
                                //Absent assembly file name
                                throw new NXsltCommandLineParsingException(NXsltStrings.ErrorMissingAssemblyFileName);
                            }
                            else
                            {
                                //Next argument must be assembly file name
                                options.AssemblyFileName = args[++i];
                                break;
                            }
                        //Assembly name
                        case "-an":
                            if (options.AssemblyFileName != null)
                            {
                                throw new NXsltCommandLineParsingException(NXsltStrings.ErrorBothAssemblyFileNameAndName);
                            }
                            if (i == args.Length - 1)
                            {
                                //Absent assembly name
                                throw new NXsltCommandLineParsingException(NXsltStrings.ErrorMissingAssemblyName);
                            }
                            else
                            {
                                //Next argument must be assembly name
                                options.AssemblyName = args[++i];
                                break;
                            }
                        //Allow multiple output documents
                        case "-mo":
                            options.MultiOutput = true;
                            break;
                        //Validate documents
                        case "-v":
                            options.ValidateDocs = true;
                            break;
                        //Do not resolve externals
                        case "-xe":
                            options.ResolveExternals = false;
                            break;
                        //Do not process XInclude
                        case "-xi":
                            options.ProcessXInclude = false;
                            break;
                        //Process XInclude in XSLT
                        case "-xslxi":
                            options.ProcessXIncludeInXSLT = true;
                            break;
                        //Pretty print source XML
                        case "-pp":
                            options.PrettyPrintMode = true;
                            break;
                        //Extension class names
                        case "-ext":
                            if (i == args.Length - 1)
                            {
                                //Absent class names
                                throw new NXsltCommandLineParsingException(NXsltStrings.ErrorMissingExtClassNames);
                            }
                            else
                            {
                                //Next argument must be ext class names list
                                options.ExtClasses = args[++i].Split(',');
                                break;
                            }
                        //Source or Stylesheet to be read from stdin
                        case "-":
                            if (options.Source == null && !options.LoadSourceFromStdin)
                            {
                                options.LoadSourceFromStdin = true;
                            }
                            else if (options.Stylesheet == null && !options.LoadStylesheetFromStdin)
                            {
                                //Check out that both source and stylesheet are not "-"
                                if (options.LoadSourceFromStdin)
                                {
                                    //Both source and stylesheet cannot be read from stdin
                                    throw new NXsltCommandLineParsingException(NXsltStrings.ErrorBothStdin);
                                }
                                else
                                    options.LoadStylesheetFromStdin = true;
                            }
                            else
                            {
                                //Unrecognized "-"
                                throw new NXsltCommandLineParsingException(NXsltStrings.ErrorUnrecognizedOption, arg);
                            }
                            break;
                        //Other argment - source URI, stylesheet URI, parameter or namespace declaration
                        default:
                            //namespace declaration?
                            if ((arg.StartsWith("xmlns:") || arg.StartsWith("xmlns=")) && arg.Contains("="))
                            {
                                ParseNamespaceDeclaration(arg);
                            }
                            //Parameter?            
                            else if (arg.Contains("="))
                            {
                                //Parameter - put to the queue till all namespaces are not processed
                                if (arg.StartsWith("="))
                                {
                                    throw new NXsltCommandLineParsingException(NXsltStrings.ErrorMissingName);
                                }
                                else if (arg.EndsWith("="))
                                {
                                    throw new NXsltCommandLineParsingException(NXsltStrings.ErrorMissingValue, arg.Remove(arg.Length - 1, 1));
                                }
                                //Enqueue param till all namespace declarations parsed
                                EnqueueParameter(args, arg);
                            }
                            else if (arg.StartsWith("-"))
                            {
                                //Unrecognized option
                                throw new NXsltCommandLineParsingException(NXsltStrings.ErrorUnrecognizedOption, arg);
                            }
                            //Source URI?
                            else if (options.Source == null && !options.LoadSourceFromStdin && !options.NoSourceXml)
                            {
                                options.Source = arg;
                            }
                            //Stylesheet URI?
                            else if (options.Stylesheet == null && !options.LoadStylesheetFromStdin && !options.GetStylesheetFromPI)
                            {
                                options.Stylesheet = arg;
                            }
                            //Unrecognized argument
                            else
                            {
                                throw new NXsltCommandLineParsingException(NXsltStrings.ErrorUnrecognizedOption, arg);
                            }
                            break;
                    }
                }
            }
            //Resolve parameters
            ResolveParameters(options);
            //Instantiate specified extension objects
            ParseExtObjects(options);
        }
Exemple #18
0
 public static XmlReader CreateReader(string filename, XmlReaderSettings settings, NXsltOptions options)
 {
     if (options.ProcessXInclude)
     {
         return(XmlReader.Create(new XIncludingReader(filename), settings));
     }
     else
     {
         return(XmlReader.Create(filename, settings));
     }
 }
        }// ResolveParameters

        /// <summary>
        /// Parses and instantiates extention objects.
        /// </summary>
        /// <param name="options">nxslt options collection</param>    
        private void ParseExtObjects(NXsltOptions options)
        {
            if (options.ExtClasses != null)
            {
                if (options.XslArgList == null)
                    options.XslArgList = new XsltArgumentList();
                foreach (string typeDecl in options.ExtClasses)
                {
                    string[] parts = typeDecl.Split(new char[] { ':' }, 2);
                    if (parts.Length == 1)
                    {
                        Type extObjType = TypeUtils.FindType(options, parts[0]);
                        try
                        {
                            object o = Activator.CreateInstance(extObjType);
                            if (namespaceManager == null || namespaceManager.DefaultNamespace == String.Empty)
                            {
                                //Extension object not bound to a namespace
                                throw new NXsltException(NXsltStrings.ErrorExtNoNamespace, parts[0]);
                            }
                            string ns = namespaceManager.DefaultNamespace;
                            if (options.XslArgList.GetExtensionObject(ns) != null)
                            {
                                //More than one extension object in the same namespace URI
                                throw new NXsltException(NXsltStrings.ErrorExtNamespaceClash, ns);

                            }
                            options.XslArgList.AddExtensionObject(ns, o);
                        }
                        catch (Exception e)
                        {
                            //Type cannot be instantiated
                            throw new NXsltException(NXsltStrings.ErrorCreateResolver, parts[0], e.Message);
                        }
                    }
                    else
                    {
                        string prefix = parts[0];
                        string uri;
                        //Resolve prefix
                        if (namespaceManager == null ||
                          (uri = namespaceManager.LookupNamespace(nameTable.Get(prefix))) == null)
                        {
                            throw new NXsltCommandLineParsingException(NXsltStrings.ErrorUnboundedPrefix, prefix);
                        }

                        Type extObjType = TypeUtils.FindType(options, parts[1]);
                        try
                        {
                            object o = Activator.CreateInstance(extObjType);
                            if (options.XslArgList.GetExtensionObject(uri) != null)
                            {
                                //More than one extension object in the same namespace URI
                                throw new NXsltCommandLineParsingException(NXsltStrings.ErrorExtNamespaceClash, uri);
                            }
                            options.XslArgList.AddExtensionObject(uri, o);
                        }
                        catch (Exception e)
                        {
                            //Type cannot be instantiated
                            throw new NXsltCommandLineParsingException(NXsltStrings.ErrorCreateResolver, parts[1], e.Message);
                        }
                    }
                }
            }
        }// ParseExtObjects
Exemple #20
0
 public static XmlReader CreateReader(string filename, XmlReaderSettings settings, NXsltOptions options, XmlResolver resolver)
 {
     if (options.ProcessXInclude)
     {
         XIncludingReader xir = new XIncludingReader(filename, (resolver != null && resolver is DTDAllowingResolver)? new XmlUrlResolver() : resolver);
         xir.XmlResolver = resolver;
         return(XmlReader.Create(xir, settings));
     }
     else
     {
         return(XmlReader.Create(filename, settings));
     }
 }