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: bootstrap UNO and get the remote component context 37*cdf0e10cSrcweir // Step 2: open an empty text document 38*cdf0e10cSrcweir // Step 3: create an enumeration of all paragraphs 39*cdf0e10cSrcweir // Step 4: create an enumeration of all text portions 40*cdf0e10cSrcweir //*************************************************************************** 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir public class TextDocumentStructure { 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir public static void main(String args[]) { 47*cdf0e10cSrcweir com.sun.star.uno.XComponentContext xContext = null; 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir try { 50*cdf0e10cSrcweir // get the remote office component context 51*cdf0e10cSrcweir xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 52*cdf0e10cSrcweir System.out.println("Connected to a running office ..."); 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir // get the rmeote service manger 55*cdf0e10cSrcweir com.sun.star.lang.XMultiComponentFactory xMCF = 56*cdf0e10cSrcweir xContext.getServiceManager(); 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir // create a new instance of the desktop 59*cdf0e10cSrcweir Object oDesktop = xMCF.createInstanceWithContext( 60*cdf0e10cSrcweir "com.sun.star.frame.Desktop", xContext); 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir // get the component laoder from the desktop to create a new 63*cdf0e10cSrcweir // text document 64*cdf0e10cSrcweir com.sun.star.frame.XComponentLoader xCLoader = 65*cdf0e10cSrcweir (com.sun.star.frame.XComponentLoader) 66*cdf0e10cSrcweir UnoRuntime.queryInterface( 67*cdf0e10cSrcweir com.sun.star.frame.XComponentLoader.class,oDesktop); 68*cdf0e10cSrcweir com.sun.star.beans.PropertyValue [] szEmptyArgs = 69*cdf0e10cSrcweir new com.sun.star.beans.PropertyValue [0]; 70*cdf0e10cSrcweir String strDoc = "private:factory/swriter"; 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir System.out.println("create new text document"); 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir com.sun.star.lang.XComponent xComp = xCLoader.loadComponentFromURL( 75*cdf0e10cSrcweir strDoc, "_blank", 0, szEmptyArgs); 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir // query the new document for the XTextDocument interface 78*cdf0e10cSrcweir com.sun.star.text.XTextDocument xTextDocument = 79*cdf0e10cSrcweir (com.sun.star.text.XTextDocument)UnoRuntime.queryInterface( 80*cdf0e10cSrcweir com.sun.star.text.XTextDocument.class, xComp); 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir // create some example data 83*cdf0e10cSrcweir com.sun.star.text.XText xText = xTextDocument.getText(); 84*cdf0e10cSrcweir createExampleData( xText ); 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir // Begin section 'The structure of text documents' of the Tutorial 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir com.sun.star.container.XEnumeration xParagraphEnumeration = null; 89*cdf0e10cSrcweir com.sun.star.container.XEnumerationAccess xParaEnumerationAccess = null; 90*cdf0e10cSrcweir com.sun.star.container.XEnumeration xPortionEnumeration = null; 91*cdf0e10cSrcweir com.sun.star.container.XEnumeration xTextPortionEnum; 92*cdf0e10cSrcweir com.sun.star.text.XTextContent xTextElement = null; 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir System.out.println("create an enumeration of all paragraphs"); 95*cdf0e10cSrcweir // create an enumeration access of all paragraphs of a document 96*cdf0e10cSrcweir com.sun.star.container.XEnumerationAccess xEnumerationAccess = 97*cdf0e10cSrcweir (com.sun.star.container.XEnumerationAccess) 98*cdf0e10cSrcweir UnoRuntime.queryInterface( 99*cdf0e10cSrcweir com.sun.star.container.XEnumerationAccess.class, xText); 100*cdf0e10cSrcweir xParagraphEnumeration = xEnumerationAccess.createEnumeration(); 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir // Loop through all paragraphs of the document 103*cdf0e10cSrcweir while ( xParagraphEnumeration.hasMoreElements() ) { 104*cdf0e10cSrcweir xTextElement = (com.sun.star.text.XTextContent) 105*cdf0e10cSrcweir UnoRuntime.queryInterface( 106*cdf0e10cSrcweir com.sun.star.text.XTextContent.class, 107*cdf0e10cSrcweir xParagraphEnumeration.nextElement()); 108*cdf0e10cSrcweir com.sun.star.lang.XServiceInfo xServiceInfo = 109*cdf0e10cSrcweir (com.sun.star.lang.XServiceInfo)UnoRuntime.queryInterface( 110*cdf0e10cSrcweir com.sun.star.lang.XServiceInfo.class, xTextElement); 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir // check ifs the current paragraph really a paragraph or an 113*cdf0e10cSrcweir // anchor of a frame or picture 114*cdf0e10cSrcweir if( xServiceInfo.supportsService("com.sun.star.text.Paragraph") ) { 115*cdf0e10cSrcweir com.sun.star.text.XTextRange xTextRange = 116*cdf0e10cSrcweir xTextElement.getAnchor(); 117*cdf0e10cSrcweir System.out.println( "This is a Paragraph" ); 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir // create another enumeration to get all text portions of 120*cdf0e10cSrcweir // the paragraph 121*cdf0e10cSrcweir xParaEnumerationAccess = 122*cdf0e10cSrcweir (com.sun.star.container.XEnumerationAccess) 123*cdf0e10cSrcweir UnoRuntime.queryInterface( 124*cdf0e10cSrcweir com.sun.star.container.XEnumerationAccess.class, 125*cdf0e10cSrcweir xTextElement); 126*cdf0e10cSrcweir xTextPortionEnum = xParaEnumerationAccess.createEnumeration(); 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir while ( xTextPortionEnum.hasMoreElements() ) { 129*cdf0e10cSrcweir com.sun.star.text.XTextRange xTextPortion = 130*cdf0e10cSrcweir (com.sun.star.text.XTextRange)UnoRuntime.queryInterface( 131*cdf0e10cSrcweir com.sun.star.text.XTextRange.class, 132*cdf0e10cSrcweir xTextPortionEnum.nextElement()); 133*cdf0e10cSrcweir System.out.println( "Text from the portion : " 134*cdf0e10cSrcweir + xTextPortion.getString() ); 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir com.sun.star.beans.XPropertySet xPropertySet = 137*cdf0e10cSrcweir (com.sun.star.beans.XPropertySet) 138*cdf0e10cSrcweir UnoRuntime.queryInterface( 139*cdf0e10cSrcweir com.sun.star.beans.XPropertySet.class, 140*cdf0e10cSrcweir xTextPortion); 141*cdf0e10cSrcweir System.out.println( "Name of the font : " 142*cdf0e10cSrcweir + xPropertySet.getPropertyValue( "CharFontName" ) ); 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir // PropertyState status of each text portion. 145*cdf0e10cSrcweir com.sun.star.beans.XPropertyState xPropertyState = 146*cdf0e10cSrcweir (com.sun.star.beans.XPropertyState) 147*cdf0e10cSrcweir UnoRuntime.queryInterface( 148*cdf0e10cSrcweir com.sun.star.beans.XPropertyState.class, 149*cdf0e10cSrcweir xTextPortion); 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir if( xPropertyState.getPropertyState("CharWeight").equals( 152*cdf0e10cSrcweir com.sun.star.beans.PropertyState.AMBIGUOUS_VALUE) ) 153*cdf0e10cSrcweir System.out.println( "- The text range contains more than one different attributes" ); 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir if( xPropertyState.getPropertyState( "CharWeight" ).equals( 156*cdf0e10cSrcweir com.sun.star.beans.PropertyState.DIRECT_VALUE ) ) 157*cdf0e10cSrcweir System.out.println( " - The text range contains hard formats" ); 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir if( xPropertyState.getPropertyState( "CharWeight" ).equals( 160*cdf0e10cSrcweir com.sun.star.beans.PropertyState.DEFAULT_VALUE ) ) 161*cdf0e10cSrcweir System.out.println( " - The text range doesn't contains hard formats" ); 162*cdf0e10cSrcweir } 163*cdf0e10cSrcweir } 164*cdf0e10cSrcweir else 165*cdf0e10cSrcweir System.out.println( "The text portion isn't a text paragraph" ); 166*cdf0e10cSrcweir // End section 'The structure of text documents' of the Tutorial 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir catch( Exception e) { 170*cdf0e10cSrcweir e.printStackTrace(System.err); 171*cdf0e10cSrcweir System.exit(1); 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir System.out.println("done"); 175*cdf0e10cSrcweir System.exit(0); 176*cdf0e10cSrcweir } 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir public static void createExampleData( com.sun.star.text.XText xText ) { 179*cdf0e10cSrcweir 180*cdf0e10cSrcweir try { 181*cdf0e10cSrcweir xText.setString( "This is an example sentence" ); 182*cdf0e10cSrcweir 183*cdf0e10cSrcweir com.sun.star.text.XWordCursor xWordCursor = 184*cdf0e10cSrcweir (com.sun.star.text.XWordCursor)UnoRuntime.queryInterface( 185*cdf0e10cSrcweir com.sun.star.text.XWordCursor.class, xText.getStart()); 186*cdf0e10cSrcweir 187*cdf0e10cSrcweir xWordCursor.gotoNextWord(false); 188*cdf0e10cSrcweir xWordCursor.gotoNextWord(false); 189*cdf0e10cSrcweir xWordCursor.gotoEndOfWord(true); 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir com.sun.star.beans.XPropertySet xPropertySet = 192*cdf0e10cSrcweir (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface( 193*cdf0e10cSrcweir com.sun.star.beans.XPropertySet.class, xWordCursor ); 194*cdf0e10cSrcweir xPropertySet.setPropertyValue("CharWeight", 195*cdf0e10cSrcweir new Float( com.sun.star.awt.FontWeight.BOLD )); 196*cdf0e10cSrcweir 197*cdf0e10cSrcweir System.out.println("create example data"); 198*cdf0e10cSrcweir } 199*cdf0e10cSrcweir catch( Exception e) { 200*cdf0e10cSrcweir e.printStackTrace(System.err); 201*cdf0e10cSrcweir } 202*cdf0e10cSrcweir 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir } 205*cdf0e10cSrcweir } 206