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 package com.sun.star.demo;
25 
26 
27 import java.io.IOException;
28 
29 
30 import com.sun.star.beans.PropertyValue;
31 import com.sun.star.beans.PropertyState;
32 
33 import com.sun.star.bridge.XUnoUrlResolver;
34 
35 import com.sun.star.io.BufferSizeExceededException;
36 import com.sun.star.io.NotConnectedException;
37 import com.sun.star.io.XInputStream;
38 import com.sun.star.io.XOutputStream;
39 
40 import com.sun.star.frame.XComponentLoader;
41 
42 import com.sun.star.lang.XComponent;
43 import com.sun.star.lang.XMultiServiceFactory;
44 import com.sun.star.lang.XServiceInfo;
45 
46 import com.sun.star.text.XSimpleText;
47 import com.sun.star.text.XText;
48 import com.sun.star.text.XTextCursor;
49 import com.sun.star.text.XTextDocument;
50 import com.sun.star.text.XTextRange;
51 
52 import com.sun.star.uno.IBridge;
53 import com.sun.star.uno.UnoRuntime;
54 import com.sun.star.uno.XInterface;
55 import com.sun.star.uno.XNamingService;
56 
57 
58 public class TestOffice {
testPipe(XMultiServiceFactory rSmgr)59 	static void testPipe(XMultiServiceFactory rSmgr) throws com.sun.star.uno.Exception {
60 		XOutputStream rOut = (XOutputStream) rSmgr.createInstance("com.sun.star.io.Pipe");
61 
62 		{
63 			byte bytes[] = new byte[10];
64 			bytes[0] = 42;
65 			rOut.writeBytes(bytes);
66 		}
67 
68 
69 		{
70 			byte bytes[][] = new byte[1][];
71 
72 			XInputStream rIn = UnoRuntime.queryInterface(XInputStream.class, rOut);
73 			if(rIn.available() != 10)
74 				System.err.println("wrong bytes available\n");
75 
76 			if(rIn.readBytes(bytes, 10) != 10)
77 				System.err.println("wrong bytes read\n");
78 
79 			if(42 != bytes[0][0])
80 				System.err.println("wrong element in sequence\n");
81 		}
82 	}
83 
84 
testWriter(XComponent rCmp)85 	static void testWriter(XComponent rCmp) throws IOException {
86 		XTextDocument rTextDoc = UnoRuntime.queryInterface(XTextDocument.class, rCmp);
87 
88 		XText rText = UnoRuntime.queryInterface(XText.class, rTextDoc.getText());
89 		XTextCursor rCursor = UnoRuntime.queryInterface(XTextCursor.class, rText.createTextCursor());
90 		XTextRange rRange = UnoRuntime.queryInterface(XTextRange.class, rCursor);
91 
92 		byte pcText[] = new byte[1024];
93 		pcText[0] = 0;
94 		System.err.println("pleast type any text\n");
95 		while(true)	{
96 			System.in.read(pcText);
97 
98 			String string = new String(pcText);
99 			if(string.equals("end")) break;
100 
101 			string += " ";
102 
103 			rText.insertString(rRange , string, false);
104 		}
105 	}
106 
testDocument(XMultiServiceFactory rSmgr)107 	static void testDocument(XMultiServiceFactory rSmgr) throws com.sun.star.uno.Exception, IOException {
108 		XComponentLoader rLoader = UnoRuntime.queryInterface(XComponentLoader.class, rSmgr.createInstance("com.sun.star.frame.Desktop"));
109 
110 		String urls[] = new String[] {
111 			"private:factory/swriter",
112 			"private:factory/scalc",
113 			"private:factory/sdraw",
114 			"http://www.heise.de",
115 		};
116 
117 		String docu[] = new String[] {
118 			"a new writer document ...\n",
119 			"a new calc document ...\n",
120 			"a new draw document ...\n",
121 			"www.heise.de\n",
122 		};
123 
124 		for(int i = 0; i < urls.length; ++ i)	{
125 			System.err.println("press any key to open "  + docu[i]);
126 
127 			System.in.read();
128 			while(System.in.available() > 0)
129 				System.in.read();
130 
131 			XComponent rComponent = rLoader.loadComponentFromURL(urls[i], "_blank",	0, new PropertyValue[0]);
132 
133 //    			testWriter(rComponent);
134 			System.err.println("press any key to close the document");
135 			System.in.read();
136 			while(System.in.available() > 0)
137 				System.in.read();
138 
139 			rComponent.dispose();
140 		}
141 	}
142 
doSomething(Object r)143 	static void doSomething(Object r) throws com.sun.star.uno.Exception, IOException {
144 		XNamingService rName = UnoRuntime.queryInterface(XNamingService.class, r);
145 
146 		if(rName != null) {
147 			System.err.println("got the remote naming service !");
148 			Object rXsmgr = rName.getRegisteredObject("StarOffice.ServiceManager");
149 
150 			XMultiServiceFactory rSmgr = UnoRuntime.queryInterface(XMultiServiceFactory.class, rXsmgr);
151 			if(rSmgr != null) {
152 				System.err.println("got the remote service manager !");
153 //  				testPipe(rSmgr);
154 				testDocument(rSmgr);
155 			}
156 		}
157 	}
158 
159 
160 
161 	static String neededServices[] = new String[] {
162 		"com.sun.star.comp.servicemanager.ServiceManager",
163 		"com.sun.star.comp.loader.JavaLoader",
164 		"com.sun.star.comp.connections.Connector",
165 		"com.sun.star.comp.bridgefactory.BridgeFactory",
166 		"com.sun.star.comp.urlresolver.UrlResolver"
167 	};
168 
main(String argv[])169 	public static void main(String argv[]) throws Exception {
170 		if(argv.length != 1)	{
171 			System.err.println("usage : testoffice uno:connection;protocol;objectName");
172 			System.exit(-1);
173 		}
174 
175 		com.sun.star.comp.servicemanager.ServiceManager smgr = new com.sun.star.comp.servicemanager.ServiceManager();
176 		smgr.addFactories(neededServices);
177 
178 		Object  resolver  = smgr.createInstance("com.sun.star.bridge.UnoUrlResolver" );
179 		XUnoUrlResolver resolver_xUnoUrlResolver = UnoRuntime.queryInterface(XUnoUrlResolver.class, resolver);
180 
181   		Object rInitialObject = resolver_xUnoUrlResolver.resolve(argv[0]);
182 
183 		if(rInitialObject != null) {
184 			System.err.println("got the remote object");
185 			doSomething(rInitialObject);
186 		}
187 	}
188 }
189 
190