summaryrefslogtreecommitdiff
path: root/filter/source/config/tools/split/Splitter.java
blob: d9c171382596a661ea555b9d196d119829a18f7a (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
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * 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.split;

//_______________________________________________

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

//_______________________________________________

/**
 *  Can split one xml file into its different xml fragments.
 *
 *
 */
public class Splitter
{
    //___________________________________________
    // const

    //___________________________________________
    // member

    /** contains all real member of this instance.
     *  That make it easy to initialize an instance
     *  of this class inside a multi-threaded environment. */
    private SplitterData m_aDataSet;

    //___________________________________________
    // interface

    /** initialize a new instance of this class with all
     *  needed resources.
     *
     *  @param  aDataSet
     *          contains all needed parameters for this instance
     *          as a complete set, which can be filled outside.
     */
    public Splitter(SplitterData aDataSet)
    {
        m_aDataSet = aDataSet;
    }

    //___________________________________________
    // interface

    /** generate xml fragments for all cache items.
     *
     *  @throw  [java.lang.Exception]
     *          if anything will fail inside during
     *          this operation runs.
     */
    public synchronized void split()
        throws java.lang.Exception
    {
        createDirectoryStructures();

        // use some statistic values to check if all cache items
        // will be transformed realy.
        int nTypes           = m_aDataSet.m_aCache.getItemCount(Cache.E_TYPE          );
        int nFilters         = m_aDataSet.m_aCache.getItemCount(Cache.E_FILTER        );
        int nDetectServices  = m_aDataSet.m_aCache.getItemCount(Cache.E_DETECTSERVICE );
        int nFrameLoaders    = m_aDataSet.m_aCache.getItemCount(Cache.E_FRAMELOADER   );
        int nContentHandlers = m_aDataSet.m_aCache.getItemCount(Cache.E_CONTENTHANDLER);

        // generate all type fragments
        m_aDataSet.m_aDebug.setGlobalInfo("generate type fragments ...");
        java.util.Vector      lNames = m_aDataSet.m_aCache.getItemNames(Cache.E_TYPE);
        java.util.Enumeration it     = lNames.elements();
        while(it.hasMoreElements())
            generateXMLFragment(Cache.E_TYPE, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirTypes);
        nTypes -= lNames.size();

        // generate filter fragments for the writer module
        m_aDataSet.m_aDebug.setGlobalInfo("generate filter fragments ...");
        m_aDataSet.m_aDebug.setGlobalInfo("\tfor module writer ...");
        java.util.HashMap rRequestedProps = new java.util.HashMap();
        rRequestedProps.put(Cache.PROPNAME_DOCUMENTSERVICE, "com.sun.star.text.TextDocument");
        lNames = m_aDataSet.m_aCache.getMatchedItemNames(Cache.E_FILTER, rRequestedProps);
        it     = lNames.elements();
        while(it.hasMoreElements())
            generateXMLFragment(Cache.E_FILTER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirModuleSWriter);
        nFilters -= lNames.size();

        // generate filter fragments for the writer/web module
        m_aDataSet.m_aDebug.setGlobalInfo("\tfor module writer/web ...");
        rRequestedProps.put(Cache.PROPNAME_DOCUMENTSERVICE, "com.sun.star.text.WebDocument");
        lNames = m_aDataSet.m_aCache.getMatchedItemNames(Cache.E_FILTER, rRequestedProps);
        it     = lNames.elements();
        while(it.hasMoreElements())
            generateXMLFragment(Cache.E_FILTER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirModuleSWeb);
        nFilters -= lNames.size();

        // generate filter fragments for the writer/global module
        m_aDataSet.m_aDebug.setGlobalInfo("\tfor module writer/global ...");
        rRequestedProps.put(Cache.PROPNAME_DOCUMENTSERVICE, "com.sun.star.text.GlobalDocument");
        lNames = m_aDataSet.m_aCache.getMatchedItemNames(Cache.E_FILTER, rRequestedProps);
        it     = lNames.elements();
        while(it.hasMoreElements())
            generateXMLFragment(Cache.E_FILTER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirModuleSGlobal);
        nFilters -= lNames.size();

        // generate filter fragments for the calc module
        m_aDataSet.m_aDebug.setGlobalInfo("\tfor module calc ...");
        rRequestedProps.put(Cache.PROPNAME_DOCUMENTSERVICE, "com.sun.star.sheet.SpreadsheetDocument");
        lNames = m_aDataSet.m_aCache.getMatchedItemNames(Cache.E_FILTER, rRequestedProps);
        it     = lNames.elements();
        while(it.hasMoreElements())
            generateXMLFragment(Cache.E_FILTER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirModuleSCalc);
        nFilters -= lNames.size();

        // generate filter fragments for the draw module
        m_aDataSet.m_aDebug.setGlobalInfo("\tfor module draw ...");
        rRequestedProps.put(Cache.PROPNAME_DOCUMENTSERVICE, "com.sun.star.drawing.DrawingDocument");
        lNames = m_aDataSet.m_aCache.getMatchedItemNames(Cache.E_FILTER, rRequestedProps);
        it     = lNames.elements();
        while(it.hasMoreElements())
            generateXMLFragment(Cache.E_FILTER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirModuleSDraw);
        nFilters -= lNames.size();

        // generate filter fragments for the impress module
        m_aDataSet.m_aDebug.setGlobalInfo("\tfor module impress ...");
        rRequestedProps.put(Cache.PROPNAME_DOCUMENTSERVICE, "com.sun.star.presentation.PresentationDocument");
        lNames = m_aDataSet.m_aCache.getMatchedItemNames(Cache.E_FILTER, rRequestedProps);
        it     = lNames.elements();
        while(it.hasMoreElements())
            generateXMLFragment(Cache.E_FILTER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirModuleSImpress);
        nFilters -= lNames.size();

        // generate filter fragments for the chart module
        m_aDataSet.m_aDebug.setGlobalInfo("\tfor module chart ...");
        rRequestedProps.put(Cache.PROPNAME_DOCUMENTSERVICE, "com.sun.star.chart2.ChartDocument");
        lNames = m_aDataSet.m_aCache.getMatchedItemNames(Cache.E_FILTER, rRequestedProps);
        it     = lNames.elements();
        while(it.hasMoreElements())
            generateXMLFragment(Cache.E_FILTER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirModuleSChart);
        nFilters -= lNames.size();

        // generate filter fragments for the math module
        m_aDataSet.m_aDebug.setGlobalInfo("\tfor module math ...");
        rRequestedProps.put(Cache.PROPNAME_DOCUMENTSERVICE, "com.sun.star.formula.FormulaProperties");
        lNames = m_aDataSet.m_aCache.getMatchedItemNames(Cache.E_FILTER, rRequestedProps);
        it     = lNames.elements();
        while(it.hasMoreElements())
            generateXMLFragment(Cache.E_FILTER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirModuleSMath);
        nFilters -= lNames.size();

        // generate fragments for 3rdParty or unspecified (may graphics) filters!
        m_aDataSet.m_aDebug.setGlobalInfo("\tfor unknown modules ...");
        rRequestedProps.put(Cache.PROPNAME_DOCUMENTSERVICE, "");
        lNames = m_aDataSet.m_aCache.getMatchedItemNames(Cache.E_FILTER, rRequestedProps);
        it     = lNames.elements();
        while(it.hasMoreElements())
            generateXMLFragment(Cache.E_FILTER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirModuleOthers);
        nFilters -= lNames.size();

        // generate all detect service fragments
        m_aDataSet.m_aDebug.setGlobalInfo("generate detect service fragments ...");
        lNames = m_aDataSet.m_aCache.getItemNames(Cache.E_DETECTSERVICE);
        it     = lNames.elements();
        while(it.hasMoreElements())
            generateXMLFragment(Cache.E_DETECTSERVICE, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirDetectServices);
        nDetectServices -= lNames.size();

        // generate all frame loader fragments
        m_aDataSet.m_aDebug.setGlobalInfo("generate frame loader fragments ...");
        lNames = m_aDataSet.m_aCache.getItemNames(Cache.E_FRAMELOADER);
        it     = lNames.elements();
        while(it.hasMoreElements())
            generateXMLFragment(Cache.E_FRAMELOADER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirFrameLoaders);
        nFrameLoaders -= lNames.size();

        // generate all content handler fragments
        m_aDataSet.m_aDebug.setGlobalInfo("generate content handler fragments ...");
        lNames = m_aDataSet.m_aCache.getItemNames(Cache.E_CONTENTHANDLER);
        it     = lNames.elements();
        while(it.hasMoreElements())
            generateXMLFragment(Cache.E_CONTENTHANDLER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirContentHandlers);
        nContentHandlers -= lNames.size();

        // check if all cache items was handled
        if (
            (nTypes           != 0) ||
            (nFilters         != 0) ||
            (nDetectServices  != 0) ||
            (nFrameLoaders    != 0) ||
            (nContentHandlers != 0)
           )
        {
            java.lang.StringBuffer sStatistic = new java.lang.StringBuffer(256);
            sStatistic.append("some cache items seems to be not transformed:\n");
            sStatistic.append(nTypes          +" unhandled types\n"          );
            sStatistic.append(nFilters        +" unhandled filters\n"        );
            sStatistic.append(nDetectServices +" unhandled detect services\n");
            sStatistic.append(nFrameLoaders   +" unhandled frame loader\n"   );
            sStatistic.append(nContentHandlers+" unhandled content handler\n");
            throw new java.lang.Exception(sStatistic.toString());
        }
    }

    //___________________________________________

    /** generate a xml fragment file from the specified cache item.
     *
     *  @param  eItemType
     *          specify, which sub container of the cache must be used
     *          to locate the right item.
     *
     *  @param  sItemName
     *          the name of the cache item inside the specified sub container.
     *
     *  @param  aOutDir
     *          output directory.
     *
     *  @throw  [java.lang.Exception]
     *          if the fragment file already exists or could not be created
     *          successfully.
     */
    private void generateXMLFragment(int              eItemType,
                                     java.lang.String sItemName,
                                     java.io.File     aOutDir  )
        throws java.lang.Exception
    {
        java.lang.String sFileName = FileHelper.convertName2FileName(sItemName);
        java.lang.String sXML      = m_aDataSet.m_aCache.getItemAsXML(eItemType, sItemName, m_aDataSet.m_nFormat);
        java.io.File     aFile     = new java.io.File(aOutDir, sFileName+m_aDataSet.m_sFragmentExtension);

        if (aFile.exists())
            throw new java.lang.Exception("fragment["+eItemType+", \""+sItemName+"\"] file named \""+aFile.getPath()+"\" already exists.");

        java.io.FileOutputStream   aStream = new java.io.FileOutputStream(aFile);
        java.io.OutputStreamWriter aWriter = new java.io.OutputStreamWriter(aStream, m_aDataSet.m_sEncoding);
        aWriter.write(sXML, 0, sXML.length());
        aWriter.flush();
        aWriter.close();

        m_aDataSet.m_aDebug.setDetailedInfo("fragment["+eItemType+", \""+sItemName+"\"] => \""+aFile.getPath()+"\" ... OK");
    }

    //___________________________________________

    /** create all needed directory structures.
     *
     *  First it try to clear old structures and
     *  create new ones afterwards.
     *
     *  @throw  [java.lang.Exception]
     *          if some of the needed structures
     *          could not be created successfully.
     */
    private void createDirectoryStructures()
        throws java.lang.Exception
    {
        m_aDataSet.m_aDebug.setGlobalInfo("create needed directory structures ...");

        // delete simple files only; no directories!
        // Because this tool may run inside
        // a cvs environment its not a godd idea to do so.
        boolean bFilesOnly = false;
        FileHelper.makeDirectoryEmpty(m_aDataSet.m_aOutDir, bFilesOnly);

        if (
            (!m_aDataSet.m_aFragmentDirTypes.exists()           && !m_aDataSet.m_aFragmentDirTypes.mkdir()          ) ||
            (!m_aDataSet.m_aFragmentDirFilters.exists()         && !m_aDataSet.m_aFragmentDirFilters.mkdir()        ) ||
            (!m_aDataSet.m_aFragmentDirDetectServices.exists()  && !m_aDataSet.m_aFragmentDirDetectServices.mkdir() ) ||
            (!m_aDataSet.m_aFragmentDirFrameLoaders.exists()    && !m_aDataSet.m_aFragmentDirFrameLoaders.mkdir()   ) ||
            (!m_aDataSet.m_aFragmentDirContentHandlers.exists() && !m_aDataSet.m_aFragmentDirContentHandlers.mkdir()) ||
            (!m_aDataSet.m_aFragmentDirModuleSWriter.exists()   && !m_aDataSet.m_aFragmentDirModuleSWriter.mkdir()  ) ||
            (!m_aDataSet.m_aFragmentDirModuleSWeb.exists()      && !m_aDataSet.m_aFragmentDirModuleSWeb.mkdir()     ) ||
            (!m_aDataSet.m_aFragmentDirModuleSGlobal.exists()   && !m_aDataSet.m_aFragmentDirModuleSGlobal.mkdir()  ) ||
            (!m_aDataSet.m_aFragmentDirModuleSCalc.exists()     && !m_aDataSet.m_aFragmentDirModuleSCalc.mkdir()    ) ||
            (!m_aDataSet.m_aFragmentDirModuleSDraw.exists()     && !m_aDataSet.m_aFragmentDirModuleSDraw.mkdir()    ) ||
            (!m_aDataSet.m_aFragmentDirModuleSImpress.exists()  && !m_aDataSet.m_aFragmentDirModuleSImpress.mkdir() ) ||
            (!m_aDataSet.m_aFragmentDirModuleSMath.exists()     && !m_aDataSet.m_aFragmentDirModuleSMath.mkdir()    ) ||
            (!m_aDataSet.m_aFragmentDirModuleSChart.exists()    && !m_aDataSet.m_aFragmentDirModuleSChart.mkdir()   ) ||
            (!m_aDataSet.m_aFragmentDirModuleOthers.exists()    && !m_aDataSet.m_aFragmentDirModuleOthers.mkdir()   )
           )
        {
            throw new java.lang.Exception("some directory structures does not exists and could not be created successfully.");
        }
    }
}