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 import java.io.*;
25 import java.util.*;
26 import com.sun.star.uno.*;
27 import com.sun.star.lang.*;
28 import com.sun.star.beans.*;
29 
30 import com.sun.star.comp.loader.FactoryHelper;
31 import com.sun.star.registry.XRegistryKey;
32 
33 import com.sun.star.xml.*;
34 import com.sun.star.xml.sax.*;
35 
36 import com.sun.star.io.XInputStream;
37 import com.sun.star.io.XOutputStream;
38 import com.sun.star.io.XActiveDataSource;
39 
40 
41 
42 public class FlatXml implements XImportFilter, XExportFilter, XServiceName,
43         XServiceInfo, XDocumentHandler, XTypeProvider
44 {
45 
46     /*
47      * private data members
48      */
49     private XMultiServiceFactory m_xServiceFactory;
50     private XExtendedDocumentHandler m_xHandler;
51     private boolean m_bPrettyPrint = true;
52 
53     static private final String __serviceName = "devguide.officedev.samples.filter.FlatXmlJava";
54     static private final String __implName = "FlatXml";
55     static private final String[] __supportedServiceNames = {
56         "devguide.officedev.samples.filter.FlatXmlJava"
57     };
58 
FlatXml(XMultiServiceFactory f)59     public FlatXml(XMultiServiceFactory f) {
60         m_xServiceFactory = f;
61     }
62 
63     // --- XTypeProvider ---
getImplementationId()64     public byte[] getImplementationId() {
65         return Integer.toString(this.hashCode()).getBytes();
66     }
67 
68     // --- XServiceName ---
getServiceName()69     public String getServiceName() {
70         return( __serviceName );
71     }
72 
73     // --- XServiceInfo ---
supportsService(String sName)74     public boolean supportsService(String sName) {
75         for (int i = 0; i < __supportedServiceNames.length; i++) {
76             if (__supportedServiceNames[i].equals(sName)) return true;
77         }
78         return false;
79     }
getImplementationName()80     public String getImplementationName() {
81         return( this.getClass().getName() );
82     }
getSupportedServiceNames()83     public String[] getSupportedServiceNames() {
84         return( __supportedServiceNames );
85     }
86 
getTypes()87     public com.sun.star.uno.Type[] getTypes() {
88         Type[] typeReturn = {};
89         try {
90             typeReturn = new Type[] {
91                 new Type( XTypeProvider.class ),
92                 new Type( XExportFilter.class ),
93 		        new Type( XImportFilter.class ),
94                 new Type( XServiceName.class ),
95                 new Type( XServiceInfo.class )
96             };
97         } catch( java.lang.Exception exception ) {
98             return null;
99         }
100         return( typeReturn );
101     }
102 
importer(PropertyValue[] aSourceData, XDocumentHandler xDocHandler, String[] msUserData)103     public boolean importer(PropertyValue[] aSourceData, XDocumentHandler xDocHandler, String[] msUserData)
104         throws com.sun.star.uno.RuntimeException, com.sun.star.lang.IllegalArgumentException
105     {
106         String sName = null;
107         String sFileName = null;
108 	    String sURL = null;
109 	    com.sun.star.io.XInputStream xin = null;
110 
111 	    try {
112 
113     	    for  (int  i = 0 ; i < aSourceData.length; i++)
114 	        {
115 		        sName = aSourceData[i].Name;
116 		        if (sName.equals("InputStream"))
117 			        xin = (XInputStream)AnyConverter.toObject(XInputStream.class, aSourceData[i].Value);
118 		        if (sName.equals("URL"))
119 			        sURL=(String)AnyConverter.toObject(String.class, aSourceData[i].Value);
120 		        if (sName.equals("FileName"))
121 			        sFileName=(String)AnyConverter.toObject(String.class, aSourceData[i].Value);
122 		    }
123 
124 		    Object tmpObj=m_xServiceFactory.createInstance("com.sun.star.xml.sax.Parser");
125             if (tmpObj == null) return false;
126 
127 		    XParser xParser = (XParser)UnoRuntime.queryInterface(XParser.class , tmpObj);
128 		    if (xParser == null) return false;
129 
130 		    InputSource aInput = new InputSource();
131         	aInput.sSystemId = sURL;
132 		    aInput.aInputStream =xin;
133             xParser.setDocumentHandler ( xDocHandler );
134 	        xParser.parseStream ( aInput );
135 	    } catch (com.sun.star.uno.Exception e){
136 		    e.printStackTrace();
137 		    return false;
138 	    }
139 
140 	    // done...
141 	    return true;
142 	}
143 
exporter(PropertyValue[] aSourceData, String[] msUserData)144     public boolean exporter(PropertyValue[] aSourceData, String[] msUserData)
145         throws com.sun.star.uno.RuntimeException, com.sun.star.lang.IllegalArgumentException
146     {
147         try {
148 	        String sURL = null;
149 	        String sName = null;
150 	        XOutputStream xos = null;
151 
152     		// get interesting values from sourceData
153 	        for  (int  i = 0 ; i < aSourceData.length; i++)
154 	        {
155 		        sName = aSourceData[i].Name;
156 		        if (sName.equals("OutputStream"))
157 			        xos = (XOutputStream)AnyConverter.toObject(XOutputStream.class, aSourceData[i].Value);
158 		        if (sName.equals("URL"))
159 			        sURL=(String)AnyConverter.toObject(String.class, aSourceData[i].Value);
160 		    }
161 
162 		    // prepare the XML writer
163 		    Object tmpObj = null;
164 		    if (m_xHandler == null)
165 		    {
166 		        tmpObj = m_xServiceFactory.createInstance("com.sun.star.xml.sax.Writer");
167 		        if (tmpObj != null)
168 		            m_xHandler = (XExtendedDocumentHandler)UnoRuntime.queryInterface(XExtendedDocumentHandler.class, tmpObj);
169 		    }
170 		    if (m_xHandler == null)
171 		        return false;
172 
173             // Connect the provided output stream to the writer
174 		    XActiveDataSource xADSource = (XActiveDataSource)UnoRuntime.queryInterface(
175 		            XActiveDataSource.class, m_xHandler);
176 
177 		    if (xADSource != null && xos != null)
178 		        xADSource.setOutputStream(xos);
179 		    else
180 		        return false;
181         } catch (com.sun.star.uno.Exception e){
182             return false;
183         }
184 
185         // done ...
186         return true;
187     }
188 
startDocument()189     public void  startDocument ()
190         throws com.sun.star.xml.sax.SAXException
191     {
192         m_xHandler.startDocument();
193     }
194 
endDocument()195     public void endDocument()
196         throws com.sun.star.xml.sax.SAXException
197     {
198         m_xHandler.endDocument();
199     }
200 
startElement(String str, com.sun.star.xml.sax.XAttributeList xattribs)201     public void startElement (String str, com.sun.star.xml.sax.XAttributeList xattribs)
202         throws com.sun.star.xml.sax.SAXException
203     {
204         m_xHandler.startElement(str, xattribs);
205     }
206 
endElement(String str)207     public void endElement(String str)
208         throws com.sun.star.xml.sax.SAXException
209     {
210         m_xHandler.endElement(str);
211     }
212 
characters(String str)213     public void characters(String str)
214         throws com.sun.star.xml.sax.SAXException
215     {
216         m_xHandler.characters(str);
217     }
218 
ignorableWhitespace(String str)219     public void ignorableWhitespace(String str)
220         throws com.sun.star.xml.sax.SAXException
221     {
222         if (!m_bPrettyPrint) return;
223         else m_xHandler.ignorableWhitespace(str);
224     }
225 
processingInstruction(String aTarget, String aData)226     public void processingInstruction(String aTarget, String aData)
227         throws com.sun.star.xml.sax.SAXException
228     {
229         m_xHandler.processingInstruction(aTarget, aData);
230     }
231 
setDocumentLocator(XLocator xLocator)232     public void setDocumentLocator(XLocator xLocator)
233         throws com.sun.star.xml.sax.SAXException
234     {
235         m_xHandler.setDocumentLocator(xLocator);
236     }
237 
238 	// ------------------------------------------------------------
239     // component management
240 
__getServiceFactory(String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)241     public static XSingleServiceFactory __getServiceFactory(String implName,
242             XMultiServiceFactory multiFactory, XRegistryKey regKey)
243     {
244         XSingleServiceFactory xSingleServiceFactory = null;
245         if (implName.equals(__implName) ) {
246             try {
247                 xSingleServiceFactory = FactoryHelper.getServiceFactory(
248                     Class.forName(implName), __serviceName, multiFactory, regKey);
249             } catch (java.lang.ClassNotFoundException e) {
250                 return null;
251             }
252         }
253         return xSingleServiceFactory;
254     }
255 
256     // This method not longer necessary since OOo 3.4 where the component registration
257     // was changed to passive component registration. For more details see
258     // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration
259 
260 //     public static boolean __writeRegistryServiceInfo(XRegistryKey regKey)
261 //     {
262 //         return FactoryHelper.writeRegistryServiceInfo(__implName,
263 //             __serviceName, regKey);
264 //     }
265 
266 }
267