merge the formfield patch from ooo-build
[ooovba.git] / transex3 / source / filter / merge / Merger.java
blobf124a264c57269d9ea61f3f2053643dc378d17c0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Merger.java,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package com.sun.star.filter.config.tools.merge;
32 //_______________________________________________
34 import java.lang.*;
35 import java.util.*;
36 import java.io.*;
37 import com.sun.star.filter.config.tools.utils.*;
39 //_______________________________________________
41 /** can merge different xml fragments together.
45 public class Merger
47 //___________________________________________
48 // const
50 private static final java.lang.String PROP_XMLVERSION = "xmlversion" ; // <= global cfg file
51 private static final java.lang.String PROP_XMLENCODING = "xmlencoding" ; // <= global cfg file
52 private static final java.lang.String PROP_XMLPATH = "xmlpath" ; // <= global cfg file
53 private static final java.lang.String PROP_XMLPACKAGE = "xmlpackage" ; // <= global cfg file
55 private static final java.lang.String PROP_SETNAME_TYPES = "setname_types" ; // <= global cfg file
56 private static final java.lang.String PROP_SETNAME_FILTERS = "setname_filters" ; // <= global cfg file
57 private static final java.lang.String PROP_SETNAME_LOADERS = "setname_frameloaders" ; // <= global cfg file
58 private static final java.lang.String PROP_SETNAME_HANDLERS = "setname_contenthandlers" ; // <= global cfg file
60 private static final java.lang.String PROP_SUBDIR_TYPES = "subdir_types" ; // <= global cfg file
61 private static final java.lang.String PROP_SUBDIR_FILTERS = "subdir_filters" ; // <= global cfg file
62 private static final java.lang.String PROP_SUBDIR_LOADERS = "subdir_frameloaders" ; // <= global cfg file
63 private static final java.lang.String PROP_SUBDIR_HANDLERS = "subdir_contenthandlers" ; // <= global cfg file
65 private static final java.lang.String PROP_EXTENSION_XCU = "extension_xcu" ; // <= global cfg file
66 private static final java.lang.String PROP_EXTENSION_PKG = "extension_pkg" ; // <= global cfg file
68 private static final java.lang.String PROP_DELIMITER = "delimiter" ; // <= global cfg file
69 private static final java.lang.String PROP_TRIM = "trim" ; // <= global cfg file
70 private static final java.lang.String PROP_DECODE = "decode" ; // <= global cfg file
72 private static final java.lang.String PROP_FRAGMENTSDIR = "fragmentsdir" ; // <= cmdline
73 private static final java.lang.String PROP_TEMPDIR = "tempdir" ; // <= cmdline
74 private static final java.lang.String PROP_OUTDIR = "outdir" ; // <= cmdline
75 private static final java.lang.String PROP_PKG = "pkg" ; // <= cmdline
76 private static final java.lang.String PROP_DEBUG = "debug" ; // <= cmdline
78 private static final java.lang.String PROP_TCFG = "tcfg" ; // <= cmdline
79 private static final java.lang.String PROP_FCFG = "fcfg" ; // <= cmdline
80 private static final java.lang.String PROP_LCFG = "lcfg" ; // <= cmdline
81 private static final java.lang.String PROP_CCFG = "ccfg" ; // <= cmdline
82 private static final java.lang.String PROP_LANGUAGEPACK = "languagepack" ; // <= cmdline
84 private static final java.lang.String PROP_ITEMS = "items" ; // <= pkg cfg files!
86 //___________________________________________
87 // member
89 //-------------------------------------------
90 /** TODO */
91 private ConfigHelper m_aCfg;
93 //-------------------------------------------
94 /** TODO */
95 private Logger m_aLog;
97 //-------------------------------------------
98 /** TODO */
99 private java.io.File m_aFragmentsDir;
101 //-------------------------------------------
102 /** TODO */
103 private java.io.File m_aTempDir;
105 //-------------------------------------------
106 /** TODO */
107 private java.io.File m_aOutDir;
109 //-------------------------------------------
110 /** TODO */
111 private java.util.Vector m_lTypes;
112 private java.util.Vector m_lFilters;
113 private java.util.Vector m_lLoaders;
114 private java.util.Vector m_lHandlers;
116 //___________________________________________
117 // interface
119 //-------------------------------------------
120 /** initialize a new instance of this class and
121 * try to get all needed resources from the config module.
123 * @param aCfg
124 * provides access to all values of the global
125 * config file and to the command line.
127 * @param aLog
128 * can be used to print out log informations.
130 public Merger(ConfigHelper aCfg,
131 Logger aLog)
132 throws java.lang.Exception
134 m_aCfg = aCfg;
135 m_aLog = aLog;
137 m_aFragmentsDir = new java.io.File(m_aCfg.getString(PROP_FRAGMENTSDIR));
138 m_aTempDir = new java.io.File(m_aCfg.getString(PROP_TEMPDIR ));
139 // m_aOutDir = new java.io.File(m_aCfg.getString(PROP_OUTDIR ));
141 java.lang.String sDelimiter = m_aCfg.getString(PROP_DELIMITER);
142 boolean bTrim = m_aCfg.getBoolean(PROP_TRIM);
143 boolean bDecode = m_aCfg.getBoolean(PROP_DECODE);
147 ConfigHelper aFcfg = new ConfigHelper(m_aCfg.getString(PROP_TCFG), null);
148 m_lTypes = aFcfg.getStringList(PROP_ITEMS, sDelimiter, bTrim, bDecode);
150 catch(java.util.NoSuchElementException ex1)
152 m_lTypes = new java.util.Vector();
153 //m_aLog.setWarning("Fragment list of types is missing. Parameter \"items\" seems to be invalid.");
158 ConfigHelper aFcfg = new ConfigHelper(m_aCfg.getString(PROP_FCFG), null);
159 m_lFilters = aFcfg.getStringList(PROP_ITEMS, sDelimiter, bTrim, bDecode);
161 catch(java.util.NoSuchElementException ex1)
163 m_lFilters = new java.util.Vector();
164 //m_aLog.setWarning("Fragment list of filters is missing. Parameter \"items\" seems to be invalid.");
169 ConfigHelper aFcfg = new ConfigHelper(m_aCfg.getString(PROP_LCFG), null);
170 m_lLoaders = aFcfg.getStringList(PROP_ITEMS, sDelimiter, bTrim, bDecode);
172 catch(java.util.NoSuchElementException ex1)
174 m_lLoaders = new java.util.Vector();
175 //m_aLog.setWarning("Fragment list of frame loader objects is missing. Parameter \"items\" seems to be invalid.");
180 ConfigHelper aFcfg = new ConfigHelper(m_aCfg.getString(PROP_CCFG), null);
181 m_lHandlers = aFcfg.getStringList(PROP_ITEMS, sDelimiter, bTrim, bDecode);
183 catch(java.util.NoSuchElementException ex1)
185 m_lHandlers = new java.util.Vector();
186 //m_aLog.setWarning("Fragment list of content handler objects is missing. Parameter \"items\" seems to be invalid.");
190 //-------------------------------------------
191 /** TODO */
192 public synchronized void merge()
193 throws java.lang.Exception
195 java.lang.StringBuffer sBuffer = new java.lang.StringBuffer(1000000);
196 java.lang.String sPackage = m_aCfg.getString(PROP_PKG);
198 m_aLog.setGlobalInfo("create package \""+sPackage+"\" ...");
199 m_aLog.setDetailedInfo("generate package header ... ");
201 sBuffer.append(
202 XMLHelper.generateHeader(
203 m_aCfg.getString (PROP_XMLVERSION ),
204 m_aCfg.getString (PROP_XMLENCODING ),
205 m_aCfg.getString (PROP_XMLPATH ),
206 m_aCfg.getString (PROP_XMLPACKAGE ),
207 m_aCfg.getBoolean(PROP_LANGUAGEPACK, false)));
209 // counts all transfered fragments
210 // Can be used later to decide, if a generated package file
211 // contains "nothing"!
212 int nItemCount = 0;
214 for (int i=0; i<4; ++i)
216 java.lang.String sSetName = null;
217 java.lang.String sSubDir = null;
218 java.util.Vector lFragments = null;
222 switch(i)
224 case 0: // types
226 m_aLog.setDetailedInfo("generate set for types ... ");
227 sSetName = m_aCfg.getString(PROP_SETNAME_TYPES);
228 sSubDir = m_aCfg.getString(PROP_SUBDIR_TYPES );
229 lFragments = m_lTypes;
231 break;
233 case 1: // filters
235 m_aLog.setDetailedInfo("generate set for filter ... ");
236 sSetName = m_aCfg.getString(PROP_SETNAME_FILTERS);
237 sSubDir = m_aCfg.getString(PROP_SUBDIR_FILTERS );
238 lFragments = m_lFilters;
240 break;
242 case 2: // loaders
244 m_aLog.setDetailedInfo("generate set for frame loader ... ");
245 sSetName = m_aCfg.getString(PROP_SETNAME_LOADERS);
246 sSubDir = m_aCfg.getString(PROP_SUBDIR_LOADERS );
247 lFragments = m_lLoaders;
249 break;
251 case 3: // handlers
253 m_aLog.setDetailedInfo("generate set for content handler ... ");
254 sSetName = m_aCfg.getString(PROP_SETNAME_HANDLERS);
255 sSubDir = m_aCfg.getString(PROP_SUBDIR_HANDLERS );
256 lFragments = m_lHandlers;
258 break;
261 nItemCount += lFragments.size();
263 getFragments(
264 new java.io.File(m_aFragmentsDir, sSubDir),
265 sSetName,
266 lFragments,
268 sBuffer);
270 catch(java.util.NoSuchElementException exIgnore)
271 { continue; }
274 m_aLog.setDetailedInfo("generate package footer ... ");
275 sBuffer.append(XMLHelper.generateFooter());
277 // Attention!
278 // If the package seem to be empty, it make no sense to generate a corresponding
279 // xml file. We should suppress writing of this file on disk completly ...
280 if (nItemCount < 1)
282 m_aLog.setWarning("Package is empty and will not result into a xml file on disk!? Please check configuration file.");
283 return;
285 m_aLog.setGlobalInfo("package contains "+nItemCount+" items");
287 java.io.File aPackage = new File(sPackage);
288 m_aLog.setGlobalInfo("write temp package \""+aPackage.getPath()); // TODO encoding must be readed from the configuration
289 FileHelper.writeEncodedBufferToFile(aPackage, "UTF-8", false, sBuffer); // check for success is done inside this method!
292 //-------------------------------------------
293 /** TODO */
294 private void getFragments(java.io.File aDir ,
295 java.lang.String sSetName ,
296 java.util.Vector lFragments ,
297 int nPrettyTabs,
298 java.lang.StringBuffer sBuffer )
299 throws java.lang.Exception
301 if (lFragments.size()<1)
303 m_aLog.setWarning("List of fragments is empty!? Will be ignored ...");
304 return;
307 java.util.Enumeration pFragments = lFragments.elements();
308 java.lang.String sExtXcu = m_aCfg.getString(PROP_EXTENSION_XCU);
310 for (int tabs=0; tabs<nPrettyTabs; ++tabs)
311 sBuffer.append("\t");
312 sBuffer.append("<node oor:name=\""+sSetName+"\">\n");
313 ++nPrettyTabs;
315 // special mode for generating language packs.
316 // In such case we must live with some missing fragment files.
317 // Reason behind; Not all filters are realy localized.
318 // But we dont use a different fragment list. We try to locate
319 // any fragment file in its language-pack version ...
320 boolean bHandleLanguagePacks = m_aCfg.getBoolean(PROP_LANGUAGEPACK, false);
321 boolean bDebug = m_aCfg.getBoolean(PROP_DEBUG , false);
322 java.lang.String sEncoding = "UTF-8";
323 if (bDebug)
324 sEncoding = "UTF-8Special";
326 while(pFragments.hasMoreElements())
328 java.lang.String sFragment = (java.lang.String)pFragments.nextElement();
329 java.io.File aFragment = new java.io.File(aDir, sFragment+"."+sExtXcu);
331 // handle simple files only and check for existence!
332 if (!aFragment.exists())
334 if (bHandleLanguagePacks)
336 m_aLog.setWarning("language fragment \""+aFragment.getPath()+"\" does not exist. Will be ignored.");
337 continue;
339 else
340 throw new java.io.IOException("fragment \""+aFragment.getPath()+"\" does not exists.");
343 if (!aFragment.isFile())
345 m_aLog.setWarning("fragment \""+aFragment.getPath()+"\" seem to be not a valid file.");
346 continue;
349 // copy file content of original fragment
350 // Note: A FileNotFoundException will be thrown automaticly by the
351 // used reader objects. Let it break this method too. Our calli is interested
352 // on such errors :-)
353 m_aLog.setDetailedInfo("merge fragment \""+aFragment.getPath()+"\" ...");
354 FileHelper.readEncodedBufferFromFile(aFragment, sEncoding, sBuffer);
356 sBuffer.append("\n");
359 --nPrettyTabs;
360 for (int tabs=0; tabs<nPrettyTabs; ++tabs)
361 sBuffer.append("\t");
362 sBuffer.append("</node>\n");