1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 /*
23  * To change this template, choose Tools | Templates
24  * and open the template in the editor.
25  */
26 
27 package org.openoffice.xforms;
28 
29 import com.sun.star.container.NoSuchElementException;
30 import com.sun.star.container.XNameContainer;
31 import com.sun.star.lang.WrappedTargetException;
32 import com.sun.star.lang.XComponent;
33 import com.sun.star.lang.XMultiServiceFactory;
34 import com.sun.star.uno.Exception;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.xforms.XFormsSupplier;
37 import com.sun.star.xforms.XFormsUIHelper1;
38 import com.sun.star.xforms.XModel;
39 import integration.forms.DocumentType;
40 
41 /**
42  *
43  * @author fs93730
44  */
45 public class XMLDocument extends integration.forms.DocumentHelper
46 {
47     private XFormsSupplier  m_formsSupplier;
48     private XNameContainer  m_forms;
49 
50     /* ------------------------------------------------------------------ */
XMLDocument( XMultiServiceFactory _orb )51     public XMLDocument( XMultiServiceFactory _orb ) throws Exception
52     {
53         super( _orb, implLoadAsComponent( _orb, getDocumentFactoryURL( DocumentType.XMLFORM ) ) );
54         impl_initialize( getDocument() );
55     }
56 
57     /* ------------------------------------------------------------------ */
XMLDocument( XMultiServiceFactory _orb, XComponent _document )58     public XMLDocument( XMultiServiceFactory _orb, XComponent _document )
59     {
60         super( _orb, _document );
61         impl_initialize( _document );
62     }
63 
64     /* ------------------------------------------------------------------ */
impl_initialize( XComponent _document )65     private void impl_initialize( XComponent _document )
66     {
67         m_formsSupplier = (XFormsSupplier)UnoRuntime.queryInterface( XFormsSupplier.class,
68             _document );
69 
70         if ( m_formsSupplier == null )
71             throw new IllegalArgumentException();
72 
73         m_forms = m_formsSupplier.getXForms();
74     }
75 
76     /* ------------------------------------------------------------------ */
getXFormModelNames()77     public String[] getXFormModelNames()
78     {
79         return m_forms.getElementNames();
80     }
81 
82     /* ------------------------------------------------------------------ */
getXFormModel( String _modelName )83     public Model getXFormModel( String _modelName ) throws NoSuchElementException
84     {
85         try
86         {
87             return new Model(m_forms.getByName(_modelName));
88         }
89         catch (WrappedTargetException ex)
90         {
91             throw new NoSuchElementException();
92         }
93     }
94 
95     /* ------------------------------------------------------------------ */
addXFormModel( String _modelName )96     public Model addXFormModel( String _modelName )
97     {
98         XModel newModel = null;
99         try
100         {
101             newModel = (XModel) UnoRuntime.queryInterface( XModel.class,
102                 getOrb().createInstance( "com.sun.star.xforms.Model" ) );
103             newModel.setID(_modelName);
104             XFormsUIHelper1 modelHelper = (XFormsUIHelper1) UnoRuntime.queryInterface(
105                 XFormsUIHelper1.class, newModel );
106             modelHelper.newInstance( "Instance 1", new String(), true );
107             newModel.initialize();
108 
109             m_forms.insertByName(_modelName, newModel);
110         }
111         catch (Exception ex)
112         {
113             ex.printStackTrace();
114         }
115         return new Model( newModel );
116     }
117 }
118