1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  *   Licensed to the Apache Software Foundation (ASF) under one or more
11  *   contributor license agreements. See the NOTICE file distributed
12  *   with this work for additional information regarding copyright
13  *   ownership. The ASF licenses this file to you under the Apache
14  *   License, Version 2.0 (the "License"); you may not use this file
15  *   except in compliance with the License. You may obtain a copy of
16  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 
19 package org.openoffice.xforms;
20 
21 import com.sun.star.container.NoSuchElementException;
22 import com.sun.star.container.XNameContainer;
23 import com.sun.star.lang.WrappedTargetException;
24 import com.sun.star.lang.XComponent;
25 import com.sun.star.lang.XMultiServiceFactory;
26 import com.sun.star.uno.Exception;
27 import com.sun.star.uno.UnoRuntime;
28 import com.sun.star.xforms.XFormsSupplier;
29 import com.sun.star.xforms.XFormsUIHelper1;
30 import com.sun.star.xforms.XModel;
31 import integration.forms.DocumentType;
32 
33 public class XMLDocument extends integration.forms.DocumentHelper
34 {
35     private XNameContainer  m_forms;
36 
37     /* ------------------------------------------------------------------ */
XMLDocument( XMultiServiceFactory _orb )38     public XMLDocument( XMultiServiceFactory _orb ) throws Exception
39     {
40         super( _orb, implLoadAsComponent( _orb, getDocumentFactoryURL( DocumentType.XMLFORM ) ) );
41         impl_initialize( getDocument() );
42     }
43 
44     /* ------------------------------------------------------------------ */
impl_initialize( XComponent _document )45     private void impl_initialize( XComponent _document )
46     {
47         XFormsSupplier  formsSupplier = UnoRuntime.queryInterface( XFormsSupplier.class,
48             _document );
49 
50         if ( formsSupplier == null )
51             throw new IllegalArgumentException();
52 
53         m_forms = formsSupplier.getXForms();
54     }
55 
56     /* ------------------------------------------------------------------ */
getXFormModelNames()57     public String[] getXFormModelNames()
58     {
59         return m_forms.getElementNames();
60     }
61 
62     /* ------------------------------------------------------------------ */
getXFormModel( String _modelName )63     public Model getXFormModel( String _modelName ) throws NoSuchElementException
64     {
65         try
66         {
67             return new Model(m_forms.getByName(_modelName));
68         }
69         catch (WrappedTargetException ex)
70         {
71             throw new NoSuchElementException(ex);
72         }
73     }
74 
75     /* ------------------------------------------------------------------ */
addXFormModel( String _modelName )76     public Model addXFormModel( String _modelName )
77     {
78         XModel newModel = null;
79         try
80         {
81             newModel = UnoRuntime.queryInterface( XModel.class,
82                 getOrb().createInstance( "com.sun.star.xforms.Model" ) );
83             newModel.setID(_modelName);
84             XFormsUIHelper1 modelHelper = UnoRuntime.queryInterface(
85                 XFormsUIHelper1.class, newModel );
86             modelHelper.newInstance( "Instance 1", "", true );
87             newModel.initialize();
88 
89             m_forms.insertByName(_modelName, newModel);
90         }
91         catch (Exception ex)
92         {
93             ex.printStackTrace();
94         }
95         return new Model( newModel );
96     }
97 }
98