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 OfficeDev.samples.Filter; 25 26 import com.sun.star.uno.AnyConverter; 27 import com.sun.star.beans.PropertyValue; 28 import com.sun.star.io.XInputStream; 29 import com.sun.star.io.XOutputStream; 30 import com.sun.star.ucb.XSimpleFileAccess; 31 import com.sun.star.uno.XComponentContext; 32 import com.sun.star.lang.XMultiComponentFactory; 33 import com.sun.star.uno.UnoRuntime; 34 35 import com.sun.star.uno.Exception; 36 import com.sun.star.ucb.CommandAbortedException; 37 import com.sun.star.lang.IllegalArgumentException; 38 39 /*-************************************************************************ 40 @title helper to analyze necessary option properties of our filter 41 @description Our filter needs some necessary properties for working: 42 - a stream for input or output 43 - or an URL for creating such streams 44 - informations about required action on filtering 45 46 @attention This class mustn't be threadsafe - because instances of it 47 are used temp. only - not as members. So no concurrent access 48 should occur. 49 Another reason: It would be very difficult to safe every 50 access on our internal member. To do so - we must implement 51 special methods instead of allowing pure member access. 52 ************************************************************************-*/ 53 54 public class FilterOptions 55 { 56 //_____________________________________ 57 // public member to provide these options to our outside filter class 58 public com.sun.star.io.XInputStream m_xInput ; 59 public com.sun.star.io.XOutputStream m_xOutput ; 60 public boolean m_bStreamOwner ; 61 public String m_sURL ; 62 public String m_sOld ; 63 public String m_sNew ; 64 public boolean m_bCaseChange ; 65 public boolean m_bLower ; 66 67 //_____________________________________ 68 // private members for internal things 69 private XMultiComponentFactory m_xMCF ; 70 private XComponentContext m_Ctx ; 71 72 //_____________________________________ 73 // interface 74 /** 75 * creates a new instance of this class 76 * It use the given MediaDescriptor to find right 77 * properties for initialization of the internal members. 78 * To do so it use another interface method analyze() 79 * which can be used after creation of an object instance 80 * to set a new descriptor here. 81 * 82 * @param xSMGR 83 * we need it to create special help service top open 84 * streams in case they are not already a part of given 85 * MediaDescriptor 86 * 87 * @param bImport 88 * we must know which stream member should be valid initialized 89 * 90 * @param lDescriptor 91 * the initial MediaDescriptor to set internal member from it 92 */ FilterOptions( XMultiComponentFactory xMCF , XComponentContext Context , boolean bImport , com.sun.star.beans.PropertyValue[] lDescriptor )93 public FilterOptions( XMultiComponentFactory xMCF , 94 XComponentContext Context , 95 boolean bImport , 96 com.sun.star.beans.PropertyValue[] lDescriptor ) 97 { 98 m_xMCF = xMCF; 99 m_Ctx = Context; 100 analyze(bImport, lDescriptor); 101 } 102 103 /** 104 * analyze given MediaDescriptor to find values for our internal member 105 * It reset all members to defaults before - to prevent us against 106 * mixed descriptor values! 107 * 108 * @param bImport 109 * we must know which stream member should be valid initialized 110 * 111 * @param lDescriptor 112 * the new MediaDescriptor to set internal member from it 113 */ analyze( boolean bImport , com.sun.star.beans.PropertyValue[] lDescriptor )114 public void analyze( boolean bImport , 115 com.sun.star.beans.PropertyValue[] lDescriptor ) 116 { 117 m_xInput = null ; 118 m_xOutput = null ; 119 m_bStreamOwner = false ; 120 m_sURL = null ; 121 m_sOld = new String(); 122 m_sNew = new String(); 123 m_bCaseChange = false ; 124 m_bLower = false ; 125 126 for ( int i=0; i<lDescriptor.length; ++i ) 127 { 128 try 129 { 130 if (lDescriptor[i].Name.equals("FileName")) 131 m_sURL = AnyConverter.toString(lDescriptor[i].Value); 132 else 133 if (lDescriptor[i].Name.equals("InputStream")) 134 m_xInput = (com.sun.star.io.XInputStream)AnyConverter.toObject(new com.sun.star.uno.Type(com.sun.star.io.XInputStream.class), lDescriptor[i].Value); 135 else 136 if (lDescriptor[i].Name.equals("OutputStream")) 137 m_xOutput = (com.sun.star.io.XOutputStream)AnyConverter.toObject(new com.sun.star.uno.Type(com.sun.star.io.XOutputStream.class), lDescriptor[i].Value); 138 else 139 if (lDescriptor[i].Name.equals("FilterData")) 140 { 141 com.sun.star.beans.PropertyValue[] lFilterProps = (com.sun.star.beans.PropertyValue[])AnyConverter.toArray(lDescriptor[i].Value); 142 int nCount = lFilterProps.length; 143 for (int p=0; p<nCount; ++p) 144 { 145 if (lFilterProps[p].Name.equals("OldString")) 146 m_sOld = AnyConverter.toString(lFilterProps[p].Value); 147 else 148 if (lFilterProps[p].Name.equals("NewString")) 149 m_sNew = AnyConverter.toString(lFilterProps[p].Value); 150 else 151 if (lFilterProps[p].Name.equals("LowerCase")) 152 { 153 m_bLower = AnyConverter.toBoolean(lFilterProps[p].Value); 154 m_bCaseChange = true; // Set it after m_bLower - because an exception can occur and we must use default values then! 155 } 156 } 157 } 158 } 159 catch(com.sun.star.lang.IllegalArgumentException exConvert) 160 { 161 // ONE argument has the wrong type 162 // But I think we mustn't react here - because we set 163 // default values for every necessary item we need. 164 // In case this exception occurs - this default exist 165 // and we can live with it. 166 } 167 } 168 169 // Decide if it's necessary AND possible to open streams. 170 // Outside user can check for valid FilterOptions by using 171 // corresponding method isValid(). So it's not necessary to 172 // handle this error here in any case. 173 if (m_xInput==null && m_xOutput==null && m_sURL!=null) 174 impl_openStreams(bImport); 175 } 176 177 /** 178 * with this method it's possible for the outside filter to decide 179 * if he can use this FilterOptions really or not. 180 * That means especially if necessary streams are available or not. 181 */ isValid()182 public boolean isValid() 183 { 184 return(m_xInput!=null || m_xOutput!=null); 185 } 186 187 //_____________________________________ 188 // helper 189 /** 190 * In case we couldn't found any valid stream inside the given MediaDescriptor, 191 * we must create it. Then we use a special helper service in combination 192 * with an existing URL to open a stream for reading or writing. It depends 193 * from given parameter bImport. 194 * 195 * Note: This method doesn't check for a valid URL. It must be done before. 196 * 197 * @param bImport 198 * indicates which stream member must be valid as result of this call 199 */ impl_openStreams( boolean bImport )200 private void impl_openStreams( boolean bImport ) 201 { 202 try{ 203 com.sun.star.ucb.XSimpleFileAccess xHelper = (com.sun.star.ucb.XSimpleFileAccess)UnoRuntime.queryInterface( 204 com.sun.star.ucb.XSimpleFileAccess.class, 205 m_xMCF.createInstanceWithContext("com.sun.star.ucb.SimpleFileAccess", m_Ctx)); 206 if (xHelper!=null) 207 { 208 if (bImport==true) 209 m_xInput = xHelper.openFileRead(m_sURL); 210 else 211 m_xOutput = xHelper.openFileWrite(m_sURL); 212 } 213 214 m_bStreamOwner = (m_xInput!=null || m_xOutput!=null); 215 } 216 catch(com.sun.star.ucb.CommandAbortedException exAborted) {} 217 catch(com.sun.star.uno.Exception exUno ) {} 218 } 219 } 220