merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / runner / basicrunner / basichelper / Filter.java
blob552209609a55a7aab4ab7c6ef123efcd1d8cd3a0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Filter.java,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package basicrunner.basichelper;
32 import com.sun.star.lang.XInitialization;
33 import com.sun.star.lang.XServiceInfo;
34 import com.sun.star.lang.XTypeProvider;
35 import com.sun.star.uno.Type;
36 import com.sun.star.container.XNameAccess;
37 import com.sun.star.container.NoSuchElementException;
38 import com.sun.star.uno.AnyConverter;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.lang.XSingleServiceFactory;
41 import com.sun.star.document.XFilter;
42 import com.sun.star.beans.PropertyValue;
45 /**
46 * Provides an implementation of XFilter.
47 * @see com.sun.star.document.XFilter
48 * @see com.sun.star.lang.XServiceInfo
49 * @see com.sun.star.lang.XSingleServiceFactory
50 */
51 public class Filter implements XServiceInfo, XSingleServiceFactory {
52 /** The service name of this class **/
53 static final String __serviceName = "basichelper.Filter";
54 /** The actual filter **/
55 static FilterImpl oFilter = null;
57 /**
58 * Construct a new filter
60 public Filter() {
61 oFilter = new FilterImpl();
64 /**
65 * Returns an instance of the filter.
66 * Arguments are not supported here and will be ignored.
67 * @param args The arguments.
68 * @return The filter.
70 public Object createInstanceWithArguments(Object[] args) {
71 return oFilter;
74 /**
75 * Returns an instance of the filter.
76 * @return The filter.
78 public Object createInstance() {
79 return createInstanceWithArguments(null);
82 /**
83 * Get a unique id for this implementation.
84 * @return The id.
86 public byte[] getImplementationId() {
87 return toString().getBytes();
90 /**
91 * Return all implemented types of this class.
92 * @return The implemented UNO types.
94 public Type[] getTypes() {
95 Class interfaces[] = getClass().getInterfaces();
97 Type types[] = new Type[interfaces.length];
98 for(int i = 0; i < interfaces.length; ++ i)
99 types[i] = new Type(interfaces[i]);
101 return types;
104 /** Is this servioce supported?
105 * @param name The service name.
106 * @return True, if the service is supported.
108 public boolean supportsService(String name) {
109 return __serviceName.equals(name);
113 * Get all supported service names.
114 * @return All supported servcices.
116 public String[] getSupportedServiceNames() {
117 return new String[] {__serviceName};
121 * Get the implementation name of this class.
122 * @return The implementation name.
124 public String getImplementationName() {
125 return getClass().getName();
130 * The actual filter implementation
131 * @see com.sun.star.lang.XInitialization;
132 * @see com.sun.star.lang.XTypeProvider;
133 * @see com.sun.star.container.XNameAccess;
135 class FilterImpl implements XInitialization, XTypeProvider, XNameAccess {
136 /** A state **/
137 static String aState;
138 /** A result **/
139 static boolean bResult;
142 * Constructs a new filter.
144 public FilterImpl() {
145 aState = "just created";
146 bResult = false;
150 * Get the element names
151 * @return All element names.
153 public String[] getElementNames() {
154 return new String[]{"State", "Result"};
158 * Does this element exist?
159 * @param name The element name.
160 * @return True, if the name exists.
162 public boolean hasByName(String name) {
163 return (name.equals("State") || name.equals("Result"));
167 * Get an element by its name.
168 * @param name The name of the element.
169 * @return The value of the element.
170 * @throws NoSuchElementException The element does not exist.
172 public Object getByName(String name) throws NoSuchElementException{
173 if (name.equals("State"))
174 return aState;
175 else if (name.equals("Result"))
176 return new Boolean(bResult);
177 else
178 throw new NoSuchElementException();
182 * Are there elements
183 * @return Always true.
185 public boolean hasElements() {
186 return true;
190 * Get element type.
191 * @return null.
193 public Type getElementType() {
194 return null;
198 * Get a unique id for this implementation.
199 * @return The id.
201 public byte[] getImplementationId() {
202 return toString().getBytes();
206 * Return all implemented types of this class.
207 * @return The implemented UNO types.
209 public Type[] getTypes() {
210 Class interfaces[] = getClass().getInterfaces();
212 Type types[] = new Type[interfaces.length];
213 for(int i = 0; i < interfaces.length; ++ i)
214 types[i] = new Type(interfaces[i]);
216 return types;
221 * Method initialize() creates a new thread that will try to start
222 * filtering
223 * @param parm1 An instance of XFilter
224 * @see com.sun.star.document.XFilter
225 * @throws Exception Is thrown, when initialize fails.
227 public void initialize(Object[] parm1) throws com.sun.star.uno.Exception {
228 XFilter oFilter = (XFilter)UnoRuntime.queryInterface(
229 XFilter.class, parm1[0]);
230 PropertyValue[] FilterDesc = (PropertyValue[])AnyConverter.toArray(parm1[1]);
231 aState = "just initialized";
232 FilterThread aThread = new FilterThread(oFilter, FilterDesc);
233 aThread.start();
238 * A thread for filtering.
240 class FilterThread extends Thread {
241 /** The filter that is used **/
242 XFilter oFilter = null;
243 /** Filter descriptions **/
244 PropertyValue[] FilterDesc = null;
247 * Construct the thread.
248 * @param oObj The filter.
249 * @param Desc The descriptions.
251 public FilterThread(XFilter oObj, PropertyValue[] Desc){
252 oFilter = oObj;
253 FilterDesc = Desc;
257 * Let the thread run
259 public void run(){
260 boolean bOK;
261 try {
262 FilterImpl.aState = "before filtering";
263 bOK = oFilter.filter(FilterDesc);
264 FilterImpl.aState = "filtering finished";
265 FilterImpl.bResult = bOK;
266 } catch (Exception e) {
267 ConnectorImpl.aState = "error";
268 throw new RuntimeException("Can't filtering exception"
269 + e.toString());