1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Filter.java,v $
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
;
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
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;
58 * Construct a new filter
61 oFilter
= new FilterImpl();
65 * Returns an instance of the filter.
66 * Arguments are not supported here and will be ignored.
67 * @param args The arguments.
70 public Object
createInstanceWithArguments(Object
[] args
) {
75 * Returns an instance of the filter.
78 public Object
createInstance() {
79 return createInstanceWithArguments(null);
83 * Get a unique id for this implementation.
86 public byte[] getImplementationId() {
87 return toString().getBytes();
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
]);
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
{
137 static String aState
;
139 static boolean bResult
;
142 * Constructs a new filter.
144 public FilterImpl() {
145 aState
= "just created";
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"))
175 else if (name
.equals("Result"))
176 return new Boolean(bResult
);
178 throw new NoSuchElementException();
183 * @return Always true.
185 public boolean hasElements() {
193 public Type
getElementType() {
198 * Get a unique id for this implementation.
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
]);
221 * Method initialize() creates a new thread that will try to start
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
);
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
){
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"