Package org.jostraca

Examples of org.jostraca.Template


  }


  protected boolean compileRequired( Template pTemplate ) {
    try {
      Template     tm              = pTemplate;
      boolean      compileRequired = true;
      PropertySet  tmps            = tm.getMergedPropertySet();
      StringBuffer reason          = new StringBuffer();
      File         executablePath  = pTemplate.getCodeWriterExecutablePath();

      // definitely compile
      if( tmps.isYes( Property.main_CompileCodeWriter ) ) {
        compileRequired = true;
        reason.append( "main.CompileCodeWriter="+tmps.get( Property.main_CompileCodeWriter ) );
      }

      // definitely do not compile
      else if( tmps.isNo( Property.main_CompileCodeWriter ) ) {
        compileRequired = false;
        reason.append( "main.CompileCodeWriter="+tmps.get( Property.main_CompileCodeWriter ) );
      }

      // otherwise, compile only if needed
      else if( tmps.isYes( Property.lang_HasExecutable ) ) {

        // no executable => definitely compile
        if( ! executablePath.exists() ) {
          compileRequired = true;
          reason.append( "executable not found: "+executablePath );
        }

        // code writer changed => definitely compile
        else if( tm.getCodeWriterChanged() ) {
          compileRequired = true;
          reason.append( "CodeWriter changed" );
        }
       
        // no changes, so use the existing executable
View Full Code Here


    PropertySet ps = new PropertySet();
    ps.set( Property.main_MakeBackup,        Standard.NO  );
    ps.set( Property.main_CompileCodeWriter, Standard.YES );
    ps.set( Property.main_ExecuteCodeWriter, Standard.YES );
    g.setCmdPropertySet( ps );
    Template tm = g.generate( FileUtil.findFile( "org/jostraca/test/simple.jtm" ) );

    RecordingUserMessageHandler rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
    assertTrue( null != rumh );
    assertEquals( 7, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
    assertEquals( 2, ((Vector)rumh.get( UserMessageHandler.INFO  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );

    simple_txt = new File( "simple.txt" );
    System.out.println(simple_txt.getAbsolutePath());
    assertTrue( simple_txt.exists() );

    assertEquals( "simple: simple", FileUtil.readFile( simple_txt ).trim() );
    assertEquals( "simple: simple", ((String)tm.getResult()).trim() );   
  }
View Full Code Here

    }

    Generator   g  = new Generator();
    PropertySet ps = Jostraca.parseCommandLine( "-Bcg -Dfoo=bar", new ArrayList(), new ArrayList() );
    g.setCmdPropertySet( ps );
    Template tm = g.generate( new File( "src/org/jostraca/test/cmdargs.jtm" ) );

    RecordingUserMessageHandler rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
    assertTrue( null != rumh );
    //System.out.println( rumh );
    assertEquals( 7, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
    assertEquals( 2, ((Vector)rumh.get( UserMessageHandler.INFO  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );

    cmdargs_txt = new File( "cmdargs.txt" );
    assertTrue( cmdargs_txt.exists() );

    assertEquals( "cmdargs: cmdargs, foo: bar", FileUtil.readFile( cmdargs_txt ).trim() );
    assertEquals( "cmdargs: cmdargs, foo: bar", ((String)tm.getResult()).trim() );   
  }
View Full Code Here

    List         tmlist       = pTemplateList;
    StringBuffer sourceFilesB = pSourceFilesBuffer;
    boolean      compile      = false;

    for( Iterator tmT = tmlist.iterator(); tmT.hasNext(); ) {
      Template tm  = (Template) tmT.next();

      File executablePath = makeExecutablePath( tm );
      tm.setCodeWriterExecutablePath( executablePath );

      boolean compileRequired = compileRequired( tm );

      if( compileRequired ) {
        File cwF = tm.getCodeWriterPath();
        sourceFilesB.append( " "+TextUtil.quoteSpaces( cwF.getPath() ) );
        pSourceFilesList.add( cwF.getPath() );
        WayPointRecorder.add( BasicWayPoint.CompilingCodeWriter.make( cwF.getAbsolutePath() ) );
        compile = true;
      }
View Full Code Here

    // do nothing
  }


  protected void processImpl( Template pTemplate ) {
    Template    template   = pTemplate;
    PropertySet tmps       = template.getMergedPropertySet();

    boolean                     successful = false;
    PrintStream                 sysOut     = null;
    PrintStream                 sysErr     = null;
    RecordingUserMessageHandler rumh       = new RecordingUserMessageHandler();
    Object                      result     = null;

    try {
      String   cwo  = makeCodeWriterOptions( tmps, template );
      String[] args = ArgUtil.splitQuoted( cwo );

      // DO NOT use CommandLineUserMessageHandler here - causes infinite recursion
      MessageHandlerOutputStream  out  = new MessageHandlerOutputStream( UserMessageHandler.INFO,  rumh );
      MessageHandlerOutputStream  err  = new MessageHandlerOutputStream( UserMessageHandler.ERROR, rumh );

      // space out dots to avoid accidental replace on System DOT out search
      sysOut = System . out;
      sysErr = System . err;
   
      // set context if not null
      Object context = template.getContext();

      boolean throwWriterExceptions = tmps.isYes( Property.main_CodeWriter_throwExceptions );

      String script = template.getCodeWriterSource();
     
      Context cx = Context.enter();

      // DO NOT put System.out in MessageHandler as this will cause infinite loop
      System.setOut( new PrintStream( out ) );
      System.setErr( new PrintStream( err ) );

      Scriptable scope = cx.initStandardObjects();
      cx.evaluateString(scope, script, "CodeWriter Line Number: ", 1, null);     

      Object codewriterObj = scope.get( "codewriter", scope );
      if( null == codewriterObj || !(codewriterObj instanceof Scriptable) ) {
        throw new RuntimeException( "no codewriter in script" );
      }
      Scriptable codewriter = (Scriptable) codewriterObj;

      Object _setContextObj = codewriter.get( "_setContext", codewriter );
      if( null == _setContextObj || !(_setContextObj instanceof Function) ) {
        throw new RuntimeException( "no codewriter._setContext in script" );
      }

      Function _setContext = (Function) _setContextObj;
      _setContext.call(cx,scope,codewriter,new Object[]{context});

      Object _generateObj = codewriter.get( "_generate", codewriter );
      if( null == _generateObj || !(_generateObj instanceof Function) ) {
        throw new RuntimeException( "no codewriter._generate in script" );
      }

      Function _generate = (Function) _generateObj;
      result = _generate.call(cx,scope,codewriter,new Object[]{args,new Boolean(throwWriterExceptions)});

      template.setResult( result );
    }

    catch( StandardException se ) {
      if( null != sysOut ) {
        System.setOut( sysOut );
View Full Code Here

    // do nothing
  }


  protected void processImpl( Template pTemplate ) {
    Template    template   = pTemplate;
    PropertySet tmps       = template.getMergedPropertySet();

    File   codeWriterPath   = template.getCodeWriterPath();
    String codeWriterSource = template.getCodeWriterSource();


    try {
      String externalController        = makeExternalController( tmps );
      String externalControllerOptions = makeExternalControllerOptions( tmps );
      String codeWriter                = makeCodeWriter( tmps );
      String codeWriterOptions         = makeCodeWriterOptions( tmps, template );

      String cmd
        = externalController + Standard.SPACE
        + externalControllerOptions + Standard.SPACE
        + codeWriter + Standard.SPACE
        + codeWriterOptions
        ;
     
      boolean successful = executeGeneratingCmd( cmd, template );
      WayPointRecorder.add( BasicWayPoint.ExecutingCodeWriter.make( template.getCodeWriterPath().getAbsolutePath() ) );
    }
    catch( StandardException se ) {
      throw se;
    }
    catch( Exception e ) {
View Full Code Here

    assertTrue( content.equals( tahs.getLastAppendedContent() ) );
  }


  private BasicTemplateElementProcessor create( TemplateActionHandler pTemplateActionHandler ) throws Exception {
    Template    tm = new BasicTemplate();
    PropertySet ps   = new PropertySet();
    ps.set( "o", "<%" );
    BasicTemplateElementProcessor btep = new BasicTemplateElementProcessor( pTemplateActionHandler, ps, tm );
    return btep;
  }
View Full Code Here

    mFormatManager = pFormatManager;
  }
 

  public void process( Template pTemplate ) throws Exception {
    Template tm = (Template) Internal.null_arg( pTemplate );
    processImpl( tm );
  }
View Full Code Here

      }
      if( !(to instanceof Template) ) {
        ProcessException.CODE_not_tm( "stage", getName(), "index", ""+tI, "object", String.valueOf(to) );
      }

      Template tm = (Template) to;
      process( tm );
    }

    // complete templates
    int numTH = iTemplateHandlers.size();
View Full Code Here

    // do nothing
  }


  protected void processImpl( Template pTemplate ) {
    Template    template   = pTemplate;
    PropertySet tmps       = template.getMergedPropertySet();

    boolean     successful = false;
    PrintStream sysOut     = null;
    PrintStream sysErr     = null;
    RecordingUserMessageHandler rumh = new RecordingUserMessageHandler();

    try {
      String   cw   = makeCodeWriter( tmps );
      String   cwo  = makeCodeWriterOptions( tmps, template );
      String[] args = ArgUtil.splitQuoted( cwo );

      String jcwp = getCodeWriterPath( tmps );
      jcwp = TextUtil.replace( jcwp, ".java", ".class" );
      File jcwpf = new File( jcwp );
      String cwp = jcwpf.getAbsolutePath();

      mUserMessageHandler.debug( UserText.get(UserText.TXT_generating), "class-file:"+cwp+Standard.SPACE+cwo );

      // NOTE: has to be _generate to prevent exit
      Class  cwClass        = ClassUtil.makeClassFromFile( cwp );
      Object cwInstance     = cwClass.newInstance();
      Method generateMethod = cwClass.getMethod( "_generate", new Class[] { (new String[] {}).getClass(), Boolean.TYPE } );

      // DO NOT use CommandLineUserMessageHandler here - causes infinite recursion
      MessageHandlerOutputStream  out  = new MessageHandlerOutputStream( UserMessageHandler.INFO,  rumh );
      MessageHandlerOutputStream  err  = new MessageHandlerOutputStream( UserMessageHandler.ERROR, rumh );

      // space out dots to avoid accidental replace on System DOT out search
      sysOut = System . out;
      sysErr = System . err;
   
      // set context if not null
      Object context = template.getContext();
      if( null != context ) {
        Method setContextMethod = cwClass.getMethod( "_setContext", new Class[] { Object.class } );
        setContextMethod.invoke( cwInstance, new Object[] { context } );
      }

      boolean throwWriterExceptions = tmps.isYes( Property.main_CodeWriter_throwExceptions );

      // DO NOT put System.out in MessageHandler as this will case infinite loop
      System.setOut( new PrintStream( out ) );
      System.setErr( new PrintStream( err ) );

      Object result = generateMethod.invoke( cwInstance, new Object[] { args, new Boolean(throwWriterExceptions) } );
      WayPointRecorder.add( BasicWayPoint.ExecutingCodeWriter.make( template.getCodeWriterPath().getAbsolutePath() ) );

      if( result instanceof Integer ) {
        successful = ( 0 == ((Integer)result).intValue() );
      }
      else {
        successful = true;
      }

      template.setResult( result );
    }

    catch( InvocationTargetException ite ) {
      if( null != sysOut ) {
        System.setOut( sysOut );
View Full Code Here

TOP

Related Classes of org.jostraca.Template

Copyright © 2018 www.massapicom. 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.