Package KFM.preprocessor

Source Code of KFM.preprocessor.TemplatesProcessor

/*
*  This software and supporting documentation were developed by
*
*    Siemens Corporate Technology
*    Competence Center Knowledge Management and Business Transformation
*    D-81730 Munich, Germany
*
*    Authors (representing a really great team ;-) )
*            Stefan B. Augustin, Thorbj´┐Żrn Hansen, Manfred Langen
*
*  This software is Open Source under GNU General Public License (GPL).
*  Read the text of this license in LICENSE.TXT
*  or look at www.opensource.org/licenses/
*
*  Once more we emphasize, that:
*  THIS SOFTWARE IS MADE AVAILABLE,  AS IS,  WITHOUT ANY WARRANTY
*  REGARDING  THE  SOFTWARE,  ITS  PERFORMANCE OR
*  FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR
*  ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND
*  PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
*
*/


//  TemplatesProcessor

// ************ package ******************************************************
package KFM.preprocessor;

// ************ imports ******************************************************
import java.util.*;
import java.io.*;
import KFM.DirNavigator.*;
import KFM.*;
import KFM.File.*;

/** The TemplatesProcessor traverses the templates directory.<br>
* <p>It does the necessary jsp-compiling through the jsp engine.
* Each processing is done for each language described in the corresponding
* properties file 'TemplProcessor.properties' if the file has no language in
* its path (new templates). The old templates will be processed as before.</p>
*
* @see KFM.Taglib.LocalizerTag
* @see KFM.preprocessor.AbstractPreprocessor

*/

public class TemplatesProcessor extends AbstractPreprocessor
{
    private static final String cTemplateDir = "resources"; // the templates directory
    private PropertiesNode  mConfigNode = null; //holds the properties of the corresponding
    //properties file
    private Properties mHttpProps = new Properties(); // the http properties
    private Vector mLangs = new Vector(); //vector of the actual supported languages
    private Properties mLocalizedDirs = new Properties();
    //properties with the actual localized directories as keys
    private String mLang = null; //the current language

    /**
    * Constructor.
    * @param aServerName the name of the server the tomcat engine runs on
    * @param aServerPort the port of the tomcat engine
    * @param aConfigDir path to the corresponding TemplProcessor.properties.
    * @param aInitProps the command line arguments that will be searched after http parameters.
    */
    public TemplatesProcessor(
        String aServerName,
        String aServerPort,
        String aConfigPath,
        Properties aInitProps,
        String aTempDir)
        throws IOException,
        FileNotFoundException,
        NoSuchNodeException
    {
        super(aServerName,
        aServerPort,
        "resources",
        "resources",
        new File(aTempDir));

        //load the properties
        Properties tHelpProps = new Properties();
        tHelpProps.load(new FileInputStream (aConfigPath));

        //build the properties nodes
        PropertiesNodeBuilder tBuilder = new PropertiesNodeBuilder(tHelpProps);
        mConfigNode = tBuilder.load();
        mHttpProps = AbstractPreprocessor.initHttpProps(aInitProps);

        PropertiesNode[] tLocalizedDirs = mConfigNode.getChild("localizedDir").getChildren();
        PropertiesNode[] tLangs = mConfigNode.getChild("lang").getChildren();

        //initialize the current languages
        for (int i = 0; i < tLangs.length; i++)
        {
            PropertiesNode tLangNode = tLangs[i];
            String tLang = (String)tLangNode.getValue();
            mLangs.addElement(tLang);
        }

        //initialize the current localized directories
        for (int i = 0; i < tLocalizedDirs.length; i++)
        {
            PropertiesNode tLocalizedDirNode = tLocalizedDirs[i];
            String tLocalizedDir = (String)tLocalizedDirNode.getValue();
            mLocalizedDirs.put(tLocalizedDir, "true");
        }
    }

    private void setLanguage (String aLang)
    {
        mLang = aLang;
    }


    /**
    * Returns all current http parameters
    */
    public Properties getHttpParams()
    {
        if (mLang != null)
            mHttpProps.put("language", mLang);
        else mHttpProps.remove("language");
        return mHttpProps;
    }

    /**
    * Returns true if the file is a file that has to be divides in as much copies
    * as languages are provided.
    *
    */
    private boolean checkWithLanguage(File aFile)
    {
        int tIndex = -1;
        for (int i = 0; i < mLangs.size(); i++)
        {
            String tLang = (String)mLangs.elementAt(i);
            String tNewPath = aFile.getAbsolutePath();
            tNewPath = tNewPath.replace('\\', '/');
            String tLangTxt = "." + tLang + "." ;
            tIndex = tNewPath.indexOf(tLangTxt);
        }
        return (tIndex != -1);
    }

    /**
    * Create the new dirs. If file already exists on file system
    * delete it to prevent errors if it is write protected.
    */
    private void ensurePathForFileExists(File aFile)
    {
        File tPath = new File(aFile.getParent());
        if(!tPath.exists()) {
            tPath.mkdirs();
        }
    }

    /**
    * Returns the path to the new file.
    * The new path will contain the language if 'checkWithLanguage' returns true.
    */
    public String computeNewFilePath(File aFile)
    {
        String cSrcDir = "/src";
        String tNewPath = aFile.getAbsolutePath();
        tNewPath = tNewPath.replace('\\', '/');

        // Now we separate the tempates sources and 'objects' = precompiled files.
        // The sources are in the 'resources/src' directory whereas the precompiled
        // files stay in the 'resources' directory. So remove the 'src' directory from
        // target path.
        int tSrcIndex = 0;
        if ((tSrcIndex = tNewPath.indexOf(cSrcDir)) != -1)
        {
            String tPostFix = tNewPath.substring(tSrcIndex + cSrcDir.length());
            tNewPath = tNewPath.substring(0, tSrcIndex) + tPostFix;
            // System.out.println("new path "+tNewPath);
        }

        if (mLang != null)
        {
            StringBuffer tNewPathBuf = new StringBuffer();
            String tStr = tNewPath.substring(0, (tNewPath.lastIndexOf("/") + 1));
            tNewPathBuf.append(tStr);
            String tFileName = aFile.getName();

            int tIndex = tFileName.indexOf(".kfmt");
            if (tIndex != -1)
            {
                tNewPathBuf.append(tFileName.substring(0, tFileName.indexOf(".kfmt")));
                tNewPathBuf.append(".");
                tNewPathBuf.append(mLang);
                tNewPathBuf.append(tFileName.substring(tFileName.indexOf(".kfmt"), tFileName.length()));
                tNewPath = tNewPathBuf.toString();
            }
            else if ((tIndex = tFileName.indexOf(".xsl"))!= -1)
            {
                tNewPathBuf.append(tFileName.substring(0, tFileName.indexOf(".xsl")));
                tNewPathBuf.append(".");
                tNewPathBuf.append(mLang);
                tNewPathBuf.append(tFileName.substring(tFileName.indexOf(".xsl"), tFileName.length()));
                tNewPath = tNewPathBuf.toString();
            }

        }
        return tNewPath;
    }




    /**
    * Prints the usage of this class.
    */
    public static void printUsage()
    {
        System.out.println("Wrong usage! Please refer to:");
        System.out.println("java KFM.preprocessor.TemplatesProcessor tempDir=<tempDir> startDir=<startDir> configDir=<configDir> host=<hostname> port=<port> [tomcat=<TRUE|false>]");

    }

    /**
    * Nothing to filter so return the file content itself.
    *
    * @param aContent The content of the properties file.
    */
    public String filterContent(String aContent)
    {
        return aContent;

    }

    /**
    * Traverses each file one time for each language (for the new templates)
    * else only one time.
    */
    public void workFile(File aFile)
    {
        String tPath = aFile.getAbsolutePath();
        if (tPath.endsWith(".jsp"))
        {
            tPath = tPath.replace('\\', '/');
            System.out.println("work file "+tPath);
            tPath = tPath.substring(0, (tPath.indexOf(aFile.getName()) - 1));
            tPath = tPath.substring (tPath.indexOf(cTemplateDir), tPath.length());

            if ((!checkWithLanguage(aFile))&& (mLocalizedDirs.getProperty(tPath)!= null))
            {
                for (int i = 0; i < mLangs.size(); i++)
                {
                    String tLang = (String)mLangs.elementAt(i);
                    setLanguage(tLang);
                    super.workFile(aFile, false);
                }
                super.deleteTempFiles();
            }
            else
            {
                setLanguage(null);
                super.workFile(aFile);
                super.deleteTempFiles();
            }
        } else {
            File tTargetFile = new File(computeNewFilePath(aFile));
            ensurePathForFileExists(tTargetFile);
            if(tTargetFile.exists()) {
                tTargetFile.delete();
            }
            try {
                FileUtils.copy(aFile, tTargetFile);
            } catch(IOException e) {
                mErrors++;
                System.err.println(e);
            }
            System.out.println("copied to " + tTargetFile);
        }
    }



    public static void main (String[] args)
    {
        try{
            // @@@ TODO : JD: I'm not sure if this number is still ok. check
            if (args.length < 2)
            {
                TemplatesProcessor.printUsage();
                // @@@ JD: There should be a System.exit() here when the number is fixed.
            }
            else
            {
                System.out.println("Attention:::Each localized directory has to be in the TemplProcessor.properties file!");
                System.out.println("Else you will get a NullPointerException of your LocalizerBean!");
                System.out.println();
                //Ensures all mandantory properties are included
                Properties tProps = Converter.getParams(args);

                //users host
                String tHost = tProps.getProperty("host");

                //users port
                String tPort = tProps.getProperty("port");
                String tConfigDir = tProps.getProperty("configDir");
                String tStartDir = tProps.getProperty("startDir");
                String tTempDir = tProps.getProperty("tempDir");
                // true if we are using tomcat, this parameter is optional
                String tTomcat = tProps.getProperty("tomcat");
                boolean tComments = false;
                // fetch make portal.properties
                Properties tFileProps = new Properties();
                try {
                    tFileProps.load(new FileInputStream("/home/sipdev/KFM/config" + "/" + tProps.getProperty("propFilePath") + "/MakePortal.properties"));
                } catch(IOException e) {
                    e.printStackTrace();
                }
                // step through the ifdef params to check if comments flag is set
                PropertiesNodeBuilder tBuilder = new PropertiesNodeBuilder(tFileProps);
                PropertiesNode tRoot = tBuilder.load();
                PropertiesNode tChild = tRoot.getChild("ifdefparam");
                if (tChild != null)
                {
                    PropertiesNode[] tChildren = tChild.getChildren();
                    for (int i = 0; i < tChildren.length; i++)
                    {
                        PropertiesNode tNode = tChildren[i];

                        String tValue = ((String)tNode.getValue()).trim();
                        if (tValue.startsWith("comments"))
                        {
                            // no value of 'false' found so comments are on
                            if (tValue.indexOf("false") == -1)
                            {

                                tComments = true;
                                break;
                            }
                        }

                    }
                }
                if ((tHost == null)
                    || (tPort == null)
                    || (tConfigDir == null)
                    || (tStartDir == null)
                    || (tTempDir == null))
                    TemplatesProcessor.printUsage();
                tProps.remove("host");
                tProps.remove("port");
                tProps.remove("configDir");
                tProps.remove("startDir");
                tProps.remove("tempDir");
                if (tTomcat != null)
                    tProps.remove("tomcat");

                tComments = false;

                // Resin for example can handle long filenames and there is need to copy
                // them to temporary files with shorter filenames.
                if(!(tTomcat == null || tTomcat.equals("")) && /* tomcat param provided */
                    Boolean.valueOf(tTomcat).equals(Boolean.FALSE) /* and it must be false */) {
                    AbstractPreprocessor.setTomcat(false);
                }

                TemplatesProcessor tProcessor = new TemplatesProcessor(
                    tHost,
                    tPort,
                    tConfigDir,
                    tProps,
                    tTempDir);
                tProcessor.setCommentsOn(tComments);

                tProcessor.traverse(tStartDir);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
TOP

Related Classes of KFM.preprocessor.TemplatesProcessor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.