1*34dd1e25SAndrew Rist /************************************************************** 2*34dd1e25SAndrew Rist * 3*34dd1e25SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*34dd1e25SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*34dd1e25SAndrew Rist * distributed with this work for additional information 6*34dd1e25SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*34dd1e25SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*34dd1e25SAndrew Rist * "License"); you may not use this file except in compliance 9*34dd1e25SAndrew Rist * with the License. You may obtain a copy of the License at 10*34dd1e25SAndrew Rist * 11*34dd1e25SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*34dd1e25SAndrew Rist * 13*34dd1e25SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*34dd1e25SAndrew Rist * software distributed under the License is distributed on an 15*34dd1e25SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*34dd1e25SAndrew Rist * KIND, either express or implied. See the License for the 17*34dd1e25SAndrew Rist * specific language governing permissions and limitations 18*34dd1e25SAndrew Rist * under the License. 19*34dd1e25SAndrew Rist * 20*34dd1e25SAndrew Rist *************************************************************/ 21*34dd1e25SAndrew Rist 22*34dd1e25SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir //*************************************************************************** 25cdf0e10cSrcweir // comment: Step 1: bootstrap UNO and get the remote component context 26cdf0e10cSrcweir // Step 2: open an empty text document 27cdf0e10cSrcweir // Step 3: create an enumeration of all paragraphs 28cdf0e10cSrcweir // Step 4: create an enumeration of all text portions 29cdf0e10cSrcweir //*************************************************************************** 30cdf0e10cSrcweir 31cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 32cdf0e10cSrcweir 33cdf0e10cSrcweir public class TextDocumentStructure { 34cdf0e10cSrcweir main(String args[])35cdf0e10cSrcweir public static void main(String args[]) { 36cdf0e10cSrcweir com.sun.star.uno.XComponentContext xContext = null; 37cdf0e10cSrcweir 38cdf0e10cSrcweir try { 39cdf0e10cSrcweir // get the remote office component context 40cdf0e10cSrcweir xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 41cdf0e10cSrcweir System.out.println("Connected to a running office ..."); 42cdf0e10cSrcweir 43cdf0e10cSrcweir // get the rmeote service manger 44cdf0e10cSrcweir com.sun.star.lang.XMultiComponentFactory xMCF = 45cdf0e10cSrcweir xContext.getServiceManager(); 46cdf0e10cSrcweir 47cdf0e10cSrcweir // create a new instance of the desktop 48cdf0e10cSrcweir Object oDesktop = xMCF.createInstanceWithContext( 49cdf0e10cSrcweir "com.sun.star.frame.Desktop", xContext); 50cdf0e10cSrcweir 51cdf0e10cSrcweir // get the component laoder from the desktop to create a new 52cdf0e10cSrcweir // text document 53cdf0e10cSrcweir com.sun.star.frame.XComponentLoader xCLoader = 54cdf0e10cSrcweir (com.sun.star.frame.XComponentLoader) 55cdf0e10cSrcweir UnoRuntime.queryInterface( 56cdf0e10cSrcweir com.sun.star.frame.XComponentLoader.class,oDesktop); 57cdf0e10cSrcweir com.sun.star.beans.PropertyValue [] szEmptyArgs = 58cdf0e10cSrcweir new com.sun.star.beans.PropertyValue [0]; 59cdf0e10cSrcweir String strDoc = "private:factory/swriter"; 60cdf0e10cSrcweir 61cdf0e10cSrcweir System.out.println("create new text document"); 62cdf0e10cSrcweir 63cdf0e10cSrcweir com.sun.star.lang.XComponent xComp = xCLoader.loadComponentFromURL( 64cdf0e10cSrcweir strDoc, "_blank", 0, szEmptyArgs); 65cdf0e10cSrcweir 66cdf0e10cSrcweir // query the new document for the XTextDocument interface 67cdf0e10cSrcweir com.sun.star.text.XTextDocument xTextDocument = 68cdf0e10cSrcweir (com.sun.star.text.XTextDocument)UnoRuntime.queryInterface( 69cdf0e10cSrcweir com.sun.star.text.XTextDocument.class, xComp); 70cdf0e10cSrcweir 71cdf0e10cSrcweir // create some example data 72cdf0e10cSrcweir com.sun.star.text.XText xText = xTextDocument.getText(); 73cdf0e10cSrcweir createExampleData( xText ); 74cdf0e10cSrcweir 75cdf0e10cSrcweir // Begin section 'The structure of text documents' of the Tutorial 76cdf0e10cSrcweir 77cdf0e10cSrcweir com.sun.star.container.XEnumeration xParagraphEnumeration = null; 78cdf0e10cSrcweir com.sun.star.container.XEnumerationAccess xParaEnumerationAccess = null; 79cdf0e10cSrcweir com.sun.star.container.XEnumeration xPortionEnumeration = null; 80cdf0e10cSrcweir com.sun.star.container.XEnumeration xTextPortionEnum; 81cdf0e10cSrcweir com.sun.star.text.XTextContent xTextElement = null; 82cdf0e10cSrcweir 83cdf0e10cSrcweir System.out.println("create an enumeration of all paragraphs"); 84cdf0e10cSrcweir // create an enumeration access of all paragraphs of a document 85cdf0e10cSrcweir com.sun.star.container.XEnumerationAccess xEnumerationAccess = 86cdf0e10cSrcweir (com.sun.star.container.XEnumerationAccess) 87cdf0e10cSrcweir UnoRuntime.queryInterface( 88cdf0e10cSrcweir com.sun.star.container.XEnumerationAccess.class, xText); 89cdf0e10cSrcweir xParagraphEnumeration = xEnumerationAccess.createEnumeration(); 90cdf0e10cSrcweir 91cdf0e10cSrcweir // Loop through all paragraphs of the document 92cdf0e10cSrcweir while ( xParagraphEnumeration.hasMoreElements() ) { 93cdf0e10cSrcweir xTextElement = (com.sun.star.text.XTextContent) 94cdf0e10cSrcweir UnoRuntime.queryInterface( 95cdf0e10cSrcweir com.sun.star.text.XTextContent.class, 96cdf0e10cSrcweir xParagraphEnumeration.nextElement()); 97cdf0e10cSrcweir com.sun.star.lang.XServiceInfo xServiceInfo = 98cdf0e10cSrcweir (com.sun.star.lang.XServiceInfo)UnoRuntime.queryInterface( 99cdf0e10cSrcweir com.sun.star.lang.XServiceInfo.class, xTextElement); 100cdf0e10cSrcweir 101cdf0e10cSrcweir // check ifs the current paragraph really a paragraph or an 102cdf0e10cSrcweir // anchor of a frame or picture 103cdf0e10cSrcweir if( xServiceInfo.supportsService("com.sun.star.text.Paragraph") ) { 104cdf0e10cSrcweir com.sun.star.text.XTextRange xTextRange = 105cdf0e10cSrcweir xTextElement.getAnchor(); 106cdf0e10cSrcweir System.out.println( "This is a Paragraph" ); 107cdf0e10cSrcweir 108cdf0e10cSrcweir // create another enumeration to get all text portions of 109cdf0e10cSrcweir // the paragraph 110cdf0e10cSrcweir xParaEnumerationAccess = 111cdf0e10cSrcweir (com.sun.star.container.XEnumerationAccess) 112cdf0e10cSrcweir UnoRuntime.queryInterface( 113cdf0e10cSrcweir com.sun.star.container.XEnumerationAccess.class, 114cdf0e10cSrcweir xTextElement); 115cdf0e10cSrcweir xTextPortionEnum = xParaEnumerationAccess.createEnumeration(); 116cdf0e10cSrcweir 117cdf0e10cSrcweir while ( xTextPortionEnum.hasMoreElements() ) { 118cdf0e10cSrcweir com.sun.star.text.XTextRange xTextPortion = 119cdf0e10cSrcweir (com.sun.star.text.XTextRange)UnoRuntime.queryInterface( 120cdf0e10cSrcweir com.sun.star.text.XTextRange.class, 121cdf0e10cSrcweir xTextPortionEnum.nextElement()); 122cdf0e10cSrcweir System.out.println( "Text from the portion : " 123cdf0e10cSrcweir + xTextPortion.getString() ); 124cdf0e10cSrcweir 125cdf0e10cSrcweir com.sun.star.beans.XPropertySet xPropertySet = 126cdf0e10cSrcweir (com.sun.star.beans.XPropertySet) 127cdf0e10cSrcweir UnoRuntime.queryInterface( 128cdf0e10cSrcweir com.sun.star.beans.XPropertySet.class, 129cdf0e10cSrcweir xTextPortion); 130cdf0e10cSrcweir System.out.println( "Name of the font : " 131cdf0e10cSrcweir + xPropertySet.getPropertyValue( "CharFontName" ) ); 132cdf0e10cSrcweir 133cdf0e10cSrcweir // PropertyState status of each text portion. 134cdf0e10cSrcweir com.sun.star.beans.XPropertyState xPropertyState = 135cdf0e10cSrcweir (com.sun.star.beans.XPropertyState) 136cdf0e10cSrcweir UnoRuntime.queryInterface( 137cdf0e10cSrcweir com.sun.star.beans.XPropertyState.class, 138cdf0e10cSrcweir xTextPortion); 139cdf0e10cSrcweir 140cdf0e10cSrcweir if( xPropertyState.getPropertyState("CharWeight").equals( 141cdf0e10cSrcweir com.sun.star.beans.PropertyState.AMBIGUOUS_VALUE) ) 142cdf0e10cSrcweir System.out.println( "- The text range contains more than one different attributes" ); 143cdf0e10cSrcweir 144cdf0e10cSrcweir if( xPropertyState.getPropertyState( "CharWeight" ).equals( 145cdf0e10cSrcweir com.sun.star.beans.PropertyState.DIRECT_VALUE ) ) 146cdf0e10cSrcweir System.out.println( " - The text range contains hard formats" ); 147cdf0e10cSrcweir 148cdf0e10cSrcweir if( xPropertyState.getPropertyState( "CharWeight" ).equals( 149cdf0e10cSrcweir com.sun.star.beans.PropertyState.DEFAULT_VALUE ) ) 150cdf0e10cSrcweir System.out.println( " - The text range doesn't contains hard formats" ); 151cdf0e10cSrcweir } 152cdf0e10cSrcweir } 153cdf0e10cSrcweir else 154cdf0e10cSrcweir System.out.println( "The text portion isn't a text paragraph" ); 155cdf0e10cSrcweir // End section 'The structure of text documents' of the Tutorial 156cdf0e10cSrcweir } 157cdf0e10cSrcweir } 158cdf0e10cSrcweir catch( Exception e) { 159cdf0e10cSrcweir e.printStackTrace(System.err); 160cdf0e10cSrcweir System.exit(1); 161cdf0e10cSrcweir } 162cdf0e10cSrcweir 163cdf0e10cSrcweir System.out.println("done"); 164cdf0e10cSrcweir System.exit(0); 165cdf0e10cSrcweir } 166cdf0e10cSrcweir createExampleData( com.sun.star.text.XText xText )167cdf0e10cSrcweir public static void createExampleData( com.sun.star.text.XText xText ) { 168cdf0e10cSrcweir 169cdf0e10cSrcweir try { 170cdf0e10cSrcweir xText.setString( "This is an example sentence" ); 171cdf0e10cSrcweir 172cdf0e10cSrcweir com.sun.star.text.XWordCursor xWordCursor = 173cdf0e10cSrcweir (com.sun.star.text.XWordCursor)UnoRuntime.queryInterface( 174cdf0e10cSrcweir com.sun.star.text.XWordCursor.class, xText.getStart()); 175cdf0e10cSrcweir 176cdf0e10cSrcweir xWordCursor.gotoNextWord(false); 177cdf0e10cSrcweir xWordCursor.gotoNextWord(false); 178cdf0e10cSrcweir xWordCursor.gotoEndOfWord(true); 179cdf0e10cSrcweir 180cdf0e10cSrcweir com.sun.star.beans.XPropertySet xPropertySet = 181cdf0e10cSrcweir (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface( 182cdf0e10cSrcweir com.sun.star.beans.XPropertySet.class, xWordCursor ); 183cdf0e10cSrcweir xPropertySet.setPropertyValue("CharWeight", 184cdf0e10cSrcweir new Float( com.sun.star.awt.FontWeight.BOLD )); 185cdf0e10cSrcweir 186cdf0e10cSrcweir System.out.println("create example data"); 187cdf0e10cSrcweir } 188cdf0e10cSrcweir catch( Exception e) { 189cdf0e10cSrcweir e.printStackTrace(System.err); 190cdf0e10cSrcweir } 191cdf0e10cSrcweir 192cdf0e10cSrcweir 193cdf0e10cSrcweir } 194cdf0e10cSrcweir } 195