1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
37 // comment: Step 1: get the Desktop object from the office
38 // Step 2: open an empty text document
39 // Step 3: enter an example text
40 // Step 4: replace some english spelled words with US spelled
44 import com
.sun
.star
.uno
.UnoRuntime
;
46 public class TextReplace
{
48 public static void main(String args
[]) {
49 // You need the desktop to create a document
50 // The getDesktop method does the UNO bootstrapping, gets the
51 // remote service manager and the desktop object.
52 com
.sun
.star
.frame
.XDesktop xDesktop
= null;
53 xDesktop
= getDesktop();
55 com
.sun
.star
.text
.XTextDocument xTextDocument
=
56 createTextdocument( xDesktop
);
58 createExampleData( xTextDocument
);
60 String mBritishWords
[] = {"colour", "neighbour", "centre", "behaviour",
62 String mUSWords
[] = { "color", "neighbor", "center", "behavior",
66 com
.sun
.star
.util
.XReplaceDescriptor xReplaceDescr
= null;
67 com
.sun
.star
.util
.XReplaceable xReplaceable
= null;
69 xReplaceable
= UnoRuntime
.queryInterface(
70 com
.sun
.star
.util
.XReplaceable
.class, xTextDocument
);
72 // You need a descriptor to set properties for Replace
73 xReplaceDescr
= xReplaceable
.createReplaceDescriptor();
75 System
.out
.println("Change all occurrences of ...");
76 for( int iArrayCounter
= 0; iArrayCounter
< mBritishWords
.length
;
79 System
.out
.println(mBritishWords
[iArrayCounter
] +
80 " -> " + mUSWords
[iArrayCounter
]);
81 // Set the properties the replace method need
82 xReplaceDescr
.setSearchString(mBritishWords
[iArrayCounter
] );
83 xReplaceDescr
.setReplaceString(mUSWords
[iArrayCounter
] );
86 xReplaceable
.replaceAll( xReplaceDescr
);
91 e
.printStackTrace(System
.err
);
94 System
.out
.println("Done");
100 protected static void createExampleData(
101 com
.sun
.star
.text
.XTextDocument xTextDocument
)
103 // Create textdocument and insert example text
104 com
.sun
.star
.text
.XTextCursor xTextCursor
= null;
107 xTextCursor
= xTextDocument
.getText().createTextCursor();
108 com
.sun
.star
.text
.XText xText
= xTextDocument
.getText();
110 xText
.insertString( xTextCursor
,
111 "He nervously looked all around. Suddenly he saw his ", false );
113 xText
.insertString( xTextCursor
, "neighbour ", true );
114 com
.sun
.star
.beans
.XPropertySet xCPS
= UnoRuntime
.queryInterface(
115 com
.sun
.star
.beans
.XPropertySet
.class, xTextCursor
);
117 xCPS
.setPropertyValue( "CharColor", Integer
.valueOf( 255 ) );
118 // Go to last character
119 xTextCursor
.gotoEnd(false);
120 xCPS
.setPropertyValue( "CharColor", Integer
.valueOf( 0 ) );
122 xText
.insertString( xTextCursor
, "in the alley. Like lightning he darted off to the left and disappeared between the two warehouses almost falling over the trash can lying in the ", false );
124 xText
.insertString( xTextCursor
, "centre ", true );
125 xCPS
= UnoRuntime
.queryInterface(
126 com
.sun
.star
.beans
.XPropertySet
.class, xTextCursor
);
128 xCPS
.setPropertyValue( "CharColor", Integer
.valueOf( 255 ) );
129 // Go to last character
130 xTextCursor
.gotoEnd(false);
131 xCPS
.setPropertyValue( "CharColor", Integer
.valueOf( 0 ) );
133 xText
.insertString( xTextCursor
, "of the sidewalk.", false );
135 xText
.insertControlCharacter( xTextCursor
,
136 com
.sun
.star
.text
.ControlCharacter
.PARAGRAPH_BREAK
, false );
137 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 );
139 xTextCursor
.gotoStart(false);
141 catch( Exception e
) {
142 e
.printStackTrace(System
.err
);
147 public static com
.sun
.star
.frame
.XDesktop
getDesktop() {
148 com
.sun
.star
.frame
.XDesktop xDesktop
= null;
149 com
.sun
.star
.lang
.XMultiComponentFactory xMCF
= null;
152 com
.sun
.star
.uno
.XComponentContext xContext
= null;
154 // get the remote office component context
155 xContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
157 // get the remote office service manager
158 xMCF
= xContext
.getServiceManager();
160 System
.out
.println("Connected to a running office ...");
162 Object oDesktop
= xMCF
.createInstanceWithContext(
163 "com.sun.star.frame.Desktop", xContext
);
164 xDesktop
= UnoRuntime
.queryInterface(
165 com
.sun
.star
.frame
.XDesktop
.class, oDesktop
);
168 System
.out
.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
170 catch( Exception e
) {
171 e
.printStackTrace(System
.err
);
179 public static com
.sun
.star
.text
.XTextDocument
createTextdocument(
180 com
.sun
.star
.frame
.XDesktop xDesktop
)
182 com
.sun
.star
.text
.XTextDocument aTextDocument
= null;
185 com
.sun
.star
.lang
.XComponent xComponent
= CreateNewDocument(xDesktop
,
187 aTextDocument
= UnoRuntime
.queryInterface(
188 com
.sun
.star
.text
.XTextDocument
.class, xComponent
);
190 catch( Exception e
) {
191 e
.printStackTrace(System
.err
);
194 return aTextDocument
;
198 protected static com
.sun
.star
.lang
.XComponent
CreateNewDocument(
199 com
.sun
.star
.frame
.XDesktop xDesktop
,
200 String sDocumentType
)
202 String sURL
= "private:factory/" + sDocumentType
;
204 com
.sun
.star
.lang
.XComponent xComponent
= null;
205 com
.sun
.star
.frame
.XComponentLoader xComponentLoader
= null;
206 com
.sun
.star
.beans
.PropertyValue xEmptyArgs
[] =
207 new com
.sun
.star
.beans
.PropertyValue
[0];
210 xComponentLoader
= UnoRuntime
.queryInterface(
211 com
.sun
.star
.frame
.XComponentLoader
.class, xDesktop
);
213 xComponent
= xComponentLoader
.loadComponentFromURL(
214 sURL
, "_blank", 0, xEmptyArgs
);
216 catch( Exception e
) {
217 e
.printStackTrace(System
.err
);
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */