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 package basicrunner.basichelper; 24 25 import com.sun.star.lang.XInitialization; 26 import com.sun.star.lang.XServiceInfo; 27 import com.sun.star.lang.XTypeProvider; 28 import com.sun.star.uno.Type; 29 import com.sun.star.container.XNameAccess; 30 import com.sun.star.container.NoSuchElementException; 31 import com.sun.star.uno.UnoRuntime; 32 import com.sun.star.lang.XSingleServiceFactory; 33 import com.sun.star.document.XFilter; 34 import com.sun.star.beans.PropertyValue; 35 36 37 /** 38 * Provides an implementation of XFilter. 39 * @see com.sun.star.document.XFilter 40 * @see com.sun.star.lang.XServiceInfo 41 * @see com.sun.star.lang.XSingleServiceFactory 42 */ 43 public class Filter implements XServiceInfo, XSingleServiceFactory { 44 /** The service name of this class **/ 45 static final String __serviceName = "basichelper.Filter"; 46 /** The actual filter **/ 47 static FilterImpl oFilter = null; 48 49 /** 50 * Construct a new filter 51 */ Filter()52 public Filter() { 53 oFilter = new FilterImpl(); 54 } 55 56 /** 57 * Returns an instance of the filter. 58 * Arguments are not supported here and will be ignored. 59 * @param args The arguments. 60 * @return The filter. 61 */ createInstanceWithArguments(Object[] args)62 public Object createInstanceWithArguments(Object[] args) { 63 return oFilter; 64 } 65 66 /** 67 * Returns an instance of the filter. 68 * @return The filter. 69 */ createInstance()70 public Object createInstance() { 71 return createInstanceWithArguments(null); 72 } 73 74 /** 75 * Get a unique id for this implementation. 76 * @return The id. 77 */ getImplementationId()78 public byte[] getImplementationId() { 79 return toString().getBytes(); 80 } 81 82 /** 83 * Return all implemented types of this class. 84 * @return The implemented UNO types. 85 */ getTypes()86 public Type[] getTypes() { 87 Class interfaces[] = getClass().getInterfaces(); 88 89 Type types[] = new Type[interfaces.length]; 90 for(int i = 0; i < interfaces.length; ++ i) 91 types[i] = new Type(interfaces[i]); 92 93 return types; 94 } 95 96 /** Is this servioce supported? 97 * @param name The service name. 98 * @return True, if the service is supported. 99 */ supportsService(String name)100 public boolean supportsService(String name) { 101 return __serviceName.equals(name); 102 } 103 104 /** 105 * Get all supported service names. 106 * @return All supported servcices. 107 */ getSupportedServiceNames()108 public String[] getSupportedServiceNames() { 109 return new String[] {__serviceName}; 110 } 111 112 /** 113 * Get the implementation name of this class. 114 * @return The implementation name. 115 */ getImplementationName()116 public String getImplementationName() { 117 return getClass().getName(); 118 } 119 } 120 121 /** 122 * The actual filter implementation 123 * @see com.sun.star.lang.XInitialization; 124 * @see com.sun.star.lang.XTypeProvider; 125 * @see com.sun.star.container.XNameAccess; 126 */ 127 class FilterImpl implements XInitialization, XTypeProvider, XNameAccess { 128 /** A state **/ 129 static String aState; 130 /** A result **/ 131 static boolean bResult; 132 133 /** 134 * Constructs a new filter. 135 */ FilterImpl()136 public FilterImpl() { 137 aState = "just created"; 138 bResult = false; 139 } 140 141 /** 142 * Get the element names 143 * @return All element names. 144 */ getElementNames()145 public String[] getElementNames() { 146 return new String[]{"State", "Result"}; 147 } 148 149 /** 150 * Does this element exist? 151 * @param name The element name. 152 * @return True, if the name exists. 153 */ hasByName(String name)154 public boolean hasByName(String name) { 155 return (name.equals("State") || name.equals("Result")); 156 } 157 158 /** 159 * Get an element by its name. 160 * @param name The name of the element. 161 * @return The value of the element. 162 * @throws NoSuchElementException The element does not exist. 163 */ getByName(String name)164 public Object getByName(String name) throws NoSuchElementException{ 165 if (name.equals("State")) 166 return aState; 167 else if (name.equals("Result")) 168 return new Boolean(bResult); 169 else 170 throw new NoSuchElementException(); 171 } 172 173 /** 174 * Are there elements 175 * @return Always true. 176 */ hasElements()177 public boolean hasElements() { 178 return true; 179 } 180 181 /** 182 * Get element type. 183 * @return null. 184 */ getElementType()185 public Type getElementType() { 186 return null; 187 } 188 189 /** 190 * Get a unique id for this implementation. 191 * @return The id. 192 */ getImplementationId()193 public byte[] getImplementationId() { 194 return toString().getBytes(); 195 } 196 197 /** 198 * Return all implemented types of this class. 199 * @return The implemented UNO types. 200 */ getTypes()201 public Type[] getTypes() { 202 Class interfaces[] = getClass().getInterfaces(); 203 204 Type types[] = new Type[interfaces.length]; 205 for(int i = 0; i < interfaces.length; ++ i) 206 types[i] = new Type(interfaces[i]); 207 208 return types; 209 } 210 211 212 /** 213 * Method initialize() creates a new thread that will try to start 214 * filtering 215 * @param parm1 An instance of XFilter 216 * @see com.sun.star.document.XFilter 217 * @throws Exception Is thrown, when initialize fails. 218 */ initialize(Object[] parm1)219 public void initialize(Object[] parm1) throws com.sun.star.uno.Exception { 220 XFilter oFilter = (XFilter)UnoRuntime.queryInterface( 221 XFilter.class, parm1[0]); 222 //? PropertyValue[] FilterDesc = (PropertyValue[])AnyConverter.toArray(parm1[1]); 223 PropertyValue[] FilterDesc = (PropertyValue[])UnoRuntime.queryInterface(PropertyValue[].class, parm1[1]); 224 aState = "just initialized"; 225 FilterThread aThread = new FilterThread(oFilter, FilterDesc); 226 aThread.start(); 227 } 228 } 229 230 /** 231 * A thread for filtering. 232 */ 233 class FilterThread extends Thread { 234 /** The filter that is used **/ 235 XFilter oFilter = null; 236 /** Filter descriptions **/ 237 PropertyValue[] FilterDesc = null; 238 239 /** 240 * Construct the thread. 241 * @param oObj The filter. 242 * @param Desc The descriptions. 243 */ FilterThread(XFilter oObj, PropertyValue[] Desc)244 public FilterThread(XFilter oObj, PropertyValue[] Desc){ 245 oFilter = oObj; 246 FilterDesc = Desc; 247 } 248 249 /** 250 * Let the thread run 251 */ run()252 public void run(){ 253 boolean bOK; 254 try { 255 FilterImpl.aState = "before filtering"; 256 bOK = oFilter.filter(FilterDesc); 257 FilterImpl.aState = "filtering finished"; 258 FilterImpl.bResult = bOK; 259 } catch (Exception e) { 260 ConnectorImpl.aState = "error"; 261 throw new RuntimeException("Can't filtering exception" 262 + e.toString()); 263 } 264 } 265 } 266