JFigConverter.java :  » Development » jfig » org » igfay » jfig » Java Open Source

Java Open Source » Development » jfig 
jfig » org » igfay » jfig » JFigConverter.java
package org.igfay.jfig;

import java.io.File;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.igfay.util.FileUtility;
import org.igfay.util.PropertyUtility;

/**
 * @author conrad4
 *
 * Perform JFig substitution on an InputStream.
 * 
 */
public class JFigConverter {

  private static Logger log = Logger.getLogger(JFigParser.class);
  private static Logger logDetail = Logger.getLogger("ConverterDetail");
  
  public String convert(File file) throws JFigException { 
    String string = FileUtility.contentsOfFile(file);
    return convert(string);
  }


  public String convert(File file, File outputFile) throws JFigException { 
    String string = FileUtility.contentsOfFile(file);
    //log.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    //log.info("~string \n"+string);
    string = convert(string);
    FileUtility.stringToFile(string, outputFile);
    return string;
  }

  public String convert(InputStream inputStream) throws JFigException {
    String string = FileUtility.streamToString(inputStream);
    return convert(string);
  }

  public String convert(String value) throws JFigException {
    value = resolveSymbolicValues(value);
    value = resolvePropertyVariables(value);
    return value;
  }

  public void convert(InputStream inputStream, File file ) {
    return;
  }

  /**
   *  Replace all occurances of symbolic references with actual values. A
   *  symbolic reference looks like "&(section)key".
   *
   *@param  section  Description of Parameter
   *@param  key      Description of Parameter
   *@param  value    Description of Parameter
   */
  private String resolveSymbolicValues(String value) throws JFigException {
    logDetail.debug("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n  value " + value);
    if (value ==null ) {
      return null;
    } 
    
    String replacementValue = null;
    String replacementSection = null;
    String replacementKey = null;
    String start = null;
    String rest = null;
    logDetail.debug("~~~~~~~~~~~~~~~~~~~~~~~~~\n ");
    
    // Loop through value while it has a symbolic reference
    Pattern reGlobal = JFigParser.getRegexSubstitution();
        Matcher matcher = reGlobal.matcher(value);
    while ( (value.indexOf(JFigConstants.SECTION_START_DELIMITER) >= 0) && matcher.find()) {
      logDetail.debug("~!foundMatch ");

      // Get the values that we matched in our regular expression
      int index3 = matcher.start(3)-1;
      start = value.substring(0,index3);
      
      replacementSection = matcher.group(3);
      replacementKey = matcher.group(5);

      int index6 = matcher.end(6);
      rest = value.substring(index6);

      // Get the value for the section/key that we are referencing
      replacementValue = JFig.getInstance().getValue(replacementSection, replacementKey, "null");
      logDetail.debug("~  rValue " + replacementValue);

      // Substitute the reference with the new value.
      value = start + replacementValue + rest;
      logDetail.debug("~ value " + value);
          matcher = reGlobal.matcher(value);
    } // end while
        
        return value;
  }

  /**
   *  Replace all occurances of Property variables with properties.
   *  Property variable looks like "$PropertyVariable$".
   *
   *@param  section  Description of Parameter
   *@param  key      Description of Parameter
   *@param  value    Description of Parameter
   */
  private String resolvePropertyVariables(String value) {
    if (value == null) {
      return null;
    }
    String replacementValue = null;
    String replacementKey = null;
    String start = null;
    String rest = null;

    // Loop through value while it has a symbolic reference
        Matcher matcher = JFigParser.getRegexPropertyVariable().matcher(value);
    while ((value.indexOf(JFigConstants.PROPERTY_DELIMITER) >= 0) && matcher.find()) {
      logDetail.debug("~ value " + value);

      // Get the values that we matched in our regular expression
      int index3 = matcher.start(3)-1;
      start = value.substring(0,index3);

      // Get the value for the property that we are referencing
      replacementKey = matcher.group(3);
      replacementValue = PropertyUtility.getProperty(replacementKey, "");
      
      int indexRest = matcher.end(4);
      rest = value.substring(indexRest);
      logDetail.debug("~ --   rKey " + replacementKey + "  rest: " + rest);

      // Substitute the reference with the new value.
      value = start + replacementValue + rest;
      logDetail.debug("~ value " + value);
      matcher = JFigParser.getRegexPropertyVariable().matcher(value);

    }// end while

    return value;
  }



}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.