Esempio n. 1
0
	/// <summary>
	/// Process an XML file using Velocity
	/// </summary>
	private void Process(String basedir, String xmlFile, FileInfo destdir, AnakiaXmlDocument projectDocument) {
	    FileInfo outFile = null;
	    FileInfo inFile = null;
	    StreamWriter writer = null;
	    try {
		// the current input file relative to the baseDir
		inFile = new System.IO.FileInfo(basedir + Path.DirectorySeparatorChar.ToString() + xmlFile);
		// the output file relative to basedir
		outFile = new System.IO.FileInfo(destdir + Path.DirectorySeparatorChar.ToString() + xmlFile.Substring(0, (xmlFile.LastIndexOf((System.Char) '.')) - (0)) + extension);

		// only process files that have changed
		if (lastModifiedCheck == false || (inFile.LastWriteTime.Ticks > outFile.LastWriteTime.Ticks || styleFile.LastWriteTime.Ticks > outFile.LastWriteTime.Ticks || projectFile.LastWriteTime.Ticks > outFile.LastWriteTime.Ticks)) {
		    EnsureDirectoryFor(outFile);

		    //-- command line status
		    Log.WriteLine(LogPrefix + "Input:  " + inFile);

		    // Build the Anakia Document
		    AnakiaXmlDocument root = new AnakiaXmlDocument();
		    root.Load(inFile.FullName);

		    // Shove things into the Context
		    VelocityContext context = new VelocityContext();

		    /*
		    *  get the property TEMPLATE_ENCODING
		    *  we know it's a string...
		    */
		    String encoding = (String) ve.GetProperty(RuntimeConstants_Fields.OUTPUT_ENCODING);
		    if (encoding == null || encoding.Length == 0 || encoding.Equals("8859-1") || encoding.Equals("8859_1")) {
			encoding = "ISO-8859-1";
		    }

		    context.Put("root", root.DocumentElement);
		    context.Put("relativePath", getRelativePath(xmlFile));
		    context.Put("escape", new Escape());
		    context.Put("date", System.DateTime.Now);

		    // only put this into the context if it exists.
		    if (projectDocument != null) {
			context.Put("project", projectDocument.DocumentElement);
		    }

		    // Process the VSL template with the context and write out
		    // the result as the outFile.
		    writer = new System.IO.StreamWriter(new System.IO.FileStream(outFile.FullName, System.IO.FileMode.Create));
		    // get the template to process

		    Template template = ve.GetTemplate(styleFile.Name)
		    ;
		    template.Merge(context, writer);

		    Log.WriteLine(LogPrefix + "Output: " + outFile);
		}
	    } catch (System.Exception e) {
		Log.WriteLine(LogPrefix + "Failed to process " + inFile);
		if (outFile != null) {
		    bool tmpBool2;
		    if (System.IO.File.Exists(outFile.FullName)) {
			System.IO.File.Delete(outFile.FullName);
			tmpBool2 = true;
		    } else if (System.IO.Directory.Exists(outFile.FullName)) {
			System.IO.Directory.Delete(outFile.FullName);
			tmpBool2 = true;
		    } else
			tmpBool2 = false;
		    bool generatedAux3 = tmpBool2;
		}
		SupportClass.WriteStackTrace(e, Console.Error);
	    } finally {
		if (writer != null) {
		    try {
			writer.Flush();
			writer.Close();
		    } catch (System.Exception e) {
			// closing down, just ignore
		    }
		}
	    }
	}
Esempio n. 2
0
        /// <summary>
        /// Process an XML file using Velocity
        /// </summary>
        private void Process(String basedir, String xmlFile, FileInfo destdir, AnakiaXmlDocument projectDocument)
        {
            FileInfo     outFile = null;
            FileInfo     inFile  = null;
            StreamWriter writer  = null;

            try {
                // the current input file relative to the baseDir
                inFile = new System.IO.FileInfo(basedir + Path.DirectorySeparatorChar.ToString() + xmlFile);
                // the output file relative to basedir
                outFile = new System.IO.FileInfo(destdir + Path.DirectorySeparatorChar.ToString() + xmlFile.Substring(0, (xmlFile.LastIndexOf((System.Char) '.')) - (0)) + extension);

                // only process files that have changed
                if (lastModifiedCheck == false || (inFile.LastWriteTime.Ticks > outFile.LastWriteTime.Ticks || styleFile.LastWriteTime.Ticks > outFile.LastWriteTime.Ticks || projectFile.LastWriteTime.Ticks > outFile.LastWriteTime.Ticks))
                {
                    EnsureDirectoryFor(outFile);

                    //-- command line status
                    Log.WriteLine(LogPrefix + "Input:  " + inFile);

                    // Build the Anakia Document
                    AnakiaXmlDocument root = new AnakiaXmlDocument();
                    root.Load(inFile.FullName);

                    // Shove things into the Context
                    VelocityContext context = new VelocityContext();

                    /*
                     *  get the property TEMPLATE_ENCODING
                     *  we know it's a string...
                     */
                    String encoding = (String)ve.GetProperty(RuntimeConstants_Fields.OUTPUT_ENCODING);
                    if (encoding == null || encoding.Length == 0 || encoding.Equals("8859-1") || encoding.Equals("8859_1"))
                    {
                        encoding = "ISO-8859-1";
                    }

                    context.Put("root", root.DocumentElement);
                    context.Put("relativePath", getRelativePath(xmlFile));
                    context.Put("escape", new Escape());
                    context.Put("date", System.DateTime.Now);

                    // only put this into the context if it exists.
                    if (projectDocument != null)
                    {
                        context.Put("project", projectDocument.DocumentElement);
                    }

                    // Process the VSL template with the context and write out
                    // the result as the outFile.
                    writer = new System.IO.StreamWriter(new System.IO.FileStream(outFile.FullName, System.IO.FileMode.Create));
                    // get the template to process

                    Template template = ve.GetTemplate(styleFile.Name)
                    ;
                    template.Merge(context, writer);

                    Log.WriteLine(LogPrefix + "Output: " + outFile);
                }
            } catch (System.Exception e) {
                Log.WriteLine(LogPrefix + "Failed to process " + inFile);
                if (outFile != null)
                {
                    bool tmpBool2;
                    if (System.IO.File.Exists(outFile.FullName))
                    {
                        System.IO.File.Delete(outFile.FullName);
                        tmpBool2 = true;
                    }
                    else if (System.IO.Directory.Exists(outFile.FullName))
                    {
                        System.IO.Directory.Delete(outFile.FullName);
                        tmpBool2 = true;
                    }
                    else
                    {
                        tmpBool2 = false;
                    }
                    bool generatedAux3 = tmpBool2;
                }
                SupportClass.WriteStackTrace(e, Console.Error);
            } finally {
                if (writer != null)
                {
                    try {
                        writer.Flush();
                        writer.Close();
                    } catch (System.Exception e) {
                        // closing down, just ignore
                    }
                }
            }
        }
Esempio n. 3
0
	/// <summary>
	/// Main body of the application
	/// </summary>
	protected override void ExecuteTask() {
	    if (destDir == null) {
		String msg = "destdir attribute must be set!";
		throw new BuildException(msg);
	    }
	    if (styleFile == null) {
		throw new BuildException("style attribute must be set!");
	    }

	    if (velocityPropertiesFile == null) {
		velocityPropertiesFile = new FileInfo("nvelocity.properties");
	    }

	    /*
	    * If the props file doesn't exist AND a templatePath hasn't 
	    * been defined, then throw the exception.
	    */
	    if (!velocityPropertiesFile.Exists && templatePath==null) {
		throw new BuildException("No template path and could not " + "locate nvelocity.properties file: " + velocityPropertiesFile);
	    }

	    Log.WriteLine(LogPrefix + "Transforming into: " + destDir);

	    // projectFile relative to baseDir
	    if (projectFile != null && !projectFile.Exists) {
		Log.WriteLine(LogPrefix + "Project file is defined, but could not be located: " + projectFile.FullName);
		projectFile = null;
	    }

	    AnakiaXmlDocument projectDocument = null;
	    try {
		if (velocityPropertiesFile.Exists) {
		    ve.Init(velocityPropertiesFile.FullName);
		} else {
		    if (templatePath != null && templatePath.FullName.Length > 0) {
			ve.SetProperty(RuntimeConstants_Fields.FILE_RESOURCE_LOADER_CACHE, true);
			ve.SetProperty(RuntimeConstants_Fields.FILE_RESOURCE_LOADER_PATH, templatePath.FullName);
			ve.Init();
		    }
		}

		// Build the Project file document
		if (projectFile != null) {
		    projectDocument = new AnakiaXmlDocument();
		    projectDocument.Load(projectFile.FullName);
		}
	    } catch (System.Exception e) {
		Log.WriteLine(LogPrefix + "Error: " + e.ToString());
		throw new BuildException(e.ToString());
	    }

	    // get the base directory from the fileset - needed to colapse ../ syntax
	    DirectoryInfo di = new DirectoryInfo(fileset.BaseDirectory);

	    // get a list of files to work on
	    foreach (string filename in fileset.FileNames) {
		String relativeFilename = filename.Substring(di.FullName.Length+1);
		FileInfo file = new FileInfo(filename);
		if (file.Exists) {
		    Process(di.FullName, relativeFilename, destDir, projectDocument);
		}
	    }
	}
Esempio n. 4
0
        /// <summary>
        /// Main body of the application
        /// </summary>
        protected override void ExecuteTask()
        {
            if (destDir == null)
            {
                String msg = "destdir attribute must be set!";
                throw new BuildException(msg);
            }
            if (styleFile == null)
            {
                throw new BuildException("style attribute must be set!");
            }

            if (velocityPropertiesFile == null)
            {
                velocityPropertiesFile = new FileInfo("nvelocity.properties");
            }

            /*
             * If the props file doesn't exist AND a templatePath hasn't
             * been defined, then throw the exception.
             */
            if (!velocityPropertiesFile.Exists && templatePath == null)
            {
                throw new BuildException("No template path and could not " + "locate nvelocity.properties file: " + velocityPropertiesFile);
            }

            Log.WriteLine(LogPrefix + "Transforming into: " + destDir);

            // projectFile relative to baseDir
            if (projectFile != null && !projectFile.Exists)
            {
                Log.WriteLine(LogPrefix + "Project file is defined, but could not be located: " + projectFile.FullName);
                projectFile = null;
            }

            AnakiaXmlDocument projectDocument = null;

            try {
                if (velocityPropertiesFile.Exists)
                {
                    ve.Init(velocityPropertiesFile.FullName);
                }
                else
                {
                    if (templatePath != null && templatePath.FullName.Length > 0)
                    {
                        ve.SetProperty(RuntimeConstants_Fields.FILE_RESOURCE_LOADER_CACHE, true);
                        ve.SetProperty(RuntimeConstants_Fields.FILE_RESOURCE_LOADER_PATH, templatePath.FullName);
                        ve.Init();
                    }
                }

                // Build the Project file document
                if (projectFile != null)
                {
                    projectDocument = new AnakiaXmlDocument();
                    projectDocument.Load(projectFile.FullName);
                }
            } catch (System.Exception e) {
                Log.WriteLine(LogPrefix + "Error: " + e.ToString());
                throw new BuildException(e.ToString());
            }

            // get the base directory from the fileset - needed to colapse ../ syntax
            DirectoryInfo di = new DirectoryInfo(fileset.BaseDirectory);

            // get a list of files to work on
            foreach (string filename in fileset.FileNames)
            {
                String   relativeFilename = filename.Substring(di.FullName.Length + 1);
                FileInfo file             = new FileInfo(filename);
                if (file.Exists)
                {
                    Process(di.FullName, relativeFilename, destDir, projectDocument);
                }
            }
        }