Hi,

After some coding and researches I have found that following code works
and adds UserDefinedAttributes to a Paragraph or a Text.

But "TextDefinedAttributes" is missing from the documentation (IDL
Reference). Why this? What is the service that has this Property?

This is the code:

********************************************************************************

public class OoHelloWorld {


    XMultiComponentFactory xMCF;
    XComponentContext xContext;
    String
nameSpace="urn:ooo:names:tc:opendocument:xmlns:semantic-text:1.0";

    public OoHelloWorld() {
        try {


            xContext = Bootstrap.bootstrap();
            System.out.println("Connessione Avviata...");

            // Ottengo il Service Manager
            xMCF = xContext.getServiceManager();

        } catch (BootstrapException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {

    OoHelloWorld oW=new OoHelloWorld();

        oW.mainTest();

        System.out.println("Uscita!");
        System.exit(0);
    }


    private void mainTest() {
        if (xMCF!=null) {
            System.out.println("Avviato!");
            try {

                XComponent xComponent = openOoWriter(xContext, xMCF);

                //Interfaccia documento di testo
                XTextDocument xTextDocument =
(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, xComponent);

                //Interfaccia del Testo
                XText xText = xTextDocument.getText();

                // Cursore
                XTextCursor xTextCursor = xText.createTextCursor();
                XPropertySet xCursorProps = (XPropertySet)
UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);

                // Inserisco il primo con i relativi paragrafi

                xCursorProps.setPropertyValue("ParaStyleName", "Heading 1");
                xText.insertString( xTextCursor, "1363", false );
                xText.insertControlCharacter(xTextCursor,
ControlCharacter.PARAGRAPH_BREAK, false);

                xText.insertString( xTextCursor, "This is the first
Paragraph", false );
                xText.insertControlCharacter(xTextCursor,
ControlCharacter.PARAGRAPH_BREAK, false);

addAttributeToParagraph(xText,2,2,"semantic-text:id_emen","22222");

                xText.insertString( xTextCursor, "This is the second
Paragraph", false );
                xText.insertControlCharacter(xTextCursor,
ControlCharacter.PARAGRAPH_BREAK, false);

addAttributeToParagraph(xText,3,3,"semantic-text:id_emen","333333");


addAttributeToParagraph(xText,1,3,"semantic-text:ddl","1363");

                // Inserisco il secondo con i relativi paragrafi

                xCursorProps.setPropertyValue("ParaStyleName", "Heading 1");
                xText.insertString( xTextCursor, "1364", false );
                xText.insertControlCharacter(xTextCursor,
ControlCharacter.PARAGRAPH_BREAK, false);
                xText.insertString( xTextCursor, "This is the first
Paragraph", false );
                addAttributeToText(xTextCursor,"semantic-text:text","N/A");

                xText.insertControlCharacter(xTextCursor,
ControlCharacter.PARAGRAPH_BREAK, false);

addAttributeToParagraph(xText,5,5,"semantic-text:id_emen","444444");


addAttributeToParagraph(xText,4,5,"semantic-text:ddl","1364");

            } catch ( com.sun.star.uno.Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        }
        else
            System.out.println("Service Manager non disponibile!");
    }


    private void addAttributeToParagraph(XText xText,
                                        int startParagraph,
                                        int endParagraph,
                                        String XMLAttrName,
                                        String XMLAttrVal)
    throws UnknownPropertyException, PropertyVetoException,
IllegalArgumentException, WrappedTargetException {

        XEnumerationAccess xParaAccess = (XEnumerationAccess)
UnoRuntime.queryInterface(XEnumerationAccess.class, xText);
        XEnumeration xParaEnum = xParaAccess.createEnumeration();

        int contParagraph=1;
        while (xParaEnum.hasMoreElements()) {

            XServiceInfo xInfo;
            try {
                xInfo = (XServiceInfo)
UnoRuntime.queryInterface(XServiceInfo.class, xParaEnum.nextElement());

                if (contParagraph>=startParagraph &&
                    contParagraph<=endParagraph    ) {

                        XPropertySet xSet = (XPropertySet)
UnoRuntime.queryInterface(XPropertySet.class, xInfo);
                        // Creo il nuovo attributo XML
                        System.out.println("Aggiungo l'attributo al
"+contParagraph +"° paragrafo.");
                        XNameContainer uda = createMyAttribute(xSet,
                                                "ParaUserDefinedAttributes",
                                                "CDATA",
                                                XMLAttrName,
                                                XMLAttrVal);


                        // lo aggiungo alla proprietà del paragrafo

xSet.setPropertyValue("ParaUserDefinedAttributes", uda);
                }
            } catch (NoSuchElementException e1) {
                System.out.println("Attenzione non vi sono paragrafi!");
                e1.printStackTrace();
                return;
            } catch (WrappedTargetException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                return;
            }

            contParagraph++;
        }
    }

    private void addAttributeToText(XTextCursor xTextCursor,
                                String XMLAttrName,
                                String XMLAttrVal) throws
UnknownPropertyException, PropertyVetoException,
IllegalArgumentException, WrappedTargetException {

        XPropertySet xCursorProps = (XPropertySet)
UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);

        // Creo il nuovo attributo XML

        XNameContainer uda = createMyAttribute(xCursorProps,
                                        "TextUserDefinedAttributes",
                                        "CDATA",
                                        XMLAttrName,
                                        XMLAttrVal);


        // lo aggiungo alla proprietà del paragrafo
        xCursorProps.setPropertyValue("TextUserDefinedAttributes", uda);
    }

    private XNameContainer createMyAttribute(XPropertySet xSet,
                                            String propertyName,
                                            String XMLAttrType,
                                            String XMLAttrName,
                                            String XMLAttrValue) throws
UnknownPropertyException, WrappedTargetException {
        AttributeData attr = new AttributeData();
        attr.Namespace = nameSpace;
        attr.Type = XMLAttrType;
        attr.Value = XMLAttrValue;

        XNameContainer uda = null;
        try{
            uda = (XNameContainer) AnyConverter.toObject(
                      new Type(XNameContainer.class),
                      xSet.getPropertyValue(propertyName));
            uda.insertByName(XMLAttrName, attr);

        } catch (com.sun.star.lang.IllegalArgumentException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ElementExistException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return uda;
    }


    private XComponent openOoWriter(XComponentContext xContext,
XMultiComponentFactory xMCF) throws Exception, IOException,
IllegalArgumentException {
        // Servizio Desktop
        Object oDesktop =
xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext);

        //System.out.println(oDesktop.getClass().getName());

        // Interfaccia xDesktop
        XDesktop xDesktop = (com.sun.star.frame.XDesktop)
UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class, oDesktop);

        PropertyValue xEmptyArgs[] = new PropertyValue[0];

        // Interfaccia Loader dei Componenti
        XComponentLoader xComponentLoader = (XComponentLoader)
UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class,
xDesktop);

        // Interfaccia per il Componente sWriter
        XComponent  xComponent  =
xComponentLoader.loadComponentFromURL("private:factory/swriter",
"_blank", 0, xEmptyArgs);
        return xComponent;
    }


}

********************************************************************************

Bye

Roberto

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to