summaryrefslogtreecommitdiff
path: root/filter/source/xsltvalidate/XSLTValidate.java
blob: 17aeb6a4364b113bd740d0ae091e2dba2561c0ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */


import com.sun.star.comp.loader.FactoryHelper;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.lang.XServiceInfo;
import com.sun.star.lang.XServiceName;
import com.sun.star.lang.XSingleServiceFactory;
import com.sun.star.lang.XTypeProvider;
import com.sun.star.registry.XRegistryKey;
import com.sun.star.uno.Type;
import java.util.Iterator;
import java.util.ArrayList;
import com.sun.star.xml.XImportFilter;
import com.sun.star.xml.XExportFilter;

// Imported TraX classes
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import com.sun.star.uno.AnyConverter;

//Uno to java Adaptor
import com.sun.star.lib.uno.adapter.*;

/** This outer class provides an inner class to implement the service
 * description and a method to instantiate the
 * component on demand (__getServiceFactory()).
 */
public class XSLTValidate {

    private static XMultiServiceFactory xMSF;
    private static ArrayList<String> parseErrors = new ArrayList<String>();

    /** This inner class provides the component as a concrete implementation
     * of the service description. It implements the needed interfaces.
     * @implements XTypeProvider
     */
    static public class _XSLTValidate implements
        XImportFilter,
        XServiceName,
        XServiceInfo,
        ErrorHandler,
        XTypeProvider {

        private com.sun.star.xml.sax.XErrorHandler xErrorHandler;

        /** The component will be registered under this name.
         */
        static private final String __serviceName = "com.sun.star.documentconversion.XSLTValidate";

        public _XSLTValidate() {
            xErrorHandler = null;
        }

        public com.sun.star.uno.Type[] getTypes() {
            Type[] typeReturn = {};

            try {
                typeReturn = new Type[] {
                new Type( XTypeProvider.class ),
                new Type( XExportFilter.class ),
                new Type( XImportFilter.class ),
                new Type( XServiceName.class ),
                new Type( XServiceInfo.class ) };
            }
            catch( Exception exception ) {

            }

            return( typeReturn );
        }


    public boolean importer(com.sun.star.beans.PropertyValue[] aSourceData,
                com.sun.star.xml.sax.XDocumentHandler xDocHandler,
                java.lang.String[] msUserData) throws com.sun.star.uno.RuntimeException,com.sun.star.lang.IllegalArgumentException {

        com.sun.star.io.XInputStream xis=null;
        com.sun.star.beans.PropertyValue[] pValue = aSourceData;
        for  (int  i = 0 ; i < pValue.length; i++)
        {
            try{
                //System.out.println("\n"+pValue[i].Name+" "+pValue[i].Value);
                if (pValue[i].Name.compareTo("InputStream")==0){
                    xis=(com.sun.star.io.XInputStream)AnyConverter.toObject(new Type(com.sun.star.io.XInputStream.class), pValue[i].Value);
                }
                else if (pValue[i].Name.compareTo("ErrorHandler")==0){
                    xErrorHandler=(com.sun.star.xml.sax.XErrorHandler)AnyConverter.toObject(new Type(com.sun.star.xml.sax.XErrorHandler.class), pValue[i].Value);
                }
            }
            catch(com.sun.star.lang.IllegalArgumentException AnyExec){
                System.out.println("\nIllegalArgumentException "+AnyExec);
            }
        }
        try{
            convert (xis);
        }
        catch (Exception AnyExec){
            throw new com.sun.star.uno.RuntimeException(AnyExec.getMessage());
        }
        return true;
    }

     public void convert (com.sun.star.io.XInputStream xml) throws com.sun.star.uno.RuntimeException {
         XInputStreamToInputStreamAdapter xis =new XInputStreamToInputStreamAdapter(xml);
         parseErrors = new ArrayList<String>();
           //String defaultTimeOut = System.getProperty("sun.net.client.defaultConnectTimeout");
           System.getProperties().setProperty("sun.net.client.defaultConnectTimeout", "10000");
         try{
             DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
             dFactory.setNamespaceAware(true);
             dFactory.setValidating(true);
             DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
             dBuilder.setErrorHandler(this);
             dBuilder.parse(xis);
             if (parseErrors.size()>0){
                 String errString ="";
                 for (Iterator<String> e = parseErrors.iterator() ; e.hasNext() ;) {
                    errString += e.next();
                    //System.out.println(e.nextElement());
                 }
                 throw new com.sun.star.uno.RuntimeException(errString);
             }
         }
        catch (Exception e){
           //System.out.println("\nException "+ e);
           throw new com.sun.star.uno.RuntimeException(e.getLocalizedMessage());
        }
     }

    public com.sun.star.uno.Any JavaSAXParseEceptionToUno( SAXParseException e )
    {
        com.sun.star.uno.XInterface xContext = null;

        String aMessage = e.getMessage();
        if( aMessage == null )
            aMessage = new String();

        String aPublicId = e.getPublicId();
        if( aPublicId == null )
            aPublicId = new String();

        String aSystemId = e.getSystemId();
        if( aSystemId == null )
            aSystemId = new String();

        return new com.sun.star.uno.Any( new Type(com.sun.star.xml.sax.SAXParseException.class),
                    new com.sun.star.xml.sax.SAXParseException( aMessage,
                    xContext,
                    com.sun.star.uno.Any.VOID,
                    aPublicId,
                    aSystemId,
                    e.getLineNumber(),
                    e.getColumnNumber() ) );

    }

    //  Warning Event Handler
    public void warning (SAXParseException e)
            throws SAXException
    {
//      System.out.println("\n_XSLTValidate::warning " + e.toString() );

        if( xErrorHandler != null )
        {
            try
            {
                xErrorHandler.warning( JavaSAXParseEceptionToUno( e ) );
            }
            catch( com.sun.star.xml.sax.SAXException ex )
            {
                throw e;
            }
        }
        else
        {

            //System.err.println ("Warning:  "+e);
            try{
                //parseErrors.write (("\n"+e.getMessage()).getBytes());
            }
            catch(Exception genEx){
                //System.out.print("\n Error while writing ParseErrors"+genEx);
            }
        }
    }

    //  Error Event Handler
    public void error (SAXParseException e)
        throws SAXException
    {
//      System.out.println("\n_XSLTValidate::error " + e.toString() );

        if( xErrorHandler != null )
        {
            try
            {
                xErrorHandler.error( JavaSAXParseEceptionToUno( e ) );
            }
            catch( com.sun.star.xml.sax.SAXException ex )
            {
                throw e;
            }
        }
        else
        {
            //System.err.println ("Error:  "+e);
            try{
                parseErrors.add (e.getLocalizedMessage()+" "+e.getLineNumber()+" ");
            }
            catch(Exception genEx){
                //System.out.print("\n Error while writing ParseErrors"+genEx);
            }
        }
    }

    //  Fatal Error Event Handler
    public void fatalError (SAXParseException e)
    throws SAXException {
//      System.out.println("\n_XSLTValidate::fatalError " + e.toString() );

        if( xErrorHandler != null )
        {
            try
            {
                xErrorHandler.fatalError(  JavaSAXParseEceptionToUno( e ) );
            }
            catch( com.sun.star.xml.sax.SAXException ex )
            {
                throw e;
            }
        }
        else
        {
            //System.err.println ("Fatal Error:  "+e);
            try{
                parseErrors.add (e.getLocalizedMessage()+" "+e.getLineNumber()+" ");
            }
            catch(Exception genEx){
                //System.out.print("\n Error while writing ParseErrors"+genEx);
            }
        }
    }

        // Implement methods from interface XTypeProvider
        public byte[] getImplementationId() {
            byte[] byteReturn = {};

            byteReturn = new String( "" + this.hashCode() ).getBytes();

            return( byteReturn );
        }

        // Implement method from interface XServiceName
        public String getServiceName() {
            return( __serviceName );
        }

        // Implement methods from interface XServiceInfo
        public boolean supportsService(String stringServiceName) {
            return( stringServiceName.equals( __serviceName ) );
        }

        public String getImplementationName() {
            return( _XSLTValidate.class.getName() );
        }

        public String[] getSupportedServiceNames() {
            String[] stringSupportedServiceNames = { __serviceName };
            return( stringSupportedServiceNames );
        }
    }

    /**
     * Returns a factory for creating the service.
     * This method is called by the <code>JavaLoader</code>
     *
     * @return  returns a <code>XSingleServiceFactory</code> for creating the
     *          component
     *
     * @param   implName     the name of the implementation for which a
     *                       service is desired
     * @param   multiFactory the service manager to be used if needed
     * @param   regKey       the registryKey
     *
     * @see                  com.sun.star.comp.loader.JavaLoader
     */
    public static XSingleServiceFactory __getServiceFactory(String implName,
    XMultiServiceFactory multiFactory,
    XRegistryKey regKey) {
        XSingleServiceFactory xSingleServiceFactory = null;
        xMSF= multiFactory;
        if (implName.equals(_XSLTValidate.class.getName()) ) {
            xSingleServiceFactory = FactoryHelper.getServiceFactory(_XSLTValidate.class,
            _XSLTValidate.__serviceName,
            multiFactory,
            regKey);
        }

        return xSingleServiceFactory;
    }
}