KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > jwi > jgallery > util > VariablesTransformer


1 package de.jwi.jgallery.util;
2
3 /*
4  * jGallery - Java web application to display image galleries
5  *
6  * Copyright (C) 2004 Juergen Weber
7  *
8  * This file is part of jGallery.
9  *
10  * jGallery is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * jGallery is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with jGallery; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston
23  */

24
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.util.regex.Matcher JavaDoc;
30 import java.util.regex.Pattern JavaDoc;
31
32 /**
33  * VariablesTransformer tries to automatically replace variable names in JAlbum skins
34  * to JGallery variable names.
35  * @author Jürgen Weber
36  * Source file created on 01.03.2004
37  *
38  */

39 public class VariablesTransformer
40 {
41     private static String JavaDoc loadFile(InputStream JavaDoc is) throws IOException JavaDoc
42     {
43         byte[] bytes = new byte[is.available()];
44         is.read(bytes);
45         String JavaDoc s = new String JavaDoc(bytes);
46         return s;
47     }
48
49     public static void main(String JavaDoc[] args) throws IOException JavaDoc
50     {
51         if (args.length<1)
52         {
53             System.out.println(VariablesTransformer.class.getClass().getName()
54                     + " <file to transform>");
55             System.exit(1);
56         }
57         
58         InputStream JavaDoc is = VariablesTransformer.class
59                 .getResourceAsStream("variables.properties");
60
61         String JavaDoc vardefs = loadFile(is);
62
63         is = new FileInputStream JavaDoc(args[0]);
64
65         String JavaDoc s = loadFile(is);
66         String JavaDoc old,nuw;
67
68         // replace $text.aWord with <jg:text>aWord</jg:text>
69
String JavaDoc regex = "\\$text\\.\\w*";
70         
71         Pattern JavaDoc pattern = Pattern.compile(regex);
72         
73         Matcher JavaDoc matcher = pattern.matcher(s);
74
75         String JavaDoc s0 = new String JavaDoc(s);
76         
77         while(matcher.find())
78         {
79             old = "\\" + matcher.group();
80             int p = old.indexOf('.');
81             nuw = "<jg:text>" + old.substring(p+1)+ "</jg:text>";
82             s0 = s0.replaceAll(old,nuw);
83         }
84         
85         s = s0;
86
87         // replace other variables
88

89         String JavaDoc[] s1 = vardefs.trim().split("[{}]");
90
91         for (int i=0;i<s1.length;i+=2)
92         {
93             String JavaDoc clazz = s1[i].trim();
94             String JavaDoc vars = s1[i+1].trim();
95             
96             String JavaDoc[] v = vars.split("\\s+"); // whitespace
97

98             for (int j=0;j<v.length;j++)
99             {
100                 old = "\\$"+v[j];
101                 nuw = "\\${"+clazz+"."+v[j]+"}";
102                 //System.out.println("replacing "+old+" with "+ nuw);
103
s = s.replaceAll(old,nuw);
104                 
105                 old="exists=\""+v[j]+"\"";
106                 nuw = "exists=\"\\${"+clazz+"."+v[j]+"}\"";
107                 s = s.replaceAll(old,nuw);
108
109                 old="exists="+v[j];
110                 nuw = "exists=\"\\${"+clazz+"."+v[j]+"}\"";
111                 s = s.replaceAll(old,nuw);
112
113             }
114         }
115         
116         System.out.println("\n\n"+s);
117         
118         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(args[0]+".jsp");
119         
120         s0 = "<%@ taglib uri=\"http://www.jwi.de/jGallery/taglib\" prefix=\"jg\" %>\n\n";
121         
122         fos.write(s0.getBytes());
123
124         old="href=\"http://www.datadosen.se/jalbum\"";
125         nuw="href=\"\\${folder.generatorurl}\"";
126         s = s.replaceAll(old,nuw);
127         
128         old="<ja:include";
129         nuw="<jsp:include";
130         s = s.replaceAll(old,nuw);
131
132         old="</ja:include";
133         nuw="</jsp:include";
134         s = s.replaceAll(old,nuw);
135         
136         old="<ja:";
137         nuw="<jg:";
138         s = s.replaceAll(old,nuw);
139         
140         old="</ja:";
141         nuw="</jg:";
142         s = s.replaceAll(old,nuw);
143         
144         
145         fos.write(s.getBytes());
146         fos.close();
147           
148         if (s.indexOf("<jg:eval>")>0)
149         {
150             System.out.println("Please update <jg:eval> Tags with JSP EL.");
151         }
152         
153     }
154 }
155
Popular Tags