View Javadoc

1   /*
2    * Copyright (c) 2001-2006,
3    * RedVerst Group, ISP RAS http://www.ispras.ru
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are met:
8    *
9    * 1. Redistributions of source code must retain the above copyright notice, this
10   *    list of conditions and the following disclaimer.
11   *
12   * 2. Redistributions in binary form must reproduce the above copyright notice,
13   *    this list of conditions and the following disclaimer in the documentation
14   *    and/or other materials provided with the distribution.
15   *
16   * 3. The names "ATP", "TreeDL", "RedVerst", "ISP RAS"
17   *    may not be used to endorse or promote products derived from this software
18   *    without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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/*String,Properties*/ propertiesMap = new HashMap/*String,Properties*/();
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 }