Beispiel #1
0
        private static void DeclareOutputControls(TestRunner.TestDriver driver, XPathCompiler xpc, XdmItem env, Environment environment)
        {
            String needsTree = xpc.Evaluate("string((output/@tree,'yes')[1])", env).ToString();

            environment.outputTree = "yes".Equals(needsTree);
            String needsSerialization = xpc.Evaluate("string((output/@serialize,'no')[1])", env).ToString();

            environment.outputSerialize = "yes".Equals(needsSerialization);
        }
Beispiel #2
0
        /**
         * Construct an Environment
         *
         * @param xpc          the XPathCompiler used to process the catalog file
         * @param env          the Environment element in the catalog file
         * @param environments the set of environments to which this one should be added (may be null)
         * @return the constructed Environment object
         * @throws SaxonApiException
         */

        public static Environment processEnvironment(TestRunner.TestDriver driver,
                                                     XPathCompiler xpc, XdmItem env, Dictionary <string, Environment> environments, Environment defaultEnvironment)
        {
            Environment environment = new Environment();
            String      name        = ((XdmNode)env).GetAttributeValue(new QName("name"));

            if (name != null)
            {
                System.Console.WriteLine("Loading environment " + name);
            }
            environment.processor = new Processor(true);
            if (defaultEnvironment != null)
            {
                environment.processor.SetProperty(JFeatureKeys.XSD_VERSION,
                                                  defaultEnvironment.processor.Implementation.getConfigurationProperty(JFeatureKeys.XSD_VERSION).ToString());
            }
            // AutoActivate.activate(environment.processor);
            if (driver.GenerateByteCode == 1)
            {
                environment.processor.SetProperty(JFeatureKeys.GENERATE_BYTE_CODE, "true");
                environment.processor.SetProperty(JFeatureKeys.DEBUG_BYTE_CODE, "false");
            }
            else if (driver.GenerateByteCode == 2)
            {
                environment.processor.SetProperty(JFeatureKeys.GENERATE_BYTE_CODE, "true");
                environment.processor.SetProperty(JFeatureKeys.DEBUG_BYTE_CODE, "true");
                //environment.processor.SetProperty(JFeatureKeys.DEBUG_BYTE_CODE_DIR, "debugByteCode");
            }
            else
            {
                environment.processor.SetProperty(JFeatureKeys.GENERATE_BYTE_CODE, "false");
                environment.processor.SetProperty(JFeatureKeys.DEBUG_BYTE_CODE, "false");
            }
            environment.xpathCompiler          = environment.processor.NewXPathCompiler();
            environment.xpathCompiler.BaseUri  = ((XdmNode)env).BaseUri.ToString();
            environment.xqueryCompiler         = environment.processor.NewXQueryCompiler();
            environment.xqueryCompiler.BaseUri = ((XdmNode)env).BaseUri.AbsolutePath;
            if (driver.Spec.ToString().Contains("XT"))
            {
                environment.xsltCompiler = environment.processor.NewXsltCompiler();
                environment.xsltCompiler.XsltLanguageVersion = ((SpecAttr)(driver.Spec.GetAttr())).version;
            }
            if (driver.Unfolded)
            {
                // environment.xqueryCompiler.Implementation.setCodeInjector(new LazyLiteralInjector()); //TODO
            }
            DocumentBuilder builder = environment.processor.NewDocumentBuilder();

            builder.TreeModel      = driver.TreeModel;
            environment.sourceDocs = new Dictionary <string, XdmNode>();
            if (environments != null && name != null)
            {
                try
                {
                    environments.Add(name, environment);
                }
                catch (Exception) { }
            }
            foreach (XdmItem dependency in xpc.Evaluate("dependency", env))
            {
                if (!driver.dependencyIsSatisfied((XdmNode)dependency, environment))
                {
                    environment.usable = false;
                }
            }

            // set the base URI if specified

            SetBaseUri(driver, xpc, env, environment);

            // set any requested collations

            RegisterCollations(xpc, env, environment);

            // declare the requested namespaces

            DeclareNamespaces(xpc, env, environment);

            // load the requested schema documents

            SchemaManager manager         = environment.processor.SchemaManager;
            bool          validateSources = LoadSchemaDocuments(xpc, env, manager);

            // load the requested source documents

            LoadSourceDocuments(driver, xpc, env, environment, builder, manager, validateSources);

            // create a collection URI resolver to handle the requested collections

            CreateCollectionUriResolver(driver, xpc, env, environment, builder);

            // create an unparsed text resolver to handle any unparsed text resources

            CreateUnparsedTextResolver(driver, xpc, env, environment);

            // register any required decimal formats

            // registerDecimalFormats(driver, xpc, env, environment);

            // declare any variables

            DeclareExternalVariables(driver, xpc, env, environment);

            // declare any output controls
            DeclareOutputControls(driver, xpc, env, environment);

            // handle requested context item
            foreach (XdmItem param in xpc.Evaluate("context-item", env))
            {
                String   select = ((XdmNode)param).GetAttributeValue(new QName("select"));
                XdmValue value  = xpc.Evaluate(select, null);
                environment.contextItem = (XdmItem)value;
            }

            XmlUrlResolver res = new XmlUrlResolver();
            // compile any stylesheet defined as part of the environment (only one allowed)
            DocumentBuilder builder1 = environment.processor.NewDocumentBuilder();

            foreach (XdmItem stylesheet in xpc.Evaluate("stylesheet[not(@role='secondary')]", env))
            {
                string fileName = ((XdmNode)stylesheet).GetAttributeValue(new QName("file"));
                try
                {
                    XdmNode styleSource = builder1.Build(res.ResolveUri(((XdmNode)env).BaseUri, fileName));
                    environment.xsltExecutable = environment.xsltCompiler.Compile(styleSource);
                }
                catch (Exception e)
                {
                    driver.println("**** failure while compiling environment-defined stylesheet " + fileName);
                }
            }


            // compile any stylesheet packages defined as part of the environment
            // Support this only in EE - an unusable environment in PE/HE
            foreach (XdmItem stylesheet in xpc.Evaluate("package[@role='secondary']", env))
            {
                if (!"EE".Equals(environment.processor.Edition))
                {
                    environment.usable = false;
                    break;
                }
                string     fileName = ((XdmNode)stylesheet).GetAttributeValue(new QName("file"));
                Uri        uri      = res.ResolveUri(((XdmNode)env).BaseUri, fileName);
                FileStream file     = new FileStream(uri.AbsolutePath, FileMode.Open, FileAccess.Read);
                try
                {
                    XsltPackage pkg = environment.xsltCompiler.CompilePackage(file);
                    environment.xsltCompiler.ImportPackage(pkg);
                }
                catch (Exception e)
                {
                    //e.printStackTrace();
                    driver.println("**** failure while compiling environment-defined stylesheet package " + fileName);
                    driver.println("****Failure " + e.Message + " in compiling environment " + name);
                    environment.usable = false;
                }
            }

            return(environment);
        }