KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > binding > model > IncludeElement


1 /*
2 Copyright (c) 2004-2005, Dennis M. Sosnoski
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8  * Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13  * Neither the name of JiBX nor the names of its contributors may be used
14    to endorse or promote products derived from this software without specific
15    prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */

28
29 package org.jibx.binding.model;
30
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.net.URL JavaDoc;
34
35 import org.jibx.binding.util.StringArray;
36 import org.jibx.runtime.IUnmarshallingContext;
37 import org.jibx.runtime.JiBXException;
38
39 /**
40  * Model component for <b>include</b> element of binding definition. During
41  * prevalidation this reads the included binding definition. All further
42  * processing of the included components needs to be handled directly by the
43  * tree walking code in {@link org.jibx.binding.model.TreeContext}, since the
44  * components of the included binding need to be treated as though they were
45  * direct children of the container of this element (and accessed in the
46  * appropriate order).
47  *
48  * @author Dennis M. Sosnoski
49  * @version 1.0
50  */

51  
52 public class IncludeElement extends NestingElementBase
53 {
54     /** Enumeration of allowed attribute names */
55     public static final StringArray s_allowedAttributes =
56         new StringArray(new String JavaDoc[] { "path" });
57     
58     /** Path to included binding definition. */
59     private String JavaDoc m_includePath;
60     
61     /** Object model for included binding. */
62     private BindingElement m_binding;
63     
64     /**
65      * Constructor.
66      */

67     public IncludeElement() {
68         super(INCLUDE_ELEMENT);
69     }
70     
71     /**
72      * Set path to included binding.
73      *
74      * @param path
75      */

76     public void setIncludePath(String JavaDoc path) {
77         m_includePath = path;
78     }
79     
80     /**
81      * Get path to included binding.
82      *
83      * @return
84      */

85     public String JavaDoc getIncludePath() {
86         return m_includePath;
87     }
88     
89     /**
90      * Get the included binding model. This call is only valid after
91      * prevalidation.
92      *
93      * @return binding element, or <code>null</code> if redundant include
94      */

95     public BindingElement getBinding() {
96         return m_binding;
97     }
98     
99     //
100
// Validation methods
101

102     /**
103      * Make sure all attributes are defined.
104      *
105      * @param uctx unmarshalling context
106      * @exception JiBXException on unmarshalling error
107      */

108     private void preSet(IUnmarshallingContext uctx) throws JiBXException {
109         validateAttributes(uctx, s_allowedAttributes);
110     }
111     
112     /* (non-Javadoc)
113      * @see org.jibx.binding.model.ElementBase#prevalidate(org.jibx.binding.model.ValidationContext)
114      */

115     public void prevalidate(ValidationContext vctx) {
116         if (m_includePath == null) {
117             vctx.addFatal("No include path specified");
118         } else {
119             try {
120                 
121                 // access the included binding as input stream
122
BindingElement root = vctx.getBindingRoot();
123                 URL JavaDoc base = root.getBaseUrl();
124                 URL JavaDoc url = new URL JavaDoc(base, m_includePath);
125                 String JavaDoc path = url.toExternalForm();
126                 if (root.addIncludePath(path)) {
127                     
128                     // get base name from path
129
int split = path.lastIndexOf('/');
130                     String JavaDoc fname = path;
131                     if (split >= 0) {
132                         fname = fname.substring(split+1);
133                     }
134                     
135                     // read stream to create object model
136
InputStream JavaDoc is = url.openStream();
137                     m_binding = BindingElement.readBinding(is, fname, vctx);
138                     m_binding.setBaseUrl(url);
139                 }
140                 
141             } catch (JiBXException e) {
142                 vctx.addFatal(e.getMessage());
143             } catch (IOException JavaDoc e) {
144                 vctx.addFatal("Error accessing included binding with path \"" +
145                     m_includePath + "\": " + e.getMessage());
146             }
147         }
148     }
149 }
Popular Tags