1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package com.sun.star.filter.config.tools.split; 25 26 //_______________________________________________ 27 28 import java.lang.*; 29 import java.util.*; 30 import java.io.*; 31 import com.sun.star.filter.config.tools.utils.*; 32 33 //_______________________________________________ 34 35 /** 36 * Can split one xml file into its different xml fragments. 37 * 38 * 39 */ 40 public class Splitter 41 { 42 //___________________________________________ 43 // const 44 45 //___________________________________________ 46 // member 47 48 /** contains all real member of this instance. 49 * That make it easy to initialize an instance 50 * of this class inside a multi-threaded environment. */ 51 private SplitterData m_aDataSet; 52 53 //___________________________________________ 54 // interface 55 56 /** initialize a new instance of this class with all 57 * needed resources. 58 * 59 * @param aDataSet 60 * contains all needed parameters for this instance 61 * as a complete set, which can be filled outside. 62 */ Splitter(SplitterData aDataSet)63 public Splitter(SplitterData aDataSet) 64 { 65 m_aDataSet = aDataSet; 66 } 67 68 //___________________________________________ 69 // interface 70 71 /** generate xml fragments for all cache items. 72 * 73 * @throw [java.lang.Exception] 74 * if anything will fail inside during 75 * this operation runs. 76 */ split()77 public synchronized void split() 78 throws java.lang.Exception 79 { 80 createDirectoryStructures(); 81 82 // use some statistic values to check if all cache items 83 // will be transformed really. 84 int nTypes = m_aDataSet.m_aCache.getItemCount(Cache.E_TYPE ); 85 int nFilters = m_aDataSet.m_aCache.getItemCount(Cache.E_FILTER ); 86 int nDetectServices = m_aDataSet.m_aCache.getItemCount(Cache.E_DETECTSERVICE ); 87 int nFrameLoaders = m_aDataSet.m_aCache.getItemCount(Cache.E_FRAMELOADER ); 88 int nContentHandlers = m_aDataSet.m_aCache.getItemCount(Cache.E_CONTENTHANDLER); 89 90 // generate all type fragments 91 m_aDataSet.m_aDebug.setGlobalInfo("generate type fragments ..."); 92 java.util.Vector lNames = m_aDataSet.m_aCache.getItemNames(Cache.E_TYPE); 93 java.util.Enumeration it = lNames.elements(); 94 while(it.hasMoreElements()) 95 generateXMLFragment(Cache.E_TYPE, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirTypes); 96 nTypes -= lNames.size(); 97 98 // generate filter fragments for the writer module 99 m_aDataSet.m_aDebug.setGlobalInfo("generate filter fragments ..."); 100 m_aDataSet.m_aDebug.setGlobalInfo("\tfor module writer ..."); 101 java.util.HashMap rRequestedProps = new java.util.HashMap(); 102 rRequestedProps.put(Cache.PROPNAME_DOCUMENTSERVICE, "com.sun.star.text.TextDocument"); 103 lNames = m_aDataSet.m_aCache.getMatchedItemNames(Cache.E_FILTER, rRequestedProps); 104 it = lNames.elements(); 105 while(it.hasMoreElements()) 106 generateXMLFragment(Cache.E_FILTER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirModuleSWriter); 107 nFilters -= lNames.size(); 108 109 // generate filter fragments for the writer/web module 110 m_aDataSet.m_aDebug.setGlobalInfo("\tfor module writer/web ..."); 111 rRequestedProps.put(Cache.PROPNAME_DOCUMENTSERVICE, "com.sun.star.text.WebDocument"); 112 lNames = m_aDataSet.m_aCache.getMatchedItemNames(Cache.E_FILTER, rRequestedProps); 113 it = lNames.elements(); 114 while(it.hasMoreElements()) 115 generateXMLFragment(Cache.E_FILTER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirModuleSWeb); 116 nFilters -= lNames.size(); 117 118 // generate filter fragments for the writer/global module 119 m_aDataSet.m_aDebug.setGlobalInfo("\tfor module writer/global ..."); 120 rRequestedProps.put(Cache.PROPNAME_DOCUMENTSERVICE, "com.sun.star.text.GlobalDocument"); 121 lNames = m_aDataSet.m_aCache.getMatchedItemNames(Cache.E_FILTER, rRequestedProps); 122 it = lNames.elements(); 123 while(it.hasMoreElements()) 124 generateXMLFragment(Cache.E_FILTER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirModuleSGlobal); 125 nFilters -= lNames.size(); 126 127 // generate filter fragments for the calc module 128 m_aDataSet.m_aDebug.setGlobalInfo("\tfor module calc ..."); 129 rRequestedProps.put(Cache.PROPNAME_DOCUMENTSERVICE, "com.sun.star.sheet.SpreadsheetDocument"); 130 lNames = m_aDataSet.m_aCache.getMatchedItemNames(Cache.E_FILTER, rRequestedProps); 131 it = lNames.elements(); 132 while(it.hasMoreElements()) 133 generateXMLFragment(Cache.E_FILTER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirModuleSCalc); 134 nFilters -= lNames.size(); 135 136 // generate filter fragments for the draw module 137 m_aDataSet.m_aDebug.setGlobalInfo("\tfor module draw ..."); 138 rRequestedProps.put(Cache.PROPNAME_DOCUMENTSERVICE, "com.sun.star.drawing.DrawingDocument"); 139 lNames = m_aDataSet.m_aCache.getMatchedItemNames(Cache.E_FILTER, rRequestedProps); 140 it = lNames.elements(); 141 while(it.hasMoreElements()) 142 generateXMLFragment(Cache.E_FILTER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirModuleSDraw); 143 nFilters -= lNames.size(); 144 145 // generate filter fragments for the impress module 146 m_aDataSet.m_aDebug.setGlobalInfo("\tfor module impress ..."); 147 rRequestedProps.put(Cache.PROPNAME_DOCUMENTSERVICE, "com.sun.star.presentation.PresentationDocument"); 148 lNames = m_aDataSet.m_aCache.getMatchedItemNames(Cache.E_FILTER, rRequestedProps); 149 it = lNames.elements(); 150 while(it.hasMoreElements()) 151 generateXMLFragment(Cache.E_FILTER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirModuleSImpress); 152 nFilters -= lNames.size(); 153 154 // generate filter fragments for the chart module 155 m_aDataSet.m_aDebug.setGlobalInfo("\tfor module chart ..."); 156 rRequestedProps.put(Cache.PROPNAME_DOCUMENTSERVICE, "com.sun.star.chart2.ChartDocument"); 157 lNames = m_aDataSet.m_aCache.getMatchedItemNames(Cache.E_FILTER, rRequestedProps); 158 it = lNames.elements(); 159 while(it.hasMoreElements()) 160 generateXMLFragment(Cache.E_FILTER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirModuleSChart); 161 nFilters -= lNames.size(); 162 163 // generate filter fragments for the math module 164 m_aDataSet.m_aDebug.setGlobalInfo("\tfor module math ..."); 165 rRequestedProps.put(Cache.PROPNAME_DOCUMENTSERVICE, "com.sun.star.formula.FormulaProperties"); 166 lNames = m_aDataSet.m_aCache.getMatchedItemNames(Cache.E_FILTER, rRequestedProps); 167 it = lNames.elements(); 168 while(it.hasMoreElements()) 169 generateXMLFragment(Cache.E_FILTER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirModuleSMath); 170 nFilters -= lNames.size(); 171 172 // generate fragments for 3rdParty or unspecified (may graphics) filters! 173 m_aDataSet.m_aDebug.setGlobalInfo("\tfor unknown modules ..."); 174 rRequestedProps.put(Cache.PROPNAME_DOCUMENTSERVICE, ""); 175 lNames = m_aDataSet.m_aCache.getMatchedItemNames(Cache.E_FILTER, rRequestedProps); 176 it = lNames.elements(); 177 while(it.hasMoreElements()) 178 generateXMLFragment(Cache.E_FILTER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirModuleOthers); 179 nFilters -= lNames.size(); 180 181 // generate all detect service fragments 182 m_aDataSet.m_aDebug.setGlobalInfo("generate detect service fragments ..."); 183 lNames = m_aDataSet.m_aCache.getItemNames(Cache.E_DETECTSERVICE); 184 it = lNames.elements(); 185 while(it.hasMoreElements()) 186 generateXMLFragment(Cache.E_DETECTSERVICE, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirDetectServices); 187 nDetectServices -= lNames.size(); 188 189 // generate all frame loader fragments 190 m_aDataSet.m_aDebug.setGlobalInfo("generate frame loader fragments ..."); 191 lNames = m_aDataSet.m_aCache.getItemNames(Cache.E_FRAMELOADER); 192 it = lNames.elements(); 193 while(it.hasMoreElements()) 194 generateXMLFragment(Cache.E_FRAMELOADER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirFrameLoaders); 195 nFrameLoaders -= lNames.size(); 196 197 // generate all content handler fragments 198 m_aDataSet.m_aDebug.setGlobalInfo("generate content handler fragments ..."); 199 lNames = m_aDataSet.m_aCache.getItemNames(Cache.E_CONTENTHANDLER); 200 it = lNames.elements(); 201 while(it.hasMoreElements()) 202 generateXMLFragment(Cache.E_CONTENTHANDLER, (java.lang.String)it.nextElement(), m_aDataSet.m_aFragmentDirContentHandlers); 203 nContentHandlers -= lNames.size(); 204 205 // check if all cache items was handled 206 if ( 207 (nTypes != 0) || 208 (nFilters != 0) || 209 (nDetectServices != 0) || 210 (nFrameLoaders != 0) || 211 (nContentHandlers != 0) 212 ) 213 { 214 java.lang.StringBuffer sStatistic = new java.lang.StringBuffer(256); 215 sStatistic.append("some cache items seems to be not transformed:\n"); 216 sStatistic.append(nTypes +" unhandled types\n" ); 217 sStatistic.append(nFilters +" unhandled filters\n" ); 218 sStatistic.append(nDetectServices +" unhandled detect services\n"); 219 sStatistic.append(nFrameLoaders +" unhandled frame loader\n" ); 220 sStatistic.append(nContentHandlers+" unhandled content handler\n"); 221 throw new java.lang.Exception(sStatistic.toString()); 222 } 223 } 224 225 //___________________________________________ 226 227 /** generate a xml fragment file from the specified cache item. 228 * 229 * @param eItemType 230 * specify, which sub container of the cache must be used 231 * to locate the right item. 232 * 233 * @param sItemName 234 * the name of the cache item inside the specified sub container. 235 * 236 * @param aOutDir 237 * output directory. 238 * 239 * @throw [java.lang.Exception] 240 * if the fragment file already exists or could not be created 241 * successfully. 242 */ generateXMLFragment(int eItemType, java.lang.String sItemName, java.io.File aOutDir )243 private void generateXMLFragment(int eItemType, 244 java.lang.String sItemName, 245 java.io.File aOutDir ) 246 throws java.lang.Exception 247 { 248 java.lang.String sFileName = FileHelper.convertName2FileName(sItemName); 249 java.lang.String sXML = m_aDataSet.m_aCache.getItemAsXML(eItemType, sItemName, m_aDataSet.m_nFormat); 250 java.io.File aFile = new java.io.File(aOutDir, sFileName+m_aDataSet.m_sFragmentExtension); 251 252 if (aFile.exists()) 253 throw new java.lang.Exception("fragment["+eItemType+", \""+sItemName+"\"] file named \""+aFile.getPath()+"\" already exists."); 254 255 java.io.FileOutputStream aStream = new java.io.FileOutputStream(aFile); 256 java.io.OutputStreamWriter aWriter = new java.io.OutputStreamWriter(aStream, m_aDataSet.m_sEncoding); 257 aWriter.write(sXML, 0, sXML.length()); 258 aWriter.flush(); 259 aWriter.close(); 260 261 m_aDataSet.m_aDebug.setDetailedInfo("fragment["+eItemType+", \""+sItemName+"\"] => \""+aFile.getPath()+"\" ... OK"); 262 } 263 264 //___________________________________________ 265 266 /** create all needed directory structures. 267 * 268 * First it try to clear old structures and 269 * create new ones afterwards. 270 * 271 * @throw [java.lang.Exception] 272 * if some of the needed structures 273 * could not be created successfully. 274 */ createDirectoryStructures()275 private void createDirectoryStructures() 276 throws java.lang.Exception 277 { 278 m_aDataSet.m_aDebug.setGlobalInfo("create needed directory structures ..."); 279 280 // delete simple files only; no directories! 281 // Because this tool may run inside 282 // a cvs environment its not a godd idea to do so. 283 boolean bFilesOnly = false; 284 FileHelper.makeDirectoryEmpty(m_aDataSet.m_aOutDir, bFilesOnly); 285 286 if ( 287 (!m_aDataSet.m_aFragmentDirTypes.exists() && !m_aDataSet.m_aFragmentDirTypes.mkdir() ) || 288 (!m_aDataSet.m_aFragmentDirFilters.exists() && !m_aDataSet.m_aFragmentDirFilters.mkdir() ) || 289 (!m_aDataSet.m_aFragmentDirDetectServices.exists() && !m_aDataSet.m_aFragmentDirDetectServices.mkdir() ) || 290 (!m_aDataSet.m_aFragmentDirFrameLoaders.exists() && !m_aDataSet.m_aFragmentDirFrameLoaders.mkdir() ) || 291 (!m_aDataSet.m_aFragmentDirContentHandlers.exists() && !m_aDataSet.m_aFragmentDirContentHandlers.mkdir()) || 292 (!m_aDataSet.m_aFragmentDirModuleSWriter.exists() && !m_aDataSet.m_aFragmentDirModuleSWriter.mkdir() ) || 293 (!m_aDataSet.m_aFragmentDirModuleSWeb.exists() && !m_aDataSet.m_aFragmentDirModuleSWeb.mkdir() ) || 294 (!m_aDataSet.m_aFragmentDirModuleSGlobal.exists() && !m_aDataSet.m_aFragmentDirModuleSGlobal.mkdir() ) || 295 (!m_aDataSet.m_aFragmentDirModuleSCalc.exists() && !m_aDataSet.m_aFragmentDirModuleSCalc.mkdir() ) || 296 (!m_aDataSet.m_aFragmentDirModuleSDraw.exists() && !m_aDataSet.m_aFragmentDirModuleSDraw.mkdir() ) || 297 (!m_aDataSet.m_aFragmentDirModuleSImpress.exists() && !m_aDataSet.m_aFragmentDirModuleSImpress.mkdir() ) || 298 (!m_aDataSet.m_aFragmentDirModuleSMath.exists() && !m_aDataSet.m_aFragmentDirModuleSMath.mkdir() ) || 299 (!m_aDataSet.m_aFragmentDirModuleSChart.exists() && !m_aDataSet.m_aFragmentDirModuleSChart.mkdir() ) || 300 (!m_aDataSet.m_aFragmentDirModuleOthers.exists() && !m_aDataSet.m_aFragmentDirModuleOthers.mkdir() ) 301 ) 302 { 303 throw new java.lang.Exception("some directory structures does not exists and could not be created successfully."); 304 } 305 } 306 } 307