update dev300-m58
[ooovba.git] / filter / source / xsltvalidate / XSLTValidate.java
blob4d87eb04c5d9a9452ae23927b3a46ed4e2a9f2e4
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: XSLTValidate.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 ************************************************************************/
32 import com.sun.star.comp.loader.FactoryHelper;
33 import com.sun.star.lang.XMultiServiceFactory;
34 import com.sun.star.lang.XServiceInfo;
35 import com.sun.star.lang.XServiceName;
36 import com.sun.star.lang.XSingleServiceFactory;
37 import com.sun.star.lang.XTypeProvider;
38 import com.sun.star.registry.XRegistryKey;
39 import com.sun.star.uno.Type;
40 import java.util.Enumeration;
41 import java.util.Vector;
42 import com.sun.star.xml.XImportFilter;
43 import com.sun.star.xml.XExportFilter;
45 // Imported TraX classes
46 import javax.xml.parsers.DocumentBuilder;
47 import javax.xml.parsers.DocumentBuilderFactory;
49 import org.xml.sax.ErrorHandler;
50 import org.xml.sax.SAXException;
51 import org.xml.sax.SAXParseException;
53 import com.sun.star.uno.AnyConverter;
55 //Uno to java Adaptor
56 import com.sun.star.lib.uno.adapter.*;
58 /** This outer class provides an inner class to implement the service
59 * description, a method to instantiate the
60 * component on demand (__getServiceFactory()), and a method to give
61 * information about the component (__writeRegistryServiceInfo()).
63 public class XSLTValidate {
65 private static XMultiServiceFactory xMSF;
66 private static Vector parseErrors =new Vector();
68 /** This inner class provides the component as a concrete implementation
69 * of the service description. It implements the needed interfaces.
70 * @implements XTypeProvider
72 static public class _XSLTValidate implements
73 XImportFilter,
74 XServiceName,
75 XServiceInfo,
76 ErrorHandler,
77 XTypeProvider {
79 private com.sun.star.xml.sax.XErrorHandler xErrorHandler;
81 /** The component will be registered under this name.
83 static private final String __serviceName = "com.sun.star.documentconversion.XSLTValidate";
85 public _XSLTValidate() {
86 xErrorHandler = null;
89 public com.sun.star.uno.Type[] getTypes() {
90 Type[] typeReturn = {};
92 try {
93 typeReturn = new Type[] {
94 new Type( XTypeProvider.class ),
95 new Type( XExportFilter.class ),
96 new Type( XImportFilter.class ),
97 new Type( XServiceName.class ),
98 new Type( XServiceInfo.class ) };
100 catch( Exception exception ) {
104 return( typeReturn );
108 public boolean importer(com.sun.star.beans.PropertyValue[] aSourceData,
109 com.sun.star.xml.sax.XDocumentHandler xDocHandler,
110 java.lang.String[] msUserData) throws com.sun.star.uno.RuntimeException,com.sun.star.lang.IllegalArgumentException {
112 com.sun.star.io.XInputStream xis=null;
113 com.sun.star.beans.PropertyValue[] pValue = aSourceData;
114 for (int i = 0 ; i < pValue.length; i++)
116 try{
117 //System.out.println("\n"+pValue[i].Name+" "+pValue[i].Value);
118 if (pValue[i].Name.compareTo("InputStream")==0){
119 xis=(com.sun.star.io.XInputStream)AnyConverter.toObject(new Type(com.sun.star.io.XInputStream.class), pValue[i].Value);
121 else if (pValue[i].Name.compareTo("ErrorHandler")==0){
122 xErrorHandler=(com.sun.star.xml.sax.XErrorHandler)AnyConverter.toObject(new Type(com.sun.star.xml.sax.XErrorHandler.class), pValue[i].Value);
125 catch(com.sun.star.lang.IllegalArgumentException AnyExec){
126 System.out.println("\nIllegalArgumentException "+AnyExec);
129 try{
130 convert (xis);
132 catch (Exception AnyExec){
133 throw new com.sun.star.uno.RuntimeException(AnyExec.getMessage());
135 return true;
138 public void convert (com.sun.star.io.XInputStream xml) throws com.sun.star.uno.RuntimeException {
139 XInputStreamToInputStreamAdapter xis =new XInputStreamToInputStreamAdapter(xml);
140 parseErrors =new Vector();
141 //String defaultTimeOut = System.getProperty("sun.net.client.defaultConnectTimeout");
142 System.getProperties().setProperty("sun.net.client.defaultConnectTimeout", "10000");
143 try{
144 DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
145 dFactory.setNamespaceAware(true);
146 dFactory.setValidating(true);
147 DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
148 dBuilder.setErrorHandler(this);
149 dBuilder.parse(xis);
150 if (parseErrors.size()>0){
151 String errString ="";
152 for (Enumeration e = parseErrors.elements() ; e.hasMoreElements() ;) {
153 errString+=e.nextElement();
154 //System.out.println(e.nextElement());
156 throw new com.sun.star.uno.RuntimeException(errString);
159 catch (Exception e){
160 //System.out.println("\nException "+ e);
161 throw new com.sun.star.uno.RuntimeException(e.getLocalizedMessage());
165 public com.sun.star.uno.Any JavaSAXParseEceptionToUno( SAXParseException e )
167 com.sun.star.uno.XInterface xContext = null;
169 String aMessage = e.getMessage();
170 if( aMessage == null )
171 aMessage = new String();
173 String aPublicId = e.getPublicId();
174 if( aPublicId == null )
175 aPublicId = new String();
177 String aSystemId = e.getSystemId();
178 if( aSystemId == null )
179 aSystemId = new String();
181 return new com.sun.star.uno.Any( new Type(com.sun.star.xml.sax.SAXParseException.class),
182 new com.sun.star.xml.sax.SAXParseException( aMessage,
183 xContext,
184 com.sun.star.uno.Any.VOID,
185 aPublicId,
186 aSystemId,
187 e.getLineNumber(),
188 e.getColumnNumber() ) );
192 // Warning Event Handler
193 public void warning (SAXParseException e)
194 throws SAXException
196 // System.out.println("\n_XSLTValidate::warning " + e.toString() );
198 if( xErrorHandler != null )
202 xErrorHandler.warning( JavaSAXParseEceptionToUno( e ) );
204 catch( com.sun.star.xml.sax.SAXException ex )
206 throw e;
209 else
212 //System.err.println ("Warning: "+e);
213 try{
214 //parseErrors.write (("\n"+e.getMessage()).getBytes());
216 catch(Exception genEx){
217 //System.out.print("\n Error while writing ParseErrors"+genEx);
222 // Error Event Handler
223 public void error (SAXParseException e)
224 throws SAXException
226 // System.out.println("\n_XSLTValidate::error " + e.toString() );
228 if( xErrorHandler != null )
232 xErrorHandler.error( JavaSAXParseEceptionToUno( e ) );
234 catch( com.sun.star.xml.sax.SAXException ex )
236 throw e;
239 else
241 //System.err.println ("Error: "+e);
242 try{
243 parseErrors.add (e.getLocalizedMessage()+" "+e.getLineNumber()+" ");
245 catch(Exception genEx){
246 //System.out.print("\n Error while writing ParseErrors"+genEx);
251 // Fatal Error Event Handler
252 public void fatalError (SAXParseException e)
253 throws SAXException {
254 // System.out.println("\n_XSLTValidate::fatalError " + e.toString() );
256 if( xErrorHandler != null )
260 xErrorHandler.fatalError( JavaSAXParseEceptionToUno( e ) );
262 catch( com.sun.star.xml.sax.SAXException ex )
264 throw e;
267 else
269 //System.err.println ("Fatal Error: "+e);
270 try{
271 parseErrors.add (e.getLocalizedMessage()+" "+e.getLineNumber()+" ");
273 catch(Exception genEx){
274 //System.out.print("\n Error while writing ParseErrors"+genEx);
279 // Implement methods from interface XTypeProvider
280 public byte[] getImplementationId() {
281 byte[] byteReturn = {};
283 byteReturn = new String( "" + this.hashCode() ).getBytes();
285 return( byteReturn );
288 // Implement method from interface XServiceName
289 public String getServiceName() {
290 return( __serviceName );
293 // Implement methods from interface XServiceInfo
294 public boolean supportsService(String stringServiceName) {
295 return( stringServiceName.equals( __serviceName ) );
298 public String getImplementationName() {
299 return( _XSLTValidate.class.getName() );
302 public String[] getSupportedServiceNames() {
303 String[] stringSupportedServiceNames = { __serviceName };
304 return( stringSupportedServiceNames );
309 * Returns a factory for creating the service.
310 * This method is called by the <code>JavaLoader</code>
312 * @return returns a <code>XSingleServiceFactory</code> for creating the
313 * component
315 * @param implName the name of the implementation for which a
316 * service is desired
317 * @param multiFactory the service manager to be used if needed
318 * @param regKey the registryKey
320 * @see com.sun.star.comp.loader.JavaLoader
322 public static XSingleServiceFactory __getServiceFactory(String implName,
323 XMultiServiceFactory multiFactory,
324 XRegistryKey regKey) {
325 XSingleServiceFactory xSingleServiceFactory = null;
326 xMSF= multiFactory;
327 if (implName.equals(_XSLTValidate.class.getName()) ) {
328 xSingleServiceFactory = FactoryHelper.getServiceFactory(_XSLTValidate.class,
329 _XSLTValidate.__serviceName,
330 multiFactory,
331 regKey);
334 return xSingleServiceFactory;
338 * Writes the service information into the given registry key.
339 * This method is called by the <code>JavaLoader</code>
340 * <p>
341 * @return returns true if the operation succeeded
342 * @param regKey the registryKey
343 * @see com.sun.star.comp.loader.JavaLoader
345 public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
347 return FactoryHelper.writeRegistryServiceInfo(_XSLTValidate.class.getName(),
348 _XSLTValidate.__serviceName, regKey);