躺在沙灘上的小豬

          快樂的每一天

          菜單的生成。

          需要生成:
          http://www.softcomplex.com/products/tigra_menu_tree/ 

          最終生成的菜單:

           

          首先按需求定義了一個model.

          package com.jxlt.db.parse; 
          import java.io.Serializable; 
          
          public class TreeModel implements Serializable { 
                  private static final long serialVersionUID = 7896562831509010976L; 
          
                  private String code; 
                  private String title; 
                  private String url; 
                  private boolean leaf; 
                  private int level; 
          
                  /** 
                  * @return Returns the leaf. 
                  */ 
                  public boolean isLeaf() { 
                          return leaf; 
                  } 
          
                  /** 
                  * @param leaf 
                  *            The leaf to set. 
                  */ 
                  public void setLeaf(boolean leaf) { 
                          this.leaf = leaf; 
                  } 
          
                  /** 
                  * @return Returns the title. 
                  */ 
                  public String getTitle() { 
                          return title; 
                  } 
          
                  /** 
                  * @param title 
                  *            The title to set. 
                  */ 
                  public void setTitle(String title) { 
                          this.title = title; 
                  } 
          
                  /** 
                  * @return Returns the url. 
                  */ 
                  public String getUrl() { 
                          return url; 
                  } 
          
                  /** 
                  * @param url 
                  *            The url to set. 
                  */ 
                  public void setUrl(String url) { 
                          this.url = url; 
                  } 
          
                  /** 
                  * @return Returns the code. 
                  */ 
                  public String getCode() { 
                          return code; 
                  } 
          
                  /** 
                  * @param code 
                  *            The code to set. 
                  */ 
                  public void setCode(String code) { 
                          this.code = code; 
                  } 
          
                  /** 
                  * @return Returns the level. 
                  */ 
                  public int getLevel() { 
                          return level; 
                  } 
          
                  /** 
                  * @param level 
                  *            The level to set. 
                  */ 
                  public void setLevel(int level) { 
                          this.level = level; 
                  } 
          
          }

          獲取內容:

          package com.jxlt.db.parse; 
          
          import java.sql.Connection; 
          import java.sql.ResultSet; 
          import java.sql.SQLException; 
          import java.sql.Statement; 
          import java.util.ArrayList; 
          import java.util.List; 
          
          import com.jxlt.db.util.DataSourceUtils; 
          
          public class ParseTree { 
                  private static final String sql = "select t.bz_code code, 
                                                    t.bz_title itle, t.bz_content content "
                                           + "  from bz_czzs t " 
                                           + "  order by t.bz_code"; 
          
                  private static final String sql2 = "select t.bz_code code, 
                                                     t.bz_title title, 
                                                     t.bz_content content " 
                                                     + "  from bz_czzs t " 
                                           + " where substr(t.bz_code, 0, 3) = '002'" 
                                           + " order by t.bz_code"; 
          
                  public static List parse() { 
                          Connection conn = null; 
                          Statement st = null; 
                          ResultSet rs = null; 
                          List list = new ArrayList(); 
                          try { 
                                  conn = DataSourceUtils.getConnection(); 
                                  st = conn.createStatement(); 
          
                                  rs = st.executeQuery(sql2); 
                                  String code, title = ""; 
          
                                  while (rs.next()) { 
                                          code = rs.getString("code"); 
                                          title = rs.getString("title"); 
                 
                                          TreeModel model = new TreeModel(); 
                                          model.setCode(code); 
                                          model.setTitle(title); 
          
                                          model.setUrl(
                                           "m/content/" + code + ".html"); 
                                          model.setLeaf(false); 
                                          model.setLevel(
                                          code != null ? code.length() / 3 : 0); 
                                          list.add(model); 
                                  } 
          
                          } catch (SQLException e) { 
                                  e.printStackTrace(); 
                          } finally { 
                                  // DataSourceUtils.close(rs); 
                                  // DataSourceUtils.close(st); 
                                  DataSourceUtils.close(conn); 
                          } 
                          return list; 
                  } 
          }

          生成js文件

          package com.jxlt.db.parse; 
          
          import org.apache.log4j.Logger; 
          
          import java.util.List; 
          
          /** 
          * Generate the left tree menu 
          * 
          * @author martin.xus (martin.xus@gmail.com) 
          */ 
          public class GeneratorTree { 
                  private static Logger logger = Logger.getLogger(GeneratorTree.class); 
          
                  public static final String TREE_FILE = "D:\\workspace\\style\\test\\martin.tree.002.js"; 
          
                  /** 
                  * Generate the left tree menu 
                  * 
                  * @return String 
                  */ 
                  public static String generator() { 
                          StringBuffer buf = new StringBuffer().append(HEADER); 
                          List list = ParseTree.parse(); 
                          logger.debug("generating tree menu begin"); 
                          TreeModel model; 
                          TreeModel modelNext; 
                          int position; 
                          if (null != list && 0 < list.size()) { 
                                  for (int i = 0, length = list.size(); i < length - 1; i++) { 
                                          logger.debug("dealed with :" + i + " rows"); 
                                          StringBuffer _buf = new StringBuffer(); 
                                          model = (TreeModel) list.get(i); 
                                          modelNext = (TreeModel) list.get(i + 1); 
                                          _buf.append("['").append(model.getTitle()).append("','") 
                                                          .append(model.getUrl()).append("'"); 
          
                                          if (model.getLevel() > modelNext.getLevel()) { 
                                                  _buf.append(SUFFIX).append(COMMA); 
                                          } else if (model.getLevel() == modelNext.getLevel()) { 
                                                  _buf.append(SUFFIX).append(COMMA).append(PLACEKICK); 
                                          } else if (model.getLevel() < modelNext.getLevel()) { 
                                                  _buf.append(COMMA).append(PLACEKICK).append(SUFFIX); 
                                          } 
          
                                          position = buf.toString().indexOf(PLACEKICK); 
                                          if (position != -1) { 
                                                  buf.replace(position, position + 7, _buf.toString()); 
                                          } else { 
                                                  buf.insert(buf.length() - model.getLevel() - 4, "," 
                                                                  + _buf.toString()); 
                                          } 
          
                                          if (i == length - 2) { 
                                                  _buf.append("['").append(modelNext.getTitle()) 
                                                                  .append("','").append(modelNext.getUrl()).append( 
                                                                                  "'").append(SUFFIX).append(COMMA); 
                                          } 
                                  } 
                          } 
                          return buf.toString(); 
                  } 
          
                  private static final String SUFFIX = "]"; 
          
                  private static final String COMMA = ","; 
          
                  private static final String PLACEKICK = "$martin"; 
          
                  private static final String HEADER = "var TREE_ITEMS = [['索引','#'," 
                                  + PLACEKICK + "]];"; 
          }

          test 一下:

          logger.debug("writing file:" +
                             GeneratorTree.TREE_FILE); 
          FileWriter writer = 
                             new FileWriter(GeneratorTree.TREE_FILE); 
          writer.write(GeneratorTree.generator()); 
          writer.flush(); 
          writer.close(); 
          logger.debug("end");

          菜單js樣本
          一:

          var TREE_ITEMS = [
          	['index', '#',
          		['001', '#001',
          			
          			['001001', '#001001',
          				['001001001', '#001001001'],
          			],
          			
          			['001002', '#001002',
          				['001002001', '#001002001',
          					['001002002', '#001002002'],
          				],
          			],
          			
          			['001003', '#001001003',
          				['001003001', '#001003001',
          					['001003001001', '#001003001001'],
          					['001003001002', '#001003001001',
          						['001003001002001', '#001003001002001'],					
          					],
          				],
          			],
          		],
          		['002', '#002'],
          	]
          ];

          posted on 2005-09-20 14:11 martin xus 閱讀(169) 評論(0)  編輯  收藏 所屬分類: java

          主站蜘蛛池模板: 堆龙德庆县| 五莲县| 体育| 景德镇市| 磴口县| 平远县| 黄冈市| 大姚县| 河源市| 清远市| 郯城县| 江都市| 视频| 太原市| 乌鲁木齐县| 栾城县| 盈江县| 临沂市| 满城县| 晴隆县| 宜春市| 方山县| 靖西县| 新源县| 涟水县| 儋州市| 彭阳县| 金寨县| 丘北县| 元朗区| 朝阳区| 泰兴市| 莱西市| 阳东县| 茂名市| 临沂市| 阳泉市| 建昌县| 乌鲁木齐县| 博乐市| 通城县|