Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Text / TextReplace.java
blob0e87656ebd0ecbf1d87d997bc5a1818efffeecc3
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
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.
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.
33 *************************************************************************/
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
43 import com.sun.star.uno.UnoRuntime;
45 public class TextReplace {
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();
54 com.sun.star.text.XTextDocument xTextDocument =
55 createTextdocument( xDesktop );
57 createExampleData( xTextDocument );
59 String mBritishWords[] = {"colour", "neighbour", "centre", "behaviour",
60 "metre", "through" };
61 String mUSWords[] = { "color", "neighbor", "center", "behavior",
62 "meter", "thru" };
64 try {
65 com.sun.star.util.XReplaceDescriptor xReplaceDescr = null;
66 com.sun.star.util.XReplaceable xReplaceable = null;
68 xReplaceable = UnoRuntime.queryInterface(
69 com.sun.star.util.XReplaceable.class, xTextDocument);
71 // You need a descriptor to set properies for Replace
72 xReplaceDescr = xReplaceable.createReplaceDescriptor();
74 System.out.println("Change all occurrences of ...");
75 for( int iArrayCounter = 0; iArrayCounter < mBritishWords.length;
76 iArrayCounter++ )
78 System.out.println(mBritishWords[iArrayCounter] +
79 " -> " + mUSWords[iArrayCounter]);
80 // Set the properties the replace method need
81 xReplaceDescr.setSearchString(mBritishWords[iArrayCounter] );
82 xReplaceDescr.setReplaceString(mUSWords[iArrayCounter] );
84 // Replace all words
85 xReplaceable.replaceAll( xReplaceDescr );
89 catch( Exception e) {
90 e.printStackTrace(System.err);
93 System.out.println("Done");
95 System.exit(0);
99 protected static void createExampleData(
100 com.sun.star.text.XTextDocument xTextDocument )
102 // Create textdocument and insert example text
103 com.sun.star.text.XTextCursor xTextCursor = null;
105 try {
106 xTextCursor = xTextDocument.getText().createTextCursor();
107 com.sun.star.text.XText xText = xTextDocument.getText();
109 xText.insertString( xTextCursor,
110 "He nervously looked all around. Suddenly he saw his ", false );
112 xText.insertString( xTextCursor, "neighbour ", true );
113 com.sun.star.beans.XPropertySet xCPS = UnoRuntime.queryInterface(
114 com.sun.star.beans.XPropertySet.class, xTextCursor);
115 // Set the word blue
116 xCPS.setPropertyValue( "CharColor", Integer.valueOf( 255 ) );
117 // Go to last character
118 xTextCursor.gotoEnd(false);
119 xCPS.setPropertyValue( "CharColor", Integer.valueOf( 0 ) );
121 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 );
123 xText.insertString( xTextCursor, "centre ", true );
124 xCPS = UnoRuntime.queryInterface(
125 com.sun.star.beans.XPropertySet.class, xTextCursor);
126 // Set the word blue
127 xCPS.setPropertyValue( "CharColor", Integer.valueOf( 255 ) );
128 // Go to last character
129 xTextCursor.gotoEnd(false);
130 xCPS.setPropertyValue( "CharColor", Integer.valueOf( 0 ) );
132 xText.insertString( xTextCursor, "of the sidewalk.", false );
134 xText.insertControlCharacter( xTextCursor,
135 com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false );
136 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 );
138 xTextCursor.gotoStart(false);
140 catch( Exception e) {
141 e.printStackTrace(System.err);
146 public static com.sun.star.frame.XDesktop getDesktop() {
147 com.sun.star.frame.XDesktop xDesktop = null;
148 com.sun.star.lang.XMultiComponentFactory xMCF = null;
150 try {
151 com.sun.star.uno.XComponentContext xContext = null;
153 // get the remote office component context
154 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
156 // get the remote office service manager
157 xMCF = xContext.getServiceManager();
158 if( xMCF != null ) {
159 System.out.println("Connected to a running office ...");
161 Object oDesktop = xMCF.createInstanceWithContext(
162 "com.sun.star.frame.Desktop", xContext);
163 xDesktop = UnoRuntime.queryInterface(
164 com.sun.star.frame.XDesktop.class, oDesktop);
166 else
167 System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
169 catch( Exception e) {
170 e.printStackTrace(System.err);
171 System.exit(1);
175 return xDesktop;
178 public static com.sun.star.text.XTextDocument createTextdocument(
179 com.sun.star.frame.XDesktop xDesktop )
181 com.sun.star.text.XTextDocument aTextDocument = null;
183 try {
184 com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop,
185 "swriter");
186 aTextDocument = UnoRuntime.queryInterface(
187 com.sun.star.text.XTextDocument.class, xComponent);
189 catch( Exception e) {
190 e.printStackTrace(System.err);
193 return aTextDocument;
197 protected static com.sun.star.lang.XComponent CreateNewDocument(
198 com.sun.star.frame.XDesktop xDesktop,
199 String sDocumentType )
201 String sURL = "private:factory/" + sDocumentType;
203 com.sun.star.lang.XComponent xComponent = null;
204 com.sun.star.frame.XComponentLoader xComponentLoader = null;
205 com.sun.star.beans.PropertyValue xEmptyArgs[] =
206 new com.sun.star.beans.PropertyValue[0];
208 try {
209 xComponentLoader = UnoRuntime.queryInterface(
210 com.sun.star.frame.XComponentLoader.class, xDesktop);
212 xComponent = xComponentLoader.loadComponentFromURL(
213 sURL, "_blank", 0, xEmptyArgs);
215 catch( Exception e) {
216 e.printStackTrace(System.err);
219 return xComponent ;