View Javadoc

1   /*
2    * Created on 2003-5-23 17:06:28 by joel guo
3    *
4    * vTradEx Information Technology Inc.
5    */
6   package com.cyclops.albumbuilder;
7   import java.io.File;
8   import java.io.FileWriter;
9   import java.util.ArrayList;
10  import java.util.Collection;
11  import java.util.Iterator;
12  import java.util.List;
13  import java.util.Set;
14  import java.util.TreeMap;
15  
16  import org.apache.commons.lang.StringUtils;
17  import org.apache.stratum.lifecycle.Executable;
18  import org.apache.velocity.VelocityContext;
19  import org.apache.velocity.app.Velocity;
20  /***
21   * Add description for this class <font color="red">HERE</font>!
22   * <big><font face="黑体">惩前毖后,治病救人</font></big>
23   * 
24   * @author <a href="mailto:[email protected]">joel guo</a>
25   * @company <a href="http://www.vtradex.com">vTradEx</a>
26   * @since 2003-5-23 17:06:28
27   */
28  public class GalleryIndexGenerator
29      extends AlbumBuilderComponent
30      implements Executable {
31      private static final String SEPARATOR = "/";
32      private boolean copySource;
33      private String encoding;
34      private String indexTemplate;
35      private int thumbnailColumns;
36      private String xdocsDirectory;
37      private String imageLink;
38      private String imageLinkPrefix;
39      /*** Override method execute() of super class
40       * @see org.apache.stratum.lifecycle.Executable#execute()
41       */
42      public void execute() throws Exception {
43          Set paths = getSourceRepository().keySet();
44          for (Iterator i = paths.iterator(); i.hasNext();) {
45              String relativePath = (String) i.next();
46              String targetFile =
47                  xdocsDirectory + relativePath + "thumbnail_index.xml";
48              File file = new File(targetFile);
49              if (!file.getParentFile().isDirectory()) {
50                  file.getParentFile().mkdirs();
51                  logger.info("Create directory " + file.getParentFile());
52              }
53              FileWriter fw = new FileWriter(targetFile);
54              VelocityContext ctx = new VelocityContext();
55              ctx.put("images", getChildResources(relativePath));
56              ctx.put("parents", getParentPackages(relativePath));
57              ctx.put("packageName", getPackageName(relativePath));
58              ctx.put(
59                  "isRoot",
60                  new Boolean(StringUtils.equals(relativePath, SEPARATOR)));
61              ctx.put("children", getChildPackages(relativePath));
62              ctx.put("imageLink", imageLink);
63              ctx.put("imageLinkPrefix", imageLinkPrefix);
64              ctx.put("columns", new Integer(thumbnailColumns));
65              Velocity.mergeTemplate(indexTemplate, encoding, ctx, fw);
66              fw.flush();
67              fw.close();
68              logger.debug("Generate gallery index xdoc file " + targetFile);
69          }
70      }
71      private List getChildPackages(String relativePath) {
72          List tm = new ArrayList();
73          int thisDepth = StringUtils.countMatches(relativePath, SEPARATOR);
74          for (Iterator i = getSourceRepository().keySet().iterator();
75              i.hasNext();
76              ) {
77              String packagePath = (String) i.next();
78              >if (!packagePath.startsWith(relativePath)) {
79                  continue;
80              }
81              int depth = StringUtils.countMatches(packagePath, SEPARATOR);
82              if (depth != thisDepth + 1) {
83                  continue;
84              }
85              String packageName = getPackageName(packagePath);
86              tm.add(packageName);
87          }
88          return tm;
89      }
90      private List getChildResources(String relativePath) {
91          List images = new ArrayList();
92          for (Iterator i =
93              ((Collection) getSourceRepository().get(relativePath)).iterator();
94              i.hasNext();
95              ) {
96              SourceResource res = (SourceResource) i.next();
97              if (res != null) {
98                  images.add(res);
99              }
100         }
101         return images;
102     }
103     private String getPackageName(String path) {
104         String[] parts = StringUtils.split(path, SEPARATOR);
105         if (parts.length == 0) {
106             return "ROOT";
107         } else {
108             return parts[parts.length - 1];
109         }
110     }
111     private List getParentPackages(String relativePath) {
112         TreeMap tm = new TreeMap();
113         for (Iterator i = getSourceRepository().keySet().iterator();
114             i.hasNext();
115             ) {
116             String packagePath = (String) i.next();
117             >if (!relativePath.startsWith(packagePath)) {
118                 continue;
119             }
120             >if (StringUtils.equals(relativePath, packagePath)) {
121                 continue;
122             }
123             String[] parts = StringUtils.split(packagePath, SEPARATOR);
124             if (parts.length == 0) {
125                 continue;
126             }
127             String packageName = parts[parts.length - 1];
128             tm.put(packagePath, packageName);
129         }
130         return new ArrayList(tm.values());
131     }
132     /*** Override method initialize() of super class
133      * @see org.apache.stratum.lifecycle.Initializable#initialize()
134      */
135     public void initialize() {
136         super.initialize();
137         xdocsDirectory =
138             getConfiguration().getString(
139                 "target.xdocsdir",
140                 "target/generated-xdocs/album");
141         indexTemplate =
142             getConfiguration().getString(
143                 "generation.index",
144                 "gallery_index.vm");
145         encoding =
146             getConfiguration().getString(
147                 "generation.index.encoding",
148                 System.getProperty("file.encoding", "8859_1"));
149         copySource =
150             getConfiguration().getBoolean("generation.copysource", false);
151         thumbnailColumns =
152             getConfiguration().getInt("generation.thumbnail.columns", 5);
153         imageLink =
154             getConfiguration().getString("generation.imagelink", "direct");
155         imageLinkPrefix =
156             getConfiguration().getString("generation.imagelink.prefix", "");
157     }
158 }