TKMarkupTree.java :  » Content-Management-System » webman » com » teamkonzept » publishing » markups » Java Open Source

Java Open Source » Content Management System » webman 
webman » com » teamkonzept » publishing » markups » TKMarkupTree.java
/*
 * $Header: /cvsroot/webman-cms/source/webman/com/teamkonzept/publishing/markups/TKMarkupTree.java,v 1.6 2001/06/11 09:14:10 alex Exp $
 *
 */
package com.teamkonzept.publishing.markups;

import java.util.*;

import com.teamkonzept.lib.*;
import org.apache.log4j.Category;

public class TKMarkupTree extends TKMarkupItemList
{
  private static final Category cat = Category.getInstance(TKMarkupTree.class);
  
  public void dump (TKVector dump, int level) {
  
    if (dump == null) return;
    
    Enumeration e = elements();
    while (e.hasMoreElements()) {
    
      Object elem = e.nextElement();
      if (elem == null) continue;
      
      if (elem instanceof String)
        dump.addElement (new TKMarkupDumpRecord(level,"String",elem.toString()));
        
      else if (elem instanceof TKMarkupNode)((TKMarkupNode) elem).dump (dump,level);
      else if (elem instanceof TKMarkupTree)((TKMarkupTree) elem).dump (dump,level);
      else if (elem instanceof TKMarkup) {

        TKMarkup markup = (TKMarkup) elem;
        dump.addElement(new TKMarkupDumpRecord(level,"Markup",markup.toDisplay()));
      }
    }
  }

  public String expand() {

    StringBuffer buffer = new StringBuffer ();

    Enumeration e = elements();
    while (e.hasMoreElements()) {
    
      Object elem = e.nextElement();
      if (elem == null) continue;
      
      if (elem instanceof String) buffer.append(elem);
      else if (elem instanceof TKMarkupNode) buffer.append (((TKMarkupNode) elem).expand());
      else if (elem instanceof TKMarkupTree) buffer.append (((TKMarkupTree) elem).expand());
      else if (elem instanceof TKMarkup) buffer.append (((TKMarkup) elem).expand());
    }

    return new String(buffer);
  }

  public String markupByName2tmpl (TKMarkupStatics statics, String name) {
  
    if (name == null) return " ";
    
    TKDefinedMarkup markup;

    try { markup = new TKDefinedMarkup (name,new TKHashtable (),-1); }
    catch (Exception ex) {
    
      cat.error("*** markupByName2tmpl: ", ex);
      return " ";
    }

    try { markup.complete (); } catch (Exception ex) {
    
      cat.error("*** markupByName2tmpl: ",ex);
    }

    return markup.convert2Tmpl();
  }

  public String doCrControl (String text) {

    TKMarkupStatics statics = TKMarkupStatics.setup();
    boolean doCrConversion = TKMarkupDefinition.doCrConversion (statics);
    String crMarkup = TKMarkupDefinition.crMarkup (statics);
    String crcrMarkup = TKMarkupDefinition.crcrMarkup (statics);

    if (!doCrConversion || (text == null)) return text;

    StringBuffer digest = new StringBuffer();
    int pos = 0;
    
    while (pos < text.length()) {
    
      char chr = text.charAt (pos);

      if ((chr != '\n') && (chr != '\r')) digest.append (chr);

      else {
        if (pos+1 < text.length())
          if ((chr == '\n') && (text.charAt (pos+1) == '\r')) pos++;
          else if ((chr == '\r') && (text.charAt (pos+1) == '\n')) pos++;
        
        if ((pos+1 < text.length()) && 
          ((text.charAt (pos+1) == '\n') || 
           (text.charAt (pos+1) == '\r'))) {

          pos++;
          chr = text.charAt (pos);

          if (pos+1 < text.length())
            if ((chr == '\n') && (text.charAt (pos+1) == '\r')) pos++;
            else if ((chr == '\r') && (text.charAt (pos+1) == '\n')) pos++;

          digest.append (markupByName2tmpl(statics,crcrMarkup));

        } else digest.append (markupByName2tmpl(statics,crMarkup));
      }

      pos++;
    }
      
    return new String (digest);
  }

  public String convert2Tmpl () {

    StringBuffer buffer = new StringBuffer ();

    Enumeration e = elements();
    while (e.hasMoreElements()) {
    
      Object elem = e.nextElement();
      if (elem == null) continue;
      
      if (elem instanceof String) buffer.append(doCrControl(TKMarkupParser.doEscapes((String) elem)));
      else if (elem instanceof TKMarkupNode) buffer.append (((TKMarkupNode) elem).convert2Tmpl());
      else if (elem instanceof TKMarkupTree) buffer.append (((TKMarkupTree) elem).convert2Tmpl());
      else if (elem instanceof TKMarkup) {
      
        TKMarkup markup = (TKMarkup) elem;
        buffer.append (markup.convert2Tmpl());

        if (elem instanceof TKDefinedMarkup) {

          if (!((TKDefinedMarkup) elem).isAtom)
            buffer.append ("</TK_TAG:").append(markup.name).append('>');

        } else buffer.append ("</").append(markup.name).append('>');
      }
    }

    return new String(buffer);
  }

  public String convert2Xml () {

    StringBuffer buffer = new StringBuffer ();

    Enumeration e = elements();
    while (e.hasMoreElements()) {
    
      Object elem = e.nextElement();
      if (elem == null) continue;
      
      if (elem instanceof String) buffer.append(doCrControl(elem.toString()));
      else if (elem instanceof TKMarkupNode) buffer.append (((TKMarkupNode) elem).convert2Xml());
      else if (elem instanceof TKMarkupTree) buffer.append (((TKMarkupTree) elem).convert2Xml());
      else if (elem instanceof TKMarkup) {
      
        TKMarkup markup = (TKMarkup) elem;
        buffer.append (markup.convert2Xml());

        if (!(elem instanceof TKDefinedMarkup) || 
            !((TKDefinedMarkup) elem).isAtom)
          buffer.append ("</").append(markup.name).append('>');
      }
    }

    return new String(buffer);
  }
}
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.