1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package com.unitesk.atp.treedl;
33
34 import java.io.File;
35 import java.io.FileNotFoundException;
36 import java.io.FileOutputStream;
37 import java.io.IOException;
38 import java.io.OutputStream;
39 import java.util.Enumeration;
40 import java.util.HashMap;
41 import java.util.Iterator;
42 import java.util.Map;
43 import java.util.Properties;
44
45 import com.unitesk.atp.messages.FileNotFoundMessage;
46 import com.unitesk.atp.messages.FileStatusMessage;
47 import com.unitesk.atp.messages.IOErrorMessage;
48 import com.unitesk.atp.messages.OutputFileStatus;
49 import com.unitesk.atp.messages.UpToDateFileStatus;
50 import com.unitesk.atp.tree.tool.Tool;
51 import com.unitesk.atp.treedl.TDL.Module;
52
53 public class Messages2Action extends FileGeneratorAction
54 {
55 public static final String PROPERTY_UPDATE = "update";
56 public static final String DEFAULT_UPDATE = "true";
57 public static final String PROPERTY_OUTPUT_DIR = "output.dir";
58 public static final String DEFAULT_OUTPUT_DIR = ".";
59 public static final String TDL_PROPERTY_SUFFIX = "suffix";
60 public static final String TDL_DEFAULT_SUFFIX = "message";
61 public static final String TDL_PROPERTY_PATTERN = "pattern";
62 public static final String TDL_PROPERTY_GROUP = "group";
63
64 /***
65 * {@inheritDoc}
66 */
67 public String getVersion()
68 {
69 return getRevision( "$Revision: 1.1 $" ) + "-beta";
70 }
71
72 public void process( com.unitesk.atp.tree.tool.Tree tree )
73 {
74 setCurrentTree( tree );
75
76 TDL_Module module = (TDL_Module)tree;
77
78 String outputDir = getProperty( PROPERTY_OUTPUT_DIR, DEFAULT_OUTPUT_DIR );
79
80 boolean update
81 = getProperty( PROPERTY_UPDATE, DEFAULT_UPDATE ).equals( DEFAULT_UPDATE );
82
83 String name = getName();
84 Module root = module.getRoot();
85 String suffix = root.getProperty( name + '.' + TDL_PROPERTY_SUFFIX
86 , TDL_DEFAULT_SUFFIX
87 );
88
89 String patternName = name + '.' + TDL_PROPERTY_PATTERN;
90 int patternNameLen = patternName.length();
91
92 Map
93
94 for( int i = 0; i < root.sizeOptMemberList(); i++ )
95 {
96 TDL.ModuleMember member = root.getOptMemberList( i );
97 if( member instanceof TDL.NodeTypeDecl )
98 {
99 TDL.NodeTypeDecl nodeType = (TDL.NodeTypeDecl)member;
100
101 Properties props = nodeType.getOptProperties();
102
103 if( props != null )
104 {
105 for( Enumeration propNames = props.propertyNames()
106 ; propNames.hasMoreElements()
107 ;
108 )
109 {
110 String propName = (String)propNames.nextElement();
111 if( propName.startsWith( patternName )
112 && ( propName.length() == patternNameLen
113 || propName.charAt( patternNameLen ) == '.'
114 )
115 )
116 {
117 String locale;
118 String sfx;
119 if( propName.length() > patternNameLen )
120 {
121 locale = propName.substring( patternNameLen + 1 );
122 int dot = locale.indexOf( '.' );
123 if( dot >= 0 )
124 {
125 sfx = locale.substring( dot + 1 );
126 locale = locale.substring( 0, dot );
127 } else {
128 sfx = suffix;
129 }
130 } else {
131 locale = "";
132 sfx = suffix;
133 }
134 addProperty( propertiesMap
135 , locale
136 , nodeType.getName().getValue() +'/' + sfx
137 , nodeType.getProperty( propName )
138 );
139 }
140 }
141 }
142 }
143 }
144 File srcFile = tree.getFile();
145 String outName = JavaTextGenerator.getOutFileName( outputDir, root.getName().toString() ) + ".properties";
146 Iterator keyIterator = propertiesMap.keySet().iterator();
147 while( keyIterator.hasNext() )
148 {
149 String locale = (String)keyIterator.next();
150 Properties props = (Properties)propertiesMap.get( locale );
151 String dstFileName = outName;
152 if( locale.length() > 0 ) dstFileName += '_' + locale;
153 File dstFile = new File( dstFileName );
154 if( update
155 && srcFile.lastModified()
156 < dstFile.lastModified()
157 )
158 {
159 mbox.info( new FileStatusMessage( UpToDateFileStatus.status
160 , dstFile.toString()
161 )
162 );
163 } else {
164 mbox.info( new FileStatusMessage( OutputFileStatus.status
165 , dstFile.toString()
166 )
167 );
168 File parentFile = dstFile.getParentFile();
169 if( parentFile != null ) parentFile.mkdirs();
170 try
171 {
172 Tool t = (Tool)pm;
173 String comment = t.getName() + '-' + t.getVersion() + ", "
174 + getName() + '-' + getVersion() + ' '
175 + srcFile;
176 OutputStream out = new FileOutputStream( dstFile.getCanonicalPath() );
177 props.store( out, comment );
178 out.close();
179 }
180 catch( FileNotFoundException e )
181 {
182 mbox.error( new FileNotFoundMessage( dstFile ) );
183 }
184 catch( IOException e )
185 {
186 mbox.error( new IOErrorMessage( dstFile ) );
187 }
188 }
189 }
190 }
191
192 private static void addProperty( Map propertiesMap, String locale, String name, String value )
193 {
194 Properties props = (Properties)propertiesMap.get( locale );
195 if( props == null )
196 {
197 props = new Properties();
198 propertiesMap.put( locale, props );
199 }
200 props.put( name, value );
201 }
202 }