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 
25 import com.sun.star.comp.loader.FactoryHelper;
26 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.lang.XServiceInfo;
28 import com.sun.star.lang.XServiceName;
29 import com.sun.star.lang.XSingleServiceFactory;
30 import com.sun.star.lang.XTypeProvider;
31 import com.sun.star.registry.XRegistryKey;
32 import com.sun.star.uno.Type;
33 import java.util.Enumeration;
34 import java.util.Vector;
35 import com.sun.star.xml.XImportFilter;
36 import com.sun.star.xml.XExportFilter;
37 
38 // Imported TraX classes
39 import javax.xml.parsers.DocumentBuilder;
40 import javax.xml.parsers.DocumentBuilderFactory;
41 
42 import org.xml.sax.ErrorHandler;
43 import org.xml.sax.SAXException;
44 import org.xml.sax.SAXParseException;
45 
46 import com.sun.star.uno.AnyConverter;
47 
48 //Uno to java Adaptor
49 import com.sun.star.lib.uno.adapter.*;
50 
51 /** This outer class provides an inner class to implement the service
52  * description and a method to instantiate the
53  * component on demand (__getServiceFactory()).
54  */
55 public class XSLTValidate {
56 
57     private static XMultiServiceFactory xMSF;
58     private static Vector parseErrors =new Vector();
59 
60     /** This inner class provides the component as a concrete implementation
61      * of the service description. It implements the needed interfaces.
62      * @implements XTypeProvider
63      */
64     static public class _XSLTValidate implements
65 		XImportFilter,
66         XServiceName,
67         XServiceInfo,
68 		ErrorHandler,
69         XTypeProvider {
70 
71 		private com.sun.star.xml.sax.XErrorHandler xErrorHandler;
72 
73         /** The component will be registered under this name.
74          */
75         static private final String __serviceName = "com.sun.star.documentconversion.XSLTValidate";
76 
_XSLTValidate()77 		public _XSLTValidate() {
78 			xErrorHandler = null;
79 		}
80 
getTypes()81         public com.sun.star.uno.Type[] getTypes() {
82             Type[] typeReturn = {};
83 
84             try {
85                 typeReturn = new Type[] {
86                 new Type( XTypeProvider.class ),
87                 new Type( XExportFilter.class ),
88 				new Type( XImportFilter.class ),
89                 new Type( XServiceName.class ),
90                 new Type( XServiceInfo.class ) };
91             }
92             catch( Exception exception ) {
93 
94             }
95 
96             return( typeReturn );
97         }
98 
99 
importer(com.sun.star.beans.PropertyValue[] aSourceData, com.sun.star.xml.sax.XDocumentHandler xDocHandler, java.lang.String[] msUserData)100 	public boolean importer(com.sun.star.beans.PropertyValue[] aSourceData,
101 				com.sun.star.xml.sax.XDocumentHandler xDocHandler,
102 				java.lang.String[] msUserData) throws com.sun.star.uno.RuntimeException,com.sun.star.lang.IllegalArgumentException {
103 
104 	    com.sun.star.io.XInputStream xis=null;
105 	    com.sun.star.beans.PropertyValue[] pValue = aSourceData;
106 		for  (int  i = 0 ; i < pValue.length; i++)
107 		{
108 			try{
109 				//System.out.println("\n"+pValue[i].Name+" "+pValue[i].Value);
110 				if (pValue[i].Name.compareTo("InputStream")==0){
111 					xis=(com.sun.star.io.XInputStream)AnyConverter.toObject(new Type(com.sun.star.io.XInputStream.class), pValue[i].Value);
112 				}
113 				else if (pValue[i].Name.compareTo("ErrorHandler")==0){
114 					xErrorHandler=(com.sun.star.xml.sax.XErrorHandler)AnyConverter.toObject(new Type(com.sun.star.xml.sax.XErrorHandler.class), pValue[i].Value);
115 				}
116 			}
117 			catch(com.sun.star.lang.IllegalArgumentException AnyExec){
118 				System.out.println("\nIllegalArgumentException "+AnyExec);
119 			}
120 		}
121 		try{
122 			convert (xis);
123 		}
124 		catch (Exception AnyExec){
125 			throw new com.sun.star.uno.RuntimeException(AnyExec.getMessage());
126 		}
127 		return true;
128 	}
129 
convert(com.sun.star.io.XInputStream xml)130 	 public void convert (com.sun.star.io.XInputStream xml) throws com.sun.star.uno.RuntimeException {
131 	     XInputStreamToInputStreamAdapter xis =new XInputStreamToInputStreamAdapter(xml);
132 	     parseErrors =new Vector();
133            //String defaultTimeOut = System.getProperty("sun.net.client.defaultConnectTimeout");
134            System.getProperties().setProperty("sun.net.client.defaultConnectTimeout", "10000");
135 	     try{
136 			 DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
137 			 dFactory.setNamespaceAware(true);
138 			 dFactory.setValidating(true);
139 			 DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
140 			 dBuilder.setErrorHandler(this);
141 			 dBuilder.parse(xis);
142 			 if (parseErrors.size()>0){
143 				 String errString ="";
144 				 for (Enumeration e = parseErrors.elements() ; e.hasMoreElements() ;) {
145 					errString+=e.nextElement();
146 					//System.out.println(e.nextElement());
147 				 }
148 				 throw new com.sun.star.uno.RuntimeException(errString);
149 			 }
150 	     }
151 	    catch (Exception e){
152 		   //System.out.println("\nException "+ e);
153 		   throw new com.sun.star.uno.RuntimeException(e.getLocalizedMessage());
154 	    }
155 	 }
156 
JavaSAXParseEceptionToUno( SAXParseException e )157 	public com.sun.star.uno.Any JavaSAXParseEceptionToUno( SAXParseException e )
158 	{
159 		com.sun.star.uno.XInterface xContext = null;
160 
161 		String aMessage = e.getMessage();
162 		if( aMessage == null )
163 			aMessage = new String();
164 
165 		String aPublicId = e.getPublicId();
166 		if( aPublicId == null )
167 			aPublicId = new String();
168 
169 		String aSystemId = e.getSystemId();
170 		if( aSystemId == null )
171 			aSystemId = new String();
172 
173 		return new com.sun.star.uno.Any( new Type(com.sun.star.xml.sax.SAXParseException.class),
174 					new com.sun.star.xml.sax.SAXParseException( aMessage,
175 					xContext,
176 					com.sun.star.uno.Any.VOID,
177 					aPublicId,
178 					aSystemId,
179 					e.getLineNumber(),
180 					e.getColumnNumber() ) );
181 
182 	}
183 
184 	//  Warning Event Handler
warning(SAXParseException e)185 	public void warning (SAXParseException e)
186 			throws SAXException
187 	{
188 //		System.out.println("\n_XSLTValidate::warning " + e.toString() );
189 
190 		if( xErrorHandler != null )
191 		{
192 			try
193 			{
194 				xErrorHandler.warning( JavaSAXParseEceptionToUno( e ) );
195 			}
196 			catch( com.sun.star.xml.sax.SAXException ex )
197 			{
198 				throw e;
199 			}
200 		}
201 		else
202 		{
203 
204 			//System.err.println ("Warning:  "+e);
205 			try{
206 				//parseErrors.write (("\n"+e.getMessage()).getBytes());
207 			}
208 			catch(Exception genEx){
209 				//System.out.print("\n Error while writing ParseErrors"+genEx);
210 			}
211 		}
212 	}
213 
214 	//  Error Event Handler
error(SAXParseException e)215 	public void error (SAXParseException e)
216 		throws SAXException
217 	{
218 //		System.out.println("\n_XSLTValidate::error " + e.toString() );
219 
220 		if( xErrorHandler != null )
221 		{
222 			try
223 			{
224 				xErrorHandler.error( JavaSAXParseEceptionToUno( e ) );
225 			}
226 			catch( com.sun.star.xml.sax.SAXException ex )
227 			{
228 				throw e;
229 			}
230 		}
231 		else
232 		{
233 			//System.err.println ("Error:  "+e);
234 			try{
235 				parseErrors.add (e.getLocalizedMessage()+" "+e.getLineNumber()+" ");
236 			}
237 			catch(Exception genEx){
238 				//System.out.print("\n Error while writing ParseErrors"+genEx);
239 			}
240 		}
241 	}
242 
243 	//  Fatal Error Event Handler
fatalError(SAXParseException e)244 	public void fatalError (SAXParseException e)
245 	throws SAXException {
246 //		System.out.println("\n_XSLTValidate::fatalError " + e.toString() );
247 
248 		if( xErrorHandler != null )
249 		{
250 			try
251 			{
252 				xErrorHandler.fatalError(  JavaSAXParseEceptionToUno( e ) );
253 			}
254 			catch( com.sun.star.xml.sax.SAXException ex )
255 			{
256 				throw e;
257 			}
258 		}
259 		else
260 		{
261 			//System.err.println ("Fatal Error:  "+e);
262 			try{
263 				parseErrors.add (e.getLocalizedMessage()+" "+e.getLineNumber()+" ");
264 			}
265 			catch(Exception genEx){
266 				//System.out.print("\n Error while writing ParseErrors"+genEx);
267 			}
268 		}
269 	}
270 
271         // Implement methods from interface XTypeProvider
getImplementationId()272         public byte[] getImplementationId() {
273             byte[] byteReturn = {};
274 
275             byteReturn = new String( "" + this.hashCode() ).getBytes();
276 
277             return( byteReturn );
278         }
279 
280         // Implement method from interface XServiceName
getServiceName()281         public String getServiceName() {
282             return( __serviceName );
283         }
284 
285         // Implement methods from interface XServiceInfo
supportsService(String stringServiceName)286         public boolean supportsService(String stringServiceName) {
287             return( stringServiceName.equals( __serviceName ) );
288         }
289 
getImplementationName()290         public String getImplementationName() {
291             return( _XSLTValidate.class.getName() );
292         }
293 
getSupportedServiceNames()294         public String[] getSupportedServiceNames() {
295             String[] stringSupportedServiceNames = { __serviceName };
296             return( stringSupportedServiceNames );
297         }
298     }
299 
300     /**
301      * Returns a factory for creating the service.
302      * This method is called by the <code>JavaLoader</code>
303      *
304      * @return  returns a <code>XSingleServiceFactory</code> for creating the
305      *          component
306      *
307      * @param   implName     the name of the implementation for which a
308      *                       service is desired
309      * @param   multiFactory the service manager to be used if needed
310      * @param   regKey       the registryKey
311      *
312      * @see                  com.sun.star.comp.loader.JavaLoader
313      */
__getServiceFactory(String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)314     public static XSingleServiceFactory __getServiceFactory(String implName,
315     XMultiServiceFactory multiFactory,
316     XRegistryKey regKey) {
317         XSingleServiceFactory xSingleServiceFactory = null;
318         xMSF= multiFactory;
319         if (implName.equals(_XSLTValidate.class.getName()) ) {
320             xSingleServiceFactory = FactoryHelper.getServiceFactory(_XSLTValidate.class,
321             _XSLTValidate.__serviceName,
322             multiFactory,
323             regKey);
324         }
325 
326         return xSingleServiceFactory;
327     }
328 }
329