1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 //***************************************************************************
36 // comment: Step 1: get the Desktop object from the office
37 //          Step 2: open an empty text document
38 //          Step 3: enter a example text
39 //          Step 4: replace some english spelled words with US spelled
40 //***************************************************************************
41 
42 
43 import com.sun.star.uno.UnoRuntime;
44 
45 public class TextReplace {
46 
47     public static void main(String args[]) {
48         // You need the desktop to create a document
49         // The getDesktop method does the UNO bootstrapping, gets the
50         // remote servie manager and the desktop object.
51         com.sun.star.frame.XDesktop xDesktop = null;
52         xDesktop = getDesktop();
53 
54         com.sun.star.text.XTextDocument xTextDocument =
55             createTextdocument( xDesktop );
56 
57         createExampleData( xTextDocument );
58 
59         String mBritishWords[] = {"colour", "neighbour", "centre", "behaviour",
60                                   "metre", "through" };
61         String mUSWords[] = { "color", "neighbor", "center", "behavior",
62                               "meter", "thru" };
63 
64         try {
65             com.sun.star.util.XReplaceDescriptor xReplaceDescr = null;
66             com.sun.star.util.XSearchDescriptor xSearchDescriptor = null;
67             com.sun.star.util.XReplaceable xReplaceable = null;
68 
69             xReplaceable = (com.sun.star.util.XReplaceable)
70                 UnoRuntime.queryInterface(
71                     com.sun.star.util.XReplaceable.class, xTextDocument);
72 
73             // You need a descriptor to set properies for Replace
74             xReplaceDescr = (com.sun.star.util.XReplaceDescriptor)
75                 xReplaceable.createReplaceDescriptor();
76 
77             System.out.println("Change all occurrences of ...");
78             for( int iArrayCounter = 0; iArrayCounter < mBritishWords.length;
79                  iArrayCounter++ )
80             {
81                 System.out.println(mBritishWords[iArrayCounter] +
82                     " -> " + mUSWords[iArrayCounter]);
83                 // Set the properties the replace method need
84                 xReplaceDescr.setSearchString(mBritishWords[iArrayCounter] );
85                 xReplaceDescr.setReplaceString(mUSWords[iArrayCounter] );
86 
87                 // Replace all words
88                 xReplaceable.replaceAll( xReplaceDescr );
89             }
90 
91         }
92         catch( Exception e) {
93             e.printStackTrace(System.err);
94         }
95 
96         System.out.println("Done");
97 
98         System.exit(0);
99 
100     }
101 
102     protected static void createExampleData(
103         com.sun.star.text.XTextDocument xTextDocument )
104     {
105         // Create textdocument and insert example text
106         com.sun.star.text.XTextCursor xTextCursor = null;
107 
108         try {
109             xTextCursor = (com.sun.star.text.XTextCursor)
110                 xTextDocument.getText().createTextCursor();
111             com.sun.star.text.XText xText = (com.sun.star.text.XText)
112                 xTextDocument.getText();
113 
114             xText.insertString( xTextCursor,
115                 "He nervously looked all around. Suddenly he saw his ", false );
116 
117             xText.insertString( xTextCursor, "neighbour ", true );
118             com.sun.star.beans.XPropertySet xCPS = (com.sun.star.beans.XPropertySet)
119                 UnoRuntime.queryInterface(
120                     com.sun.star.beans.XPropertySet.class, xTextCursor);
121             // Set the word blue
122             xCPS.setPropertyValue( "CharColor", new Integer( 255 ) );
123             // Go to last character
124             xTextCursor.gotoEnd(false);
125             xCPS.setPropertyValue( "CharColor", new Integer( 0 ) );
126 
127             xText.insertString( xTextCursor, "in the alley. Like lightening he darted off to the left and disappeared between the two warehouses almost falling over the trash can lying in the ", false  );
128 
129             xText.insertString( xTextCursor, "centre ", true );
130             xCPS = (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
131                 com.sun.star.beans.XPropertySet.class, xTextCursor);
132             // Set the word blue
133             xCPS.setPropertyValue( "CharColor", new Integer( 255 ) );
134             // Go to last character
135             xTextCursor.gotoEnd(false);
136             xCPS.setPropertyValue( "CharColor", new Integer( 0 ) );
137 
138             xText.insertString( xTextCursor, "of the sidewalk.", false );
139 
140             xText.insertControlCharacter( xTextCursor,
141                       com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false );
142             xText.insertString( xTextCursor, "He tried to nervously tap his way along in the inky darkness and suddenly stiffened: it was a dead-end, he would have to go back the way he had come.", false );
143 
144             xTextCursor.gotoStart(false);
145         }
146         catch( Exception e) {
147             e.printStackTrace(System.err);
148         }
149 
150     }
151 
152     public static com.sun.star.frame.XDesktop getDesktop() {
153         com.sun.star.frame.XDesktop xDesktop = null;
154         com.sun.star.lang.XMultiComponentFactory xMCF = null;
155 
156         try {
157             com.sun.star.uno.XComponentContext xContext = null;
158 
159             // get the remote office component context
160             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
161 
162             // get the remote office service manager
163             xMCF = xContext.getServiceManager();
164             if( xMCF != null ) {
165                 System.out.println("Connected to a running office ...");
166 
167                 Object oDesktop = xMCF.createInstanceWithContext(
168                     "com.sun.star.frame.Desktop", xContext);
169                 xDesktop = (com.sun.star.frame.XDesktop) UnoRuntime.queryInterface(
170                     com.sun.star.frame.XDesktop.class, oDesktop);
171             }
172             else
173                 System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
174         }
175         catch( Exception e) {
176             e.printStackTrace(System.err);
177             System.exit(1);
178         }
179 
180 
181         return xDesktop;
182     }
183 
184     public static com.sun.star.text.XTextDocument createTextdocument(
185         com.sun.star.frame.XDesktop xDesktop )
186     {
187         com.sun.star.text.XTextDocument aTextDocument = null;
188 
189         try {
190             com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop,
191                                                                         "swriter");
192             aTextDocument = (com.sun.star.text.XTextDocument)
193                 UnoRuntime.queryInterface(
194                     com.sun.star.text.XTextDocument.class, xComponent);
195         }
196         catch( Exception e) {
197             e.printStackTrace(System.err);
198         }
199 
200         return aTextDocument;
201     }
202 
203 
204     protected static com.sun.star.lang.XComponent CreateNewDocument(
205         com.sun.star.frame.XDesktop xDesktop,
206         String sDocumentType )
207     {
208         String sURL = "private:factory/" + sDocumentType;
209 
210         com.sun.star.lang.XComponent xComponent = null;
211         com.sun.star.frame.XComponentLoader xComponentLoader = null;
212         com.sun.star.beans.PropertyValue xValues[] =
213             new com.sun.star.beans.PropertyValue[1];
214         com.sun.star.beans.PropertyValue xEmptyArgs[] =
215             new com.sun.star.beans.PropertyValue[0];
216 
217         try {
218             xComponentLoader = (com.sun.star.frame.XComponentLoader)
219                 UnoRuntime.queryInterface(
220                     com.sun.star.frame.XComponentLoader.class, xDesktop);
221 
222             xComponent  = xComponentLoader.loadComponentFromURL(
223                 sURL, "_blank", 0, xEmptyArgs);
224         }
225         catch( Exception e) {
226             e.printStackTrace(System.err);
227         }
228 
229         return xComponent ;
230     }
231 }
232