summaryrefslogtreecommitdiff
path: root/transex3/source/filter/merge/Merger.java
blob: 0bf11fc42fbf127cde5e968c44ae5da6886fcd91 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: Merger.java,v $
 * $Revision: 1.8 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/
package com.sun.star.filter.config.tools.merge;

//_______________________________________________

import java.lang.*;
import java.util.*;
import java.io.*;
import com.sun.star.filter.config.tools.utils.*;

//_______________________________________________

/** can merge different xml fragments together.
 *
 *
 */
public class Merger
{
    //___________________________________________
    // const

    private static final java.lang.String PROP_XMLVERSION      = "xmlversion"               ; // <= global cfg file
    private static final java.lang.String PROP_XMLENCODING     = "xmlencoding"              ; // <= global cfg file
    private static final java.lang.String PROP_XMLPATH         = "xmlpath"                  ; // <= global cfg file
    private static final java.lang.String PROP_XMLPACKAGE      = "xmlpackage"               ; // <= global cfg file

    private static final java.lang.String PROP_SETNAME_TYPES    = "setname_types"           ; // <= global cfg file
    private static final java.lang.String PROP_SETNAME_FILTERS  = "setname_filters"         ; // <= global cfg file
    private static final java.lang.String PROP_SETNAME_LOADERS  = "setname_frameloaders"    ; // <= global cfg file
    private static final java.lang.String PROP_SETNAME_HANDLERS = "setname_contenthandlers" ; // <= global cfg file

    private static final java.lang.String PROP_SUBDIR_TYPES    = "subdir_types"             ; // <= global cfg file
    private static final java.lang.String PROP_SUBDIR_FILTERS  = "subdir_filters"           ; // <= global cfg file
    private static final java.lang.String PROP_SUBDIR_LOADERS  = "subdir_frameloaders"      ; // <= global cfg file
    private static final java.lang.String PROP_SUBDIR_HANDLERS = "subdir_contenthandlers"   ; // <= global cfg file

    private static final java.lang.String PROP_EXTENSION_XCU   = "extension_xcu"            ; // <= global cfg file
    private static final java.lang.String PROP_EXTENSION_PKG   = "extension_pkg"            ; // <= global cfg file

    private static final java.lang.String PROP_DELIMITER       = "delimiter"                ; // <= global cfg file
    private static final java.lang.String PROP_TRIM            = "trim"                     ; // <= global cfg file
    private static final java.lang.String PROP_DECODE          = "decode"                   ; // <= global cfg file

    private static final java.lang.String PROP_FRAGMENTSDIR    = "fragmentsdir"             ; // <= cmdline
    private static final java.lang.String PROP_TEMPDIR         = "tempdir"                  ; // <= cmdline
    private static final java.lang.String PROP_OUTDIR          = "outdir"                   ; // <= cmdline
    private static final java.lang.String PROP_PKG             = "pkg"                      ; // <= cmdline
    private static final java.lang.String PROP_DEBUG           = "debug"                    ; // <= cmdline

    private static final java.lang.String PROP_TCFG            = "tcfg"                     ; // <= cmdline
    private static final java.lang.String PROP_FCFG            = "fcfg"                     ; // <= cmdline
    private static final java.lang.String PROP_LCFG            = "lcfg"                     ; // <= cmdline
    private static final java.lang.String PROP_CCFG            = "ccfg"                     ; // <= cmdline
    private static final java.lang.String PROP_LANGUAGEPACK    = "languagepack"             ; // <= cmdline

    private static final java.lang.String PROP_ITEMS           = "items"                    ; // <= pkg cfg files!

    //___________________________________________
    // member

    //-------------------------------------------
    /** TODO */
    private ConfigHelper m_aCfg;

    //-------------------------------------------
    /** TODO */
    private Logger m_aLog;

    //-------------------------------------------
    /** TODO */
    private java.io.File m_aFragmentsDir;

    //-------------------------------------------
    /** TODO */
    private java.io.File m_aTempDir;

    //-------------------------------------------
    /** TODO */
    private java.io.File m_aOutDir;

    //-------------------------------------------
    /** TODO */
    private java.util.Vector m_lTypes;
    private java.util.Vector m_lFilters;
    private java.util.Vector m_lLoaders;
    private java.util.Vector m_lHandlers;

    //___________________________________________
    // interface

    //-------------------------------------------
    /** initialize a new instance of this class and
     *  try to get all needed resources from the config module.
     *
     *  @param  aCfg
     *          provides access to all values of the global
     *          config file and to the command line.
     *
     *  @param  aLog
     *          can be used to print out log informations.
     */
    public Merger(ConfigHelper aCfg,
                  Logger       aLog)
        throws java.lang.Exception
    {
        m_aCfg = aCfg;
        m_aLog = aLog;

        m_aFragmentsDir = new java.io.File(m_aCfg.getString(PROP_FRAGMENTSDIR));
        m_aTempDir      = new java.io.File(m_aCfg.getString(PROP_TEMPDIR     ));
//        m_aOutDir       = new java.io.File(m_aCfg.getString(PROP_OUTDIR      ));

        java.lang.String sDelimiter = m_aCfg.getString(PROP_DELIMITER);
        boolean          bTrim      = m_aCfg.getBoolean(PROP_TRIM);
        boolean          bDecode    = m_aCfg.getBoolean(PROP_DECODE);

        try
        {
            ConfigHelper aFcfg = new ConfigHelper(m_aCfg.getString(PROP_TCFG), null);
            m_lTypes = aFcfg.getStringList(PROP_ITEMS, sDelimiter, bTrim, bDecode);
        }
        catch(java.util.NoSuchElementException ex1)
        {
            m_lTypes = new java.util.Vector();
            //m_aLog.setWarning("Fragment list of types is missing. Parameter \"items\" seems to be invalid.");
        }

        try
        {
            ConfigHelper aFcfg = new ConfigHelper(m_aCfg.getString(PROP_FCFG), null);
            m_lFilters = aFcfg.getStringList(PROP_ITEMS, sDelimiter, bTrim, bDecode);
        }
        catch(java.util.NoSuchElementException ex1)
        {
            m_lFilters = new java.util.Vector();
            //m_aLog.setWarning("Fragment list of filters is missing. Parameter \"items\" seems to be invalid.");
        }

        try
        {
            ConfigHelper aFcfg = new ConfigHelper(m_aCfg.getString(PROP_LCFG), null);
            m_lLoaders = aFcfg.getStringList(PROP_ITEMS, sDelimiter, bTrim, bDecode);
        }
        catch(java.util.NoSuchElementException ex1)
        {
            m_lLoaders = new java.util.Vector();
            //m_aLog.setWarning("Fragment list of frame loader objects is missing. Parameter \"items\" seems to be invalid.");
        }

        try
        {
            ConfigHelper aFcfg = new ConfigHelper(m_aCfg.getString(PROP_CCFG), null);
            m_lHandlers = aFcfg.getStringList(PROP_ITEMS, sDelimiter, bTrim, bDecode);
        }
        catch(java.util.NoSuchElementException ex1)
        {
            m_lHandlers = new java.util.Vector();
            //m_aLog.setWarning("Fragment list of content handler objects is missing. Parameter \"items\" seems to be invalid.");
        }
    }

    //-------------------------------------------
    /** TODO */
    public synchronized void merge()
        throws java.lang.Exception
    {
        java.lang.StringBuffer sBuffer  = new java.lang.StringBuffer(1000000);
        java.lang.String       sPackage = m_aCfg.getString(PROP_PKG);

        m_aLog.setGlobalInfo("create package \""+sPackage+"\" ...");
        m_aLog.setDetailedInfo("generate package header ... ");

        sBuffer.append(
            XMLHelper.generateHeader(
                m_aCfg.getString (PROP_XMLVERSION         ),
                m_aCfg.getString (PROP_XMLENCODING        ),
                m_aCfg.getString (PROP_XMLPATH            ),
                m_aCfg.getString (PROP_XMLPACKAGE         ),
                m_aCfg.getBoolean(PROP_LANGUAGEPACK, false)));

        // counts all transfered fragments
        // Can be used later to decide, if a generated package file
        // contains "nothing"!
        int nItemCount = 0;

        for (int i=0; i<4; ++i)
        {
            java.lang.String sSetName   = null;
            java.lang.String sSubDir    = null;
            java.util.Vector lFragments = null;

            try
            {
                switch(i)
                {
                    case 0: // types
                    {
                        m_aLog.setDetailedInfo("generate set for types ... ");
                        sSetName   = m_aCfg.getString(PROP_SETNAME_TYPES);
                        sSubDir    = m_aCfg.getString(PROP_SUBDIR_TYPES );
                        lFragments = m_lTypes;
                    }
                    break;

                    case 1: // filters
                    {
                        m_aLog.setDetailedInfo("generate set for filter ... ");
                        sSetName   = m_aCfg.getString(PROP_SETNAME_FILTERS);
                        sSubDir    = m_aCfg.getString(PROP_SUBDIR_FILTERS );
                        lFragments = m_lFilters;
                    }
                    break;

                    case 2: // loaders
                    {
                        m_aLog.setDetailedInfo("generate set for frame loader ... ");
                        sSetName   = m_aCfg.getString(PROP_SETNAME_LOADERS);
                        sSubDir    = m_aCfg.getString(PROP_SUBDIR_LOADERS );
                        lFragments = m_lLoaders;
                    }
                    break;

                    case 3: // handlers
                    {
                        m_aLog.setDetailedInfo("generate set for content handler ... ");
                        sSetName   = m_aCfg.getString(PROP_SETNAME_HANDLERS);
                        sSubDir    = m_aCfg.getString(PROP_SUBDIR_HANDLERS );
                        lFragments = m_lHandlers;
                    }
                    break;
                }

                nItemCount += lFragments.size();

                getFragments(
                    new java.io.File(m_aFragmentsDir, sSubDir),
                    sSetName,
                    lFragments,
                    1,
                    sBuffer);
            }
            catch(java.util.NoSuchElementException exIgnore)
                { continue; }
        }

        m_aLog.setDetailedInfo("generate package footer ... ");
        sBuffer.append(XMLHelper.generateFooter());

        // Attention!
        // If the package seem to be empty, it make no sense to generate a corresponding
        // xml file. We should suppress writing of this file on disk completly ...
        if (nItemCount < 1)
        {
            m_aLog.setWarning("Package is empty and will not result into a xml file on disk!? Please check configuration file.");
            return;
        }
        m_aLog.setGlobalInfo("package contains "+nItemCount+" items");

        java.io.File aPackage = new File(sPackage);
        m_aLog.setGlobalInfo("write temp package \""+aPackage.getPath()); // TODO encoding must be readed from the configuration
        FileHelper.writeEncodedBufferToFile(aPackage, "UTF-8", false, sBuffer); // check for success is done inside this method!
    }

    //-------------------------------------------
    /** TODO */
    private void getFragments(java.io.File           aDir       ,
                              java.lang.String       sSetName   ,
                              java.util.Vector       lFragments ,
                              int                    nPrettyTabs,
                              java.lang.StringBuffer sBuffer    )
        throws java.lang.Exception
    {
        if (lFragments.size()<1)
        {
            m_aLog.setWarning("List of fragments is empty!? Will be ignored ...");
            return;
        }

        java.util.Enumeration  pFragments = lFragments.elements();
        java.lang.String       sExtXcu    = m_aCfg.getString(PROP_EXTENSION_XCU);

        for (int tabs=0; tabs<nPrettyTabs; ++tabs)
            sBuffer.append("\t");
        sBuffer.append("<node oor:name=\""+sSetName+"\">\n");
        ++nPrettyTabs;

        // special mode for generating language packs.
        // In such case we must live with some missing fragment files.
        // Reason behind; Not all filters are realy localized.
        // But we dont use a different fragment list. We try to locate
        // any fragment file in its language-pack version ...
        boolean bHandleLanguagePacks = m_aCfg.getBoolean(PROP_LANGUAGEPACK, false);
        boolean bDebug               = m_aCfg.getBoolean(PROP_DEBUG       , false);
        java.lang.String sEncoding   = "UTF-8";
        if (bDebug)
            sEncoding = "UTF-8Special";

        while(pFragments.hasMoreElements())
        {
            java.lang.String sFragment = (java.lang.String)pFragments.nextElement();
            java.io.File     aFragment = new java.io.File(aDir, sFragment+"."+sExtXcu);

            // handle simple files only and check for existence!
            if (!aFragment.exists())
            {
                if (bHandleLanguagePacks)
                {
                    m_aLog.setWarning("language fragment \""+aFragment.getPath()+"\" does not exist. Will be ignored.");
                    continue;
                }
                else
                    throw new java.io.IOException("fragment \""+aFragment.getPath()+"\" does not exists.");
            }

            if (!aFragment.isFile())
            {
                m_aLog.setWarning("fragment \""+aFragment.getPath()+"\" seem to be not a valid file.");
                continue;
            }

            // copy file content of original fragment
            // Note: A FileNotFoundException will be thrown automaticly by the
            // used reader objects. Let it break this method too. Our calli is interested
            // on such errors :-)
            m_aLog.setDetailedInfo("merge fragment \""+aFragment.getPath()+"\" ...");
            FileHelper.readEncodedBufferFromFile(aFragment, sEncoding, sBuffer);

            sBuffer.append("\n");
        }

        --nPrettyTabs;
        for (int tabs=0; tabs<nPrettyTabs; ++tabs)
            sBuffer.append("\t");
        sBuffer.append("</node>\n");
    }
}