This is the second of a total of four postings with the intention to demonstrate how to realize the same functionality of the posted OLE samples without OLE and in a portable way (running unchanged on Windows, Linux and Apple).

These are samples in the ooRexx scripting language, which usually can be easily adapted to other languages by replacing the tilde (~), the ooRexx message operator, with a dot (.).

Also, these solutions will use queryInterface() such that one can see for other programming languages that need to employ queryInterface() what the interface names are. The ooRexx solution (actually the ooRexx-Java bridge BSF4ooRexx) takes advantage of the available message paradigm and allows one to merely send the (unqualified) interface name to an UNO object (instead of coding the entire queryInterface() statement). The fully qualified interface name can always be looked up quickly from the AOO index for the letter "X": <https://www.openoffice.org/api/docs/common/ref/index-files/index-24.html>.

Here the portable, OLE-less solution as a follow-up to the matching posting 
(see underneath):

   /**********************************************************************
     swriter_paragraphs.rxo: using UNO.CLS (i.e. Java UNO under the hood) with 
ooRexx

     Links:<https://OpenOffice.org>
             
<https://wiki.openoffice.org/wiki/Documentation/DevGuide/ProUNO/Bridge/Automation_Bridge>
             <https://www.pitonyak.org/oo.php>
             <https://www.openoffice.org/udk/common/man/spec/ole_bridge.html>

     Using UNO.CLS create a new swriter document, add paragraphs that get 
aligned
     in four different ways. Demonstrates how to incorporate UNO_CONSTANTS and
     UNO_ENUM values into a Rexx directory-like collection for easier use.
   ***********************************************************************/

   /* create a text document, demonstrate how to align paragraphs */
   xDesktop=uno.createDesktop()        -- bootstrap & get access to XDesktop
   xcl=xDesktop~XComponentLoader       -- get XComponentLoader interface

   uri="private:factory/swriter"       -- new swriter document
   doc=xcl~loadComponentFromURL(uri,"_blank",0,.uno~noProps)

   xText=doc~XTextDocument~getText     -- get text object
   xText~setString("Hello, this is ooRexx on:" .DateTime~new"!")

   xTextCursor=xText~createTextCursor  -- create the character based cursor
       -- make paragraph's properties accessible:
   xParaProps=xTextCursor~XParagraphCursor~XPropertySet

   ctlChars=.uno_constants~new("com.sun.star.text.ControlCharacter") -- 
UNO_CONSTANT
   paraBreak=ctlChars~paragraph_break  -- get paragraph break constant

   paraAdj =.uno_enum~new("com.sun.star.style.ParagraphAdjust")   -- UNO_ENUM

   arr=.array~of("right", "center", "block", "left")  -- adjustments
   do adj over arr   -- iterate over adjustments, create string, adjust
       xTextCursor~gotoEnd(.false)      -- position at end
       xText~insertControlCharacter(xTextCursor, paraBreak, .false)
       string=("This paragraph will be" adj"-adjusted. ")~copies(8)
       xText~insertString(xTextCursor, string, .true)
       xParaProps~setPropertyValue("ParaAdjust", paraAdj~send(adj))
   end

   ::requires UNO.CLS   -- get UNO support

If there are any questions, please ask them.

---rony


On 24.06.2022 13:04, Rony G. Flatscher wrote:
This ooRexx program creates a swriter document which creates paragraphs that get right-, center-, block- and left-adjusted. There is a routine that allows to fetch constant and enum values in an ooRexx directory, such that sending the name of a constant or enum value will return the value one has to use as an argument.

/**********************************************************************
     AOO_swriter_paragraphs.rex using OLE (object linking and embedding) with 
ooRexx

     Links: <https://OpenOffice.org>
<https://wiki.openoffice.org/wiki/Documentation/DevGuide/ProUNO/Bridge/Automation_Bridge>
<https://www.pitonyak.org/oo.php>
<https://www.openoffice.org/udk/common/man/spec/ole_bridge.html>

     Using OLE create a new swriter document, add paragraphs that get aligned
     in four different ways. Demonstrates how to use UNO reflection and
     incorporate UNO_CONSTANTS and UNO_ENUM values into a Rexx directory for
     easier use.
***********************************************************************/

   /* create a text document, demonstrate how to align paragraphs */
   serviceManager = .OLEObject~new('com.sun.star.ServiceManager')
   /* create text document */
   desktop  = serviceManager~createInstance('com.sun.star.frame.Desktop')
   noProps  = .array~new   /* empty array (no properties)   */
   document = desktop~loadComponentFromURL('private:factory/swriter', '_blank', 
0, noProps)

   text = document~getText                -- get text object
   text~setString("Hello, this is ooRexx on:" .DateTime~new"!")
   cursor = text~createTextCursor

   ctlChars = getAsDirectory(serviceManager, 
"com.sun.star.text.ControlCharacter")  -- UNO_CONSTANT
   paraBreak = ctlChars~paragraph_break   -- get paragraph break constant

   paraAdj = getAsDirectory(serviceManager, 
"com.sun.star.style.ParagraphAdjust")   -- UNO_ENUM

   arr = .array~of("right", "center", "block", "left")   -- adjustments
   do adj over arr   -- iterate over adjustments, create string, adjust
       cursor~gotoEnd(.false)     -- position at end
       text~insertControlCharacter(cursor, paraBreak, .false)
       string = ("This paragraph will be" adj"-adjusted. ")~copies(8)
       text~insertString(cursor, string, .true)
       -- fetch appropriate adjust enum value from directory
       cursor~setPropertyValue("ParaAdjust", paraAdj~send(adj))
   end


   /* Routine returns a Rexx directory containing all names and values of the 
supplied
       UNO_CONSTANTS or UNO_ENUM class name (needs to be fully qualified).  */
   ::routine getAsDirectory
      use strict arg serviceManager, unoClzName

      dir = .Directory~new              -- directory will get
      dir~objectName = unoClzName       -- allows to show the uno class it 
represents

      ctxt = serviceManager~defaultContext
      tdm = 
ctxt~getValueByName("/singletons/com.sun.star.reflection.theTypeDescriptionManager")
      reflClz= tdm~getByHierarchicalName(unoClzName)
      if reflClz~isNil then return dir  -- return empty directory

      typeClass = reflClz~getTypeClass
      if typeClass = 30 then         -- UNO_CONSTANTS
      do
         dir~objectName = unoClzName "(UNO_CONSTANTS)" -- supply type info to 
name
         do c over reflClz~getConstants -- iterate over constant fields
            name = c~getName            -- fully qualified
            name = name~substr(name~lastPos('.')+1) -- extract last word
            dir[name] = c~getConstantValue -- store constant values with their 
names
            -- say "name:" name "->" c~getConstantValue
         end
      end
      else if typeClass = 15 then    -- UNO_ENUMERATION
      do
         dir~objectName = unoClzName "(UNO_ENUM)"   -- supply type info to name
         enumNames = reflClz~getEnumNames     -- get all enumeration names
         enumValues = reflClz~getEnumValues   -- get all enumeration values
         do i=1 to enumNames~items
            name = enumNames[i]
            name = name~substr(name~lastPos('.')+1) -- extract last word
            dir[name] = enumValues[i]   -- store enum values with their names
            -- say "name:" name "->" enumValues[i]
         end
      end
      return dir

HTH

---rony

P.S.: The short paper at <https://epub.wu.ac.at/8118/> introduces ooRexx briefly in ten pages, home of Rexx based technologies is the non-profit SIG "Rexx Language Association" at <https://www.RexxLA.org>.

Reply via email to