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