Package org.jostraca.process

Source Code of org.jostraca.process.GenericParser

/*
* Name:    GenericParser
* Authors: Richard Rodger
*
* Copyright (c) 2004 Richard Rodger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/


// package
package org.jostraca.process;


// import
import org.jostraca.Property;
import org.jostraca.Template;
import org.jostraca.TemplateParser;
import org.jostraca.UserText;
import org.jostraca.Constants;

import org.jostraca.section.SectionSet;
import org.jostraca.section.Section;

import org.jostraca.util.InstanceManager;
import org.jostraca.util.PropertySet;
import org.jostraca.util.Standard;
import org.jostraca.util.UserMessageHandler;

import java.util.List;


/** Processing class for parsing templates.
*/
public class GenericParser extends TemplateHandlerSupport {

  protected InstanceManager iTemplateParserIM = new InstanceManager();


  public GenericParser() {
    // do nothing
  }


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

    String src = template.getSource();
    TemplateParser templateParser = getTemplateParser( tmps );
    templateParser.setPropertySet( tmps );
    templateParser.setSectionSet( sectionSet );
    templateParser.setSourceText( src );

    iUserMessageHandler.debug( UserText.get(UserText.TXT_parsing), src.length()+" characters" );

    // FIX: how about .parse(template) and remove sets above?
    templateParser.parse();

    sectionSet = templateParser.getSectionSet();
    template.setSectionSet( sectionSet );

    // FIX: do we need a way to stop processing?
    boolean displaytmdoc = displayTemplateDocumentation( tmps, sectionSet, iUserMessageHandler );
    if( displaytmdoc ) {
      PropertySet ops = pTemplate.getPropertySet( Constants.CONF_override );
      ops.set( Property.main_CompileCodeWriter, Standard.NO );
      ops.set( Property.main_ExecuteCodeWriter, Standard.NO );
    }
   
  }


  protected void completeImpl( List pTemplateList ) {
    // do nothing
  }



  protected TemplateParser getTemplateParser( PropertySet pPropertySet ) {
    String tmpp = Property.main_TemplateParser;
    if( pPropertySet.has( tmpp ) ) {
      String templateParserClassName = pPropertySet.get( Property.main_TemplateParser );
      TemplateParser tp = (TemplateParser) iTemplateParserIM.getInstance( templateParserClassName );
      return tp;
    }
    else {
      throw ProcessException.CODE_prop_missing( "propname", tmpp );
    }
  }



  /** Display template documentation if it exists */
  public boolean displayTemplateDocumentation( PropertySet pPropertySet, SectionSet pSectionSet,
                                               UserMessageHandler pUserMessageHandler )
  {
    if( pPropertySet.isYes( Property.main_ShowDocumentation ) ) {
      String documetationText = pSectionSet.getSection( Section.NAME_documentation ).getContent();
      if( 0 < documetationText.length() ) {
        iUserMessageHandler.info( documetationText );
      }
      else {
        iUserMessageHandler.info( "No documentation available." );
      }
      return true;
    }
    else {
      return false;
    }
  }


}





TOP

Related Classes of org.jostraca.process.GenericParser

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.