Tuesday, 5 July 2011

Implementing XML Object Model based on XAM/XDM

Overview

As part of the the migration process for the NetBeans Coherence Module I have decided to migrate my Java/XML implementation from JAXB to a XAM/XDM based model because it will be a more appropriate fit for the advanced XML editor functionality and will remove some of the synchronisation issues seen in my initial release of the NetBeans Coherence Plug-in . As part of this development process I decided that it would be good to put together a simple tutorial that will allow others to repeat the process and cement the development strategy within my own mind.

At present their is currently no automated method of generating XAM/XDM Java Interfaces / Implementation classes similar to that provided by JAXB (maybe a future project if I get time) so I will be documenting my approach to the development process. The NetBeans Coherence module provides editors for the three core Coherence XML files "POF Config", Cache Config" and "Coherence Operational" and for this tutorial I will be working with the pof-config XML file. With Coherence 3.7 the Coherence team has provided XSD definitions of the XML files and hence I will be working with these as the source for my implementation.

A future Blog entry will explain how I link the XAM/XDM Model describe here to the existing Advanced POF Editors within the Coherence Netbeans Module.

Identifying the Required Objects

As I mentioned at present their are no automated tools for this hence we will need to review the XSD to identify which elements within it we need to implement. Below we can see the new Coherence 3.7 POF XSD and from this I have identified we will need to create the following Java Interfaces and their corresponding implementations.

  • pof-config - PofConfig & PofConfigImpl
  • user-type-list -UserTypeList & UserTypeListImpl
  • user-type - UserType & UserTypeImpl
  • include - Include & IncludeImpl
  • type-id - TypeId & TypeIdImpl
  • class-name - ClassName & ClassNameImpl
  • serializer-type - SerializerType & SerializerTypeImpl
  • serializer - Serializer & SerializerImpl
  • default-serializer - DefaultSerializer & DefaultSerializerImpl
  • init-params - InitParams & InitParamsImpl
  • init-param - InitParam & InitParamImpl
  • param-type - ParamType & ParamTypeImpl
  • param-value - ParamValue & ParamValueImpl
  • allow-interfaces - AllowInterfaces & AllowInterfacesImpl
  • allow-subclasses - AllowSubclasses & AllowSubclassesImpl
It can be seen from the above list that I have created an Interface for all the Elements and Complex Types. In addition to the Interfaces defined above I will create an additional interface that represent the Sub Elements of  the UserTypeList Complex Element and these will be used during the insert / removal of the Element within the appropriate Impl classes.

  • UserTypeListElement
Although I could have create an Interface to represent all Complex Type / Element they are not strictly required for this particular xsd. If we take for example the pof-config Element it contains a <xsd:sequence> element that defines the Sub Element and more importantly the order in which they can occur. Therefore given that restriction I know that the <user-type-list> must be the first element and that the <allow-interfaces> element must follow, if it exists, whilst the <default-serializer> must be the last. Therefore rather than implement a common addElement method I simply create a series of getters & setters for the Sub Elements.

In addition I created a number of Interfaces and Impl Classes that matched the "Simple Types".

  • xsd:string - PofConfigTextComponent & PofConfigTextComponentImpl
  • xsd:boolean - PofConfigBooleanComponent & PofConfigBooleanComponentImpl
  • xsd:nonNegativeInteger - PofConfigNonNegativeIntegerComponent & PofConfigNonNegativeIntegerComponen
Each of these has getValue and setValue methods that will be used to access the simple data and I extend the Simple elements (allow-subclasses, param-value, etc) from the appropriate Super Class.

coherence-pof-config.xsd

  1 <?xml version="1.0"?>
  2 
  3 
  4 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  5             targetNamespace="http://xmlns.oracle.com/coherence/coherence-pof-config"    
  6             xmlns="http://xmlns.oracle.com/coherence/coherence-pof-config"
  7             elementFormDefault="qualified" attributeFormDefault="unqualified"
  8             version="1.0">
  9 
 10     <xsd:annotation>
 11         <xsd:documentation>
 12             Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
 13 
 14             Oracle is a registered trademarks of Oracle Corporation and/or its
 15             affiliates.
 16 
 17             This software is the confidential and proprietary information of
 18             Oracle Corporation. You shall not disclose such confidential and
 19             proprietary information and shall use it only in accordance with the
 20             terms of the license agreement you entered into with Oracle.
 21 
 22             This notice may not be removed or altered.     
 23         </xsd:documentation>
 24     </xsd:annotation>
 25 
 26     <xsd:annotation>
 27         <xsd:documentation>
 28             This is the XML schema for the Coherence pof configuration file
 29             (coherence-pof-config.xml).
 30         </xsd:documentation>
 31     </xsd:annotation>
 32 
 33     <xsd:element name="pof-config">
 34         <xsd:annotation>
 35             <xsd:documentation>
 36                 The pof-config element is the root element of the pof-config
 37                 descriptor.
 38 
 39                 Used in: n/a
 40             </xsd:documentation>
 41         </xsd:annotation>
 42         <xsd:complexType>
 43             <xsd:sequence>
 44                 <xsd:element ref="user-type-list" />
 45                 <xsd:element ref="allow-interfaces" minOccurs="0" />
 46                 <xsd:element ref="allow-subclasses" minOccurs="0" />
 47                 <xsd:element ref="default-serializer" minOccurs="0" />
 48             </xsd:sequence>
 49         </xsd:complexType>
 50     </xsd:element>
 51 
 52     <xsd:element name="user-type-list">
 53         <xsd:annotation>
 54             <xsd:documentation>
 55                 The user-type-list element contains zero or more user-type elements.
 56                 Each POF user type that will be used must be listed in the 
 57                 user-type-list.
 58 
 59                 The user-type-list element may also contain zero or more include
 60                 elements. Each include element is used to add user-type elements 
 61                 defined in another pof-config file.
 62 
 63                 Used in: pof-config      
 64             </xsd:documentation>
 65         </xsd:annotation>
 66         <xsd:complexType>
 67             <xsd:choice minOccurs="0" maxOccurs="unbounded">
 68                 <xsd:element ref="user-type" />
 69                 <xsd:element ref="include" />
 70             </xsd:choice>
 71         </xsd:complexType>
 72     </xsd:element>
 73 
 74     <xsd:element name="user-type">
 75         <xsd:annotation>
 76             <xsd:documentation>
 77                 The user-type element contains the declaration of a POF user type. A
 78                 POF user type is a uniquely identifiable, portable, versionable 
 79                 object class that can be communicated among systems regardless of 
 80                 language, OS, hardware and location.
 81 
 82                 Within the user-type element, the type-id element is optional, but its use
 83                 is strongly suggested in order to support schema versioning and
 84                 evolution.
 85 
 86                 Within the user-type element, the class-name element is required, and
 87                 specifies the fully qualified name of the Java class or interface
 88                 that all values of the user type are type-assignable to.
 89 
 90                 If the serializer element is omitted, then the user type is assumed to
 91                 implement the PortableObject interface, and the
 92                 PortableObjectSerializer implementation is used as the PofSerializer.
 93 
 94                 Used in: user-type-list
 95             </xsd:documentation>
 96         </xsd:annotation>
 97         <xsd:complexType>
 98             <xsd:sequence>
 99                 <xsd:element ref="type-id" minOccurs="0" />
100                 <xsd:element ref="class-name" />
101                 <xsd:element ref="serializer" minOccurs="0" />
102             </xsd:sequence>
103         </xsd:complexType>
104     </xsd:element>
105 
106     <xsd:element name="include" type="xsd:string">
107         <xsd:annotation>
108             <xsd:documentation>
109                 The include element specifies the location of a pof-config file to load
110                 user-type elements from.
111 
112                 The value is a locator string (either a valid path or URL) that
113                 identifies
114                 the location of the target pof-config file.
115 
116                 Used in: user-type-list  
117             </xsd:documentation>
118         </xsd:annotation>
119     </xsd:element>
120 
121     <xsd:element name="type-id" type="xsd:nonNegativeInteger">
122         <xsd:annotation>
123             <xsd:documentation>
124                 The type-id element specifies an integer value (n >= 0) that uniquely
125                 identifies the user type.
126 
127                 If none of the user-type elements contains a type-id element, then the
128                 type IDs for the user types will be based on the order in which they 
129                 appear in the user-type-list, with the first user type being assigned 
130                 the type ID 0, the second user type being assigned the type ID 1, and so on.
131 
132                 However, it is strongly recommended that user types IDs always be specified,
133                 in order to support schema versioning and evolution.
134 
135                 Used in: user-type     
136             </xsd:documentation>
137         </xsd:annotation>
138     </xsd:element>
139 
140     <xsd:element name="class-name" type="xsd:string">
141         <xsd:annotation>
142             <xsd:documentation>
143                 The class-name element specifies the name of a Java class or interface.
144 
145                 Within the user-type element, the class-name element is required,
146                 and specifies the fully qualified name of the Java class or interface 
147                 that all values of the user type are type-assignable to.
148 
149                 Within the serializer element, the class-name element is required.
150 
151                 Used in: user-type, serializer
152             </xsd:documentation>
153         </xsd:annotation>
154     </xsd:element>
155 
156     <xsd:complexType name="serializer-type">
157         <xsd:sequence>
158             <xsd:element ref="class-name" />
159             <xsd:element ref="init-params" minOccurs="0" />
160         </xsd:sequence>
161     </xsd:complexType>
162 
163     <xsd:element name="serializer" type="serializer-type">
164         <xsd:annotation>
165             <xsd:documentation>
166                 The serializer element specifies what PofSerializer to use to serialize
167                 and deserialize a specific user type.
168 
169                 A PofSerializer is used to serialize and deserialize user type values
170                 to and from a POF stream. Within the serializer element, the class-name
171                 element is required, and zero or more constructor parameters can be 
172                 defined within an init-params element.
173 
174                 If the serializer element is omitted, then the user type is assumed to
175                 implement the PortableObject interface, and the
176                 PortableObjectSerializer
177                 implementation is used as the PofSerializer.
178 
179                 If the init-params element is omitted from the serializer element,
180                 then the
181                 following four constructors are attempted on the specific PofSerializer
182                 implementation, and in this order:
183 
184                 - (int nTypeId, Class clz, ClassLoader loader)
185                 - (int nTypeId, Class clz)
186                 - (int nTypeId)
187                 - ()
188 
189                 Used in: user-type
190             </xsd:documentation>
191         </xsd:annotation>
192     </xsd:element>
193 
194     <xsd:element name="default-serializer" type="serializer-type">
195         <xsd:annotation>
196             <xsd:documentation>
197                 The default serializer element specifies what PofSerializer to use to
198                 serialize and deserialize all user types defined in this config. If
199                 a serializer is specified for a user type, then that serializer will 
200                 be used for that user type instead of the default serializer.
201 
202                 If the default serializer element is omitted, the serializer defined
203                 for the specific user type will be used. If the serializer for the 
204                 user type is also omitted, then the user type is assumed to implement 
205                 the PortableObject interface, and the PortableObjectSerializer
206                 implementation is used as the PofSerializer.
207 
208                 If the init-params element is omitted from the default serializer
209                 element, then the following four constructors are attempted on the 
210                 specific PofSerializer implementation, and in this order:
211 
212                 - (int nTypeId, Class clz, ClassLoader loader)
213                 - (int nTypeId, Class clz)
214                 - (int nTypeId)
215                 - ()
216 
217                 Used in: pof-config
218             </xsd:documentation>
219         </xsd:annotation>
220     </xsd:element>
221 
222     <xsd:element name="init-params">
223         <xsd:annotation>
224             <xsd:documentation>
225                 The init-params element contains zero or more arguments (each as an
226                 init-param) that correspond to the parameters of a constructor of
227                 the class that is being configured.
228 
229                 Used in: serializer
230             </xsd:documentation>
231         </xsd:annotation>
232         <xsd:complexType>
233             <xsd:sequence>
234                 <xsd:element ref="init-param" minOccurs="0" maxOccurs="unbounded" />
235             </xsd:sequence>
236         </xsd:complexType>
237     </xsd:element>
238 
239     <xsd:element name="init-param">
240         <xsd:annotation>
241             <xsd:documentation>
242                 The init-param element provides a type for a configuration parameter
243                 and a corresponding value to pass as an argument.
244 
245                 Used in: init-params          
246              </xsd:documentation>
247         </xsd:annotation>
248         <xsd:complexType>
249             <xsd:sequence>
250                 <xsd:element ref="param-type" />
251                 <xsd:element ref="param-value" />
252             </xsd:sequence>
253         </xsd:complexType>
254     </xsd:element>
255 
256     <xsd:element name="param-type">
257         <xsd:annotation>
258             <xsd:documentation>
259                 The param-type element specifies the Java type of initialization
260                 parameter.
261 
262                 Supported types are:
263                 - string - indicates that the value is a java.lang.String
264                 - boolean - indicates that the value is a java.lang.Boolean
265                 - int - indicates that the value is a java.lang.Integer
266                 - long - indicates that the value is a java.lang.Long
267                 - double - indicates that the value is a java.lang.Double
268                 - decimal - indicates that the value is a 
269                 java.math.BigDecimal
270                 - file - indicates that the value is a
271                 java.io.File
272                 - date - indicates that the value is a java.sql.Date
273                 - time - indicates that the value is a java.sql.Time
274                 - datetime -indicates that the value is a 
275                 java.sql.Timestamp
276                 - xml - indicates that the value is the entire init-param 
277                 XmlElement
278 
279                 The value is converted to the specified type, and the target
280                 constructor or method must have a parameter of that type in 
281                 order for the instantiation to succeed.
282 
283                 Used in: init-param   
284             </xsd:documentation>
285         </xsd:annotation>
286         <xsd:simpleType>
287             <xsd:restriction base="xsd:normalizedString">
288                 <xsd:enumeration value="string" />
289                 <xsd:enumeration value="java.lang.String" />
290                 <xsd:enumeration value="int" />
291                 <xsd:enumeration value="java.lang.Integer" />
292                 <xsd:enumeration value="long" />
293                 <xsd:enumeration value="java.lang.Long" />
294                 <xsd:enumeration value="boolean" />
295                 <xsd:enumeration value="java.lang.Boolean" />
296                 <xsd:enumeration value="double" />
297                 <xsd:enumeration value="java.lang.Double" />
298                 <xsd:enumeration value="float" />
299                 <xsd:enumeration value="java.lang.Float" />
300                 <xsd:enumeration value="decimal" />
301                 <xsd:enumeration value="java.math.BigDecimal" />
302                 <xsd:enumeration value="file" />
303                 <xsd:enumeration value="java.io.File" />
304                 <xsd:enumeration value="date" />
305                 <xsd:enumeration value="java.sql.Date" />
306                 <xsd:enumeration value="time" />
307                 <xsd:enumeration value="java.sql.Time" />
308                 <xsd:enumeration value="datetime" />
309                 <xsd:enumeration value="java.sql.Timestamp" />
310                 <xsd:enumeration value="xml" />
311                 <xsd:enumeration value="com.tangosol.run.xml.XmlElement" />
312             </xsd:restriction>
313         </xsd:simpleType>
314     </xsd:element>
315 
316     <xsd:element name="param-value" type="xsd:string">
317         <xsd:annotation>
318             <xsd:documentation>
319                 The param-value element specifies a value of the initialization
320                 parameter.
321 
322                 The value is in a format specific to the type of the parameter.
323 
324                 There are four reserved values that can be specified. Each of these
325                 values
326                 is replaced at runtime with a specific runtime value before the
327                 constructor
328                 is invoked:
329                 - "{type-id}" - replaced with the Type ID of the User Type;
330                 - "{class-name}" - replaced with the name of the class for the User
331                 Type;
332                 - "{class}" - replaced with the Class for the User Type;
333                 -
334                 "{class-loader}" - replaced with the ConfigurablePofContext's
335                 ContextClassLoader.
336 
337                 Used in: init-param
338             </xsd:documentation>
339         </xsd:annotation>
340     </xsd:element>
341 
342     <xsd:element name="allow-interfaces" type="xsd:boolean">
343         <xsd:annotation>
344             <xsd:documentation>
345                 The allow-interfaces element indicates whether the user-type class-name
346                 can specify Java interface types in addition to Java class types.
347 
348                 Valid values are "true" or "false". Default value is false.
349 
350                 Used in: pof-config 
351             </xsd:documentation>
352         </xsd:annotation>
353     </xsd:element>
354 
355     <xsd:element name="allow-subclasses" type="xsd:boolean">
356         <xsd:annotation>
357             <xsd:documentation>
358                 The allow-subclasses element indicates whether the user-type class-name
359                 can specify a Java class type that is abstract, and whether sub-classes 
360                 of any specified user-type class-name will be permitted at runtime and
361                 automatically mapped to the specified super-class for purposes of 
362                 obtaining a serializer.
363 
364                 Valid values are "true" or "false". Default value is false.
365 
366                 Used in: pof-config     
367             </xsd:documentation>
368         </xsd:annotation>
369     </xsd:element>
370 
371 </xsd:schema>
372 
373 

Implementation Examples

Because the implementation of the Elements and Complex types is fairly generic I will not document all the Interfaces and Classes I have produced but rather document some example Interfaces and Classes. The common methods and functionality implemented will be further explained in the XAM/XDM implementation section.

PofConfigTextComponent & PofConfigTextComponentImpl

This Interface and class define the methods that will be used by all Simple Elements that are of xsd:string type. The interface defines a simple getValue and setValue method that return and set a simple string value. It can be seen that the Abstract class, associated with the interface, assumes that any inherited classes implement the getTagName() methods to return the name of this tag. This is required because setValue implements the underlying protected setText method that requires the name of the implementing element.

PofConfigTextComponent

 1 /*
 2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 3  *
 4  * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
 5  *
 6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
 7  * Other names may be trademarks of their respective owners.
 8  *
 9  * The contents of this file are subject to the terms of either the GNU
10  * General Public License Version 2 only ("GPL") or the Common
11  * Development and Distribution License("CDDL") (collectively, the
12  * "License"). You may not use this file except in compliance with the
13  * License. You can obtain a copy of the License at
14  * http://www.netbeans.org/cddl-gplv2.html
15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16  * specific language governing permissions and limitations under the
17  * License.  When distributing the software, include this License Header
18  * Notice in each file and include the License file at
19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20  * particular file as subject to the "Classpath" exception as provided
21  * by Oracle in the GPL Version 2 section of the License file that
22  * accompanied this code. If applicable, add the following below the
23  * License Header, with the fields enclosed by brackets [] replaced by
24  * your own identifying information:
25  * "Portions Copyrighted [year] [name of copyright owner]"
26  *
27  * If you wish your version of this file to be governed by only the CDDL
28  * or only the GPL Version 2, indicate your decision by adding
29  * "[Contributor] elects to include this software in this distribution
30  * under the [CDDL or GPL Version 2] license." If you do not indicate a
31  * single choice of license, a recipient has the option to distribute
32  * your version of this file under either the CDDL, the GPL Version 2 or
33  * to extend the choice of license to its licensees as provided above.
34  * However, if you add GPL Version 2 code and therefore, elected the GPL
35  * Version 2 license, then the option applies only if the new code is
36  * made subject to such option by the copyright holder.
37  *
38  * Contributor(s):
39  *
40  * Portions Copyrighted 2011 Sun Microsystems, Inc.
41  */
42 package org.netbeans.modules.coherence.xml.pof;
43 
44 /**
45  *
46  * @author Andrew Hopkinson (Oracle A-Team)
47  */
48 public interface PofConfigTextComponent extends PofConfigComponent {
49     public String getValue();
50     public void setValue(String value);
51 }
52 
53 

PofConfigTextComponentImpl

 1 /*
 2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 3  *
 4  * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
 5  *
 6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
 7  * Other names may be trademarks of their respective owners.
 8  *
 9  * The contents of this file are subject to the terms of either the GNU
10  * General Public License Version 2 only ("GPL") or the Common
11  * Development and Distribution License("CDDL") (collectively, the
12  * "License"). You may not use this file except in compliance with the
13  * License. You can obtain a copy of the License at
14  * http://www.netbeans.org/cddl-gplv2.html
15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16  * specific language governing permissions and limitations under the
17  * License.  When distributing the software, include this License Header
18  * Notice in each file and include the License file at
19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20  * particular file as subject to the "Classpath" exception as provided
21  * by Oracle in the GPL Version 2 section of the License file that
22  * accompanied this code. If applicable, add the following below the
23  * License Header, with the fields enclosed by brackets [] replaced by
24  * your own identifying information:
25  * "Portions Copyrighted [year] [name of copyright owner]"
26  *
27  * If you wish your version of this file to be governed by only the CDDL
28  * or only the GPL Version 2, indicate your decision by adding
29  * "[Contributor] elects to include this software in this distribution
30  * under the [CDDL or GPL Version 2] license." If you do not indicate a
31  * single choice of license, a recipient has the option to distribute
32  * your version of this file under either the CDDL, the GPL Version 2 or
33  * to extend the choice of license to its licensees as provided above.
34  * However, if you add GPL Version 2 code and therefore, elected the GPL
35  * Version 2 license, then the option applies only if the new code is
36  * made subject to such option by the copyright holder.
37  *
38  * Contributor(s):
39  *
40  * Portions Copyrighted 2011 Sun Microsystems, Inc.
41  */
42 package org.netbeans.modules.coherence.xml.pof.impl;
43 
44 import org.netbeans.modules.coherence.xml.pof.PofConfigTextComponent;
45 import org.w3c.dom.Element;
46 
47 /**
48  *
49  * @author Andrew Hopkinson (Oracle A-Team)
50  */
51 public abstract class PofConfigTextComponentImpl extends PofConfigComponentImpl implements PofConfigTextComponent {
52 
53     public PofConfigTextComponentImpl(PofConfigModelImpl model, Element e) {
54         super(model, e);
55     }
56 
57     @Override
58     public String getValue() {
59         return getText();
60     }
61     
62     @Override
63     public void setValue(String value) {
64         setText(getTagName(), value);
65     }
66     
67 }
68 
69 

IncludeImpl

 1 /*
 2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 3  *
 4  * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
 5  *
 6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
 7  * Other names may be trademarks of their respective owners.
 8  *
 9  * The contents of this file are subject to the terms of either the GNU
10  * General Public License Version 2 only ("GPL") or the Common
11  * Development and Distribution License("CDDL") (collectively, the
12  * "License"). You may not use this file except in compliance with the
13  * License. You can obtain a copy of the License at
14  * http://www.netbeans.org/cddl-gplv2.html
15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16  * specific language governing permissions and limitations under the
17  * License.  When distributing the software, include this License Header
18  * Notice in each file and include the License file at
19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20  * particular file as subject to the "Classpath" exception as provided
21  * by Oracle in the GPL Version 2 section of the License file that
22  * accompanied this code. If applicable, add the following below the
23  * License Header, with the fields enclosed by brackets [] replaced by
24  * your own identifying information:
25  * "Portions Copyrighted [year] [name of copyright owner]"
26  *
27  * If you wish your version of this file to be governed by only the CDDL
28  * or only the GPL Version 2, indicate your decision by adding
29  * "[Contributor] elects to include this software in this distribution
30  * under the [CDDL or GPL Version 2] license." If you do not indicate a
31  * single choice of license, a recipient has the option to distribute
32  * your version of this file under either the CDDL, the GPL Version 2 or
33  * to extend the choice of license to its licensees as provided above.
34  * However, if you add GPL Version 2 code and therefore, elected the GPL
35  * Version 2 license, then the option applies only if the new code is
36  * made subject to such option by the copyright holder.
37  *
38  * Contributor(s):
39  *
40  * Portions Copyrighted 2011 Sun Microsystems, Inc.
41  */
42 package org.netbeans.modules.coherence.xml.pof.impl;
43 
44 import org.netbeans.modules.coherence.xml.pof.Include;
45 import org.netbeans.modules.coherence.xml.pof.PofConfigComponent;
46 import org.netbeans.modules.coherence.xml.pof.PofConfigVisitor;
47 import org.w3c.dom.Element;
48 
49 /**
50  *
51  * @author Andrew Hopkinson (Oracle A-Team)
52  */
53 public class IncludeImpl extends PofConfigTextComponentImpl implements Include {
54 
55     public IncludeImpl(PofConfigModelImpl model, Element e) {
56         super(model, e);
57     }
58     
59     public IncludeImpl(PofConfigModelImpl model) {
60         super(model, createNewElement(XML_TAG_NAME, model));
61     }
62     
63     @Override
64     public String getTagName() {
65         return Include.XML_TAG_NAME;
66     }
67 
68     @Override
69     public void accept(PofConfigVisitor visitor) {
70         visitor.visit(this);
71     }
72 
73     @Override
74     public Class<? extends PofConfigComponent> getComponentType() {
75         return Include.class;
76     }
77 
78 }
79 
80 

UserTypeList & UserTypeListImpl

The UserTypeList Interface and Class show how to implement a Complex Element / Type that contains an un-sequenced repeating group and it can be seen that we have 4 key methods that add, remove and retrieve the Sub Elements of the <user-type-list> element. The Impl class shows that these simply wrap the underlying org.netbeans.modules.xml.xam.AbstractComponent insertAtIndex, appendChild and removeChild specifically indicating that the element concerned is a Sub Element of the <user-type-list> tag. Hence the user does not need to be aware of the tag type or name.

To improve read access I have also implement two additional methods, one for each sub-element, getIncludes() and getUserTypes() which simply return just the elements of the appropriate type.

UserTypeList

 1 /*
 2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 3  *
 4  * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
 5  *
 6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
 7  * Other names may be trademarks of their respective owners.
 8  *
 9  * The contents of this file are subject to the terms of either the GNU
10  * General Public License Version 2 only ("GPL") or the Common
11  * Development and Distribution License("CDDL") (collectively, the
12  * "License"). You may not use this file except in compliance with the
13  * License. You can obtain a copy of the License at
14  * http://www.netbeans.org/cddl-gplv2.html
15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16  * specific language governing permissions and limitations under the
17  * License.  When distributing the software, include this License Header
18  * Notice in each file and include the License file at
19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20  * particular file as subject to the "Classpath" exception as provided
21  * by Oracle in the GPL Version 2 section of the License file that
22  * accompanied this code. If applicable, add the following below the
23  * License Header, with the fields enclosed by brackets [] replaced by
24  * your own identifying information:
25  * "Portions Copyrighted [year] [name of copyright owner]"
26  *
27  * If you wish your version of this file to be governed by only the CDDL
28  * or only the GPL Version 2, indicate your decision by adding
29  * "[Contributor] elects to include this software in this distribution
30  * under the [CDDL or GPL Version 2] license." If you do not indicate a
31  * single choice of license, a recipient has the option to distribute
32  * your version of this file under either the CDDL, the GPL Version 2 or
33  * to extend the choice of license to its licensees as provided above.
34  * However, if you add GPL Version 2 code and therefore, elected the GPL
35  * Version 2 license, then the option applies only if the new code is
36  * made subject to such option by the copyright holder.
37  *
38  * Contributor(s):
39  *
40  * Portions Copyrighted 2011 Sun Microsystems, Inc.
41  */
42 package org.netbeans.modules.coherence.xml.pof;
43 
44 import java.util.List;
45 
46 /**
47  *
48  * @author Andrew Hopkinson (Oracle A-Team)
49  */
50 public interface UserTypeList extends PofConfigComponent {
51 
52     static String XML_TAG_NAME = "user-type-list";
53 
54     public List<Include> getIncludes();
55 
56     public List<UserType> getUserTypes();
57 
58     List<UserTypeListElement> getElements();
59 
60     void addElement(UserTypeListElement element) throws ValueNotPermittedException;
61 
62     void addElement(int index, UserTypeListElement element) throws ValueNotPermittedException;
63 
64     void removeElement(UserTypeListElement element);
65 }
66 
67 

UserTypeListImpl

  1 /*
  2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3  *
  4  * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
  5  *
  6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7  * Other names may be trademarks of their respective owners.
  8  *
  9  * The contents of this file are subject to the terms of either the GNU
 10  * General Public License Version 2 only ("GPL") or the Common
 11  * Development and Distribution License("CDDL") (collectively, the
 12  * "License"). You may not use this file except in compliance with the
 13  * License. You can obtain a copy of the License at
 14  * http://www.netbeans.org/cddl-gplv2.html
 15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 16  * specific language governing permissions and limitations under the
 17  * License.  When distributing the software, include this License Header
 18  * Notice in each file and include the License file at
 19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
 20  * particular file as subject to the "Classpath" exception as provided
 21  * by Oracle in the GPL Version 2 section of the License file that
 22  * accompanied this code. If applicable, add the following below the
 23  * License Header, with the fields enclosed by brackets [] replaced by
 24  * your own identifying information:
 25  * "Portions Copyrighted [year] [name of copyright owner]"
 26  *
 27  * If you wish your version of this file to be governed by only the CDDL
 28  * or only the GPL Version 2, indicate your decision by adding
 29  * "[Contributor] elects to include this software in this distribution
 30  * under the [CDDL or GPL Version 2] license." If you do not indicate a
 31  * single choice of license, a recipient has the option to distribute
 32  * your version of this file under either the CDDL, the GPL Version 2 or
 33  * to extend the choice of license to its licensees as provided above.
 34  * However, if you add GPL Version 2 code and therefore, elected the GPL
 35  * Version 2 license, then the option applies only if the new code is
 36  * made subject to such option by the copyright holder.
 37  *
 38  * Contributor(s):
 39  *
 40  * Portions Copyrighted 2011 Sun Microsystems, Inc.
 41  */
 42 package org.netbeans.modules.coherence.xml.pof.impl;
 43 
 44 import java.util.List;
 45 import org.netbeans.modules.coherence.xml.pof.Include;
 46 import org.netbeans.modules.coherence.xml.pof.PofConfigComponent;
 47 import org.netbeans.modules.coherence.xml.pof.PofConfigVisitor;
 48 import org.netbeans.modules.coherence.xml.pof.UserType;
 49 import org.netbeans.modules.coherence.xml.pof.UserTypeList;
 50 import org.netbeans.modules.coherence.xml.pof.UserTypeListElement;
 51 import org.netbeans.modules.coherence.xml.pof.ValueNotPermittedException;
 52 import org.w3c.dom.Element;
 53 
 54 /**
 55  *
 56  * @author Andrew Hopkinson (Oracle A-Team)
 57  */
 58 public class UserTypeListImpl extends PofConfigComponentImpl implements UserTypeList {
 59 
 60     public UserTypeListImpl(PofConfigModelImpl model, Element e) {
 61         super(model, e);
 62     }
 63     
 64     public UserTypeListImpl(PofConfigModelImpl model) {
 65         super(model, createNewElement(XML_TAG_NAME, model));
 66     }
 67     
 68     @Override
 69     public String getTagName() {
 70         return UserTypeList.XML_TAG_NAME;
 71     }
 72 
 73     @Override
 74     public void accept(PofConfigVisitor visitor) {
 75         visitor.visit(this);
 76     }
 77 
 78     @Override
 79     public Class<? extends PofConfigComponent> getComponentType() {
 80         return UserTypeList.class;
 81     }
 82 
 83     @Override
 84     public List<Include> getIncludes() {
 85         return getChildren(Include.class);
 86     }
 87 
 88     @Override
 89     public List<UserType> getUserTypes() {
 90         return getChildren(UserType.class);
 91     }
 92 
 93     @Override
 94     public List<UserTypeListElement> getElements() {
 95         return getChildren(UserTypeListElement.class);
 96     }
 97 
 98     @Override
 99     public void addElement(UserTypeListElement element) throws ValueNotPermittedException {
100         appendChild(UserTypeListElement.USERTYPELIST_ELEMENT, element);
101     }
102 
103     @Override
104     public void addElement(int index, UserTypeListElement element) throws ValueNotPermittedException {
105         insertAtIndex(UserTypeListElement.USERTYPELIST_ELEMENT, element, index);
106     }
107 
108     @Override
109     public void removeElement(UserTypeListElement element) {
110         removeChild( UserTypeListElement.USERTYPELIST_ELEMENT,  element );
111     }
112     
113 }
114 
115 

PofConfig & PofConfigImpl

This Interface and associated class are a representation of the top level <pof-config> tag which cotains a <xsd:sequence> now as mentioned the sequence defines the order in which elements can appear within the xml and as such we can simply write a getter and setter for each of the Sub Elements. The implemented getter methods will call the underlying getChildren method specifying the appropriate Interface class then return the first element, because their can only be one, or null if it does not exist.

It can be seen within the implementation class that the set methods for each of the Sub Elements use the underlying addBefore or addAfter methods to specify where in the sequence they should be added.

PofConfig

 1 /*
 2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 3  *
 4  * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
 5  *
 6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
 7  * Other names may be trademarks of their respective owners.
 8  *
 9  * The contents of this file are subject to the terms of either the GNU
10  * General Public License Version 2 only ("GPL") or the Common
11  * Development and Distribution License("CDDL") (collectively, the
12  * "License"). You may not use this file except in compliance with the
13  * License. You can obtain a copy of the License at
14  * http://www.netbeans.org/cddl-gplv2.html
15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16  * specific language governing permissions and limitations under the
17  * License.  When distributing the software, include this License Header
18  * Notice in each file and include the License file at
19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20  * particular file as subject to the "Classpath" exception as provided
21  * by Oracle in the GPL Version 2 section of the License file that
22  * accompanied this code. If applicable, add the following below the
23  * License Header, with the fields enclosed by brackets [] replaced by
24  * your own identifying information:
25  * "Portions Copyrighted [year] [name of copyright owner]"
26  *
27  * If you wish your version of this file to be governed by only the CDDL
28  * or only the GPL Version 2, indicate your decision by adding
29  * "[Contributor] elects to include this software in this distribution
30  * under the [CDDL or GPL Version 2] license." If you do not indicate a
31  * single choice of license, a recipient has the option to distribute
32  * your version of this file under either the CDDL, the GPL Version 2 or
33  * to extend the choice of license to its licensees as provided above.
34  * However, if you add GPL Version 2 code and therefore, elected the GPL
35  * Version 2 license, then the option applies only if the new code is
36  * made subject to such option by the copyright holder.
37  *
38  * Contributor(s):
39  *
40  * Portions Copyrighted 2011 Sun Microsystems, Inc.
41  */
42 package org.netbeans.modules.coherence.xml.pof;
43 
44 import java.util.List;
45 
46 /**
47  *
48  * @author Andrew Hopkinson (Oracle A-Team)
49  */
50 public interface PofConfig extends PofConfigComponent {
51 
52     public static String XML_TAG_NAME = "pof-config";
53 
54     public UserTypeList getUserTypeList();
55 
56     public void setUserTypeList(UserTypeList element);
57 
58     public AllowInterfaces getAllowInterfaces();
59 
60     public void setAllowInterfaces(AllowInterfaces element);
61 
62     public AllowSubclasses getAllowSubclasses();
63 
64     public void setAllowSubclasses(AllowSubclasses element);
65 
66     public DefaultSerializer getDefaultSerializer();
67 
68     public void setDefaultSerializer(DefaultSerializer element);
69 
70 }
71 
72 

PofConfigImpl

  1 /*
  2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3  *
  4  * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
  5  *
  6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7  * Other names may be trademarks of their respective owners.
  8  *
  9  * The contents of this file are subject to the terms of either the GNU
 10  * General Public License Version 2 only ("GPL") or the Common
 11  * Development and Distribution License("CDDL") (collectively, the
 12  * "License"). You may not use this file except in compliance with the
 13  * License. You can obtain a copy of the License at
 14  * http://www.netbeans.org/cddl-gplv2.html
 15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 16  * specific language governing permissions and limitations under the
 17  * License.  When distributing the software, include this License Header
 18  * Notice in each file and include the License file at
 19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
 20  * particular file as subject to the "Classpath" exception as provided
 21  * by Oracle in the GPL Version 2 section of the License file that
 22  * accompanied this code. If applicable, add the following below the
 23  * License Header, with the fields enclosed by brackets [] replaced by
 24  * your own identifying information:
 25  * "Portions Copyrighted [year] [name of copyright owner]"
 26  *
 27  * If you wish your version of this file to be governed by only the CDDL
 28  * or only the GPL Version 2, indicate your decision by adding
 29  * "[Contributor] elects to include this software in this distribution
 30  * under the [CDDL or GPL Version 2] license." If you do not indicate a
 31  * single choice of license, a recipient has the option to distribute
 32  * your version of this file under either the CDDL, the GPL Version 2 or
 33  * to extend the choice of license to its licensees as provided above.
 34  * However, if you add GPL Version 2 code and therefore, elected the GPL
 35  * Version 2 license, then the option applies only if the new code is
 36  * made subject to such option by the copyright holder.
 37  *
 38  * Contributor(s):
 39  *
 40  * Portions Copyrighted 2011 Sun Microsystems, Inc.
 41  */
 42 package org.netbeans.modules.coherence.xml.pof.impl;
 43 
 44 import java.util.ArrayList;
 45 import java.util.Collection;
 46 import java.util.List;
 47 import org.netbeans.modules.coherence.xml.pof.AllowInterfaces;
 48 import org.netbeans.modules.coherence.xml.pof.AllowSubclasses;
 49 import org.netbeans.modules.coherence.xml.pof.DefaultSerializer;
 50 import org.netbeans.modules.coherence.xml.pof.PofConfig;
 51 import org.netbeans.modules.coherence.xml.pof.PofConfigComponent;
 52 import org.netbeans.modules.coherence.xml.pof.PofConfigVisitor;
 53 import org.netbeans.modules.coherence.xml.pof.UserTypeList;
 54 import org.w3c.dom.Element;
 55 
 56 /**
 57  *
 58  * @author Andrew Hopkinson (Oracle A-Team)
 59  */
 60 public class PofConfigImpl extends PofConfigComponentImpl implements PofConfig {
 61 
 62     PofConfigImpl(PofConfigModelImpl model, Element e) {
 63         super(model, e);
 64     }
 65 
 66     PofConfigImpl(PofConfigModelImpl model) {
 67         this(model, createNewElement(XML_TAG_NAME, model));
 68     }
 69 
 70     @Override
 71     public String getTagName() {
 72         return PofConfig.XML_TAG_NAME;
 73     }
 74 
 75     @Override
 76     public void accept(PofConfigVisitor visitor) {
 77         visitor.visit(this);
 78     }
 79 
 80     @Override
 81     public Class<? extends PofConfigComponent> getComponentType() {
 82         return PofConfig.class;
 83     }
 84 
 85     @Override
 86     public UserTypeList getUserTypeList() {
 87         List<UserTypeList> elements = getChildren(UserTypeList.class);
 88         if (elements != null && elements.size() > 0) {
 89             return elements.get(0);
 90         }
 91         return null;
 92     }
 93 
 94     @Override
 95     public void setUserTypeList(UserTypeList element) {
 96         UserTypeList child = getUserTypeList();
 97         if (child != null) {
 98             removeChild(UserTypeList.XML_TAG_NAME, child);
 99         }
100         if (element != null) {
101             Collection typeList = new ArrayList();
102             typeList.add(AllowInterfacesImpl.class);
103             typeList.add(AllowSubclassesImpl.class);
104             typeList.add(DefaultSerializerImpl.class);
105             addBefore(UserTypeList.XML_TAG_NAME, element, typeList);
106         }
107     }
108 
109     @Override
110     public AllowInterfaces getAllowInterfaces() {
111         List<AllowInterfaces> elements = getChildren(AllowInterfaces.class);
112         if (elements != null && elements.size() > 0) {
113             return elements.get(0);
114         }
115         return null;
116     }
117 
118     @Override
119     public void setAllowInterfaces(AllowInterfaces element) {
120         AllowInterfaces child = getAllowInterfaces();
121         if (child != null) {
122             removeChild(AllowInterfaces.XML_TAG_NAME, child);
123         }
124         if (element != null) {
125             Collection typeList = new ArrayList();
126             typeList.add(UserTypeListImpl.class);
127             addAfter(AllowInterfaces.XML_TAG_NAME, element, typeList);
128         }
129     }
130 
131     @Override
132     public AllowSubclasses getAllowSubclasses() {
133         List<AllowSubclasses> elements = getChildren(AllowSubclasses.class);
134         if (elements != null && elements.size() > 0) {
135             return elements.get(0);
136         }
137         return null;
138     }
139 
140     @Override
141     public void setAllowSubclasses(AllowSubclasses element) {
142         AllowSubclasses child = getAllowSubclasses();
143         if (child != null) {
144             removeChild(AllowSubclasses.XML_TAG_NAME, child);
145         }
146         if (element != null) {
147             Collection typeList = new ArrayList();
148             typeList.add(UserTypeListImpl.class);
149             typeList.add(AllowInterfacesImpl.class);
150             addAfter(AllowSubclasses.XML_TAG_NAME, element, typeList);
151         }
152     }
153 
154     @Override
155     public DefaultSerializer getDefaultSerializer() {
156         List<DefaultSerializer> elements = getChildren(DefaultSerializer.class);
157         if (elements != null && elements.size() > 0) {
158             return elements.get(0);
159         }
160         return null;
161     }
162 
163     @Override
164     public void setDefaultSerializer(DefaultSerializer element) {
165         DefaultSerializer child = getDefaultSerializer();
166         if (child != null) {
167             removeChild(DefaultSerializer.XML_TAG_NAME, child);
168         }
169         if (element != null) {
170             Collection typeList = new ArrayList();
171             typeList.add(UserTypeListImpl.class);
172             typeList.add(AllowInterfacesImpl.class);
173             typeList.add(AllowSubclassesImpl.class);
174             addAfter(DefaultSerializer.XML_TAG_NAME, element, typeList);
175         }
176     }
177 }
178 
179 

XAM/XDM Implementation Requirements

To implement the XAM/XDM Object Model their are a number of classes that must be created, in addition to those required for the Elements, and these control the parsing, creation, sychronisation and removal of the Object Model and its elements. Essentially these Interfaces and Classes would be common across every XAM/XDM implementation although their actual implementation would be different as can be seen from the code.

I will discuss each of these and explain what functionality they provide. I will use a simple naming convention that takes the name of the top level element, pof-config (PofConfig), and prepends it to the various required Interfaces and Classes.

PofConfigComponentFactory & PofConfigComponentFactoryImpl

These constitute the Factory classes that will be used to create all the available Elements within the coherence-pof-config.xsd. It can be seen that no parameters are passed to any of the methods and as such they create simple empty default instances of the appropriate element. At present the Impl does not force the existence of any Mandatory Sub-Elements this is simply left to the user of the code to implement.

In addition to the create methods the Impl provides an implementation of a getVisitor method that will create / retrieve an instance of the Visitor Implementation (see later).

PofConfigComponentFactory

 1 /*
 2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 3  *
 4  * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
 5  *
 6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
 7  * Other names may be trademarks of their respective owners.
 8  *
 9  * The contents of this file are subject to the terms of either the GNU
10  * General Public License Version 2 only ("GPL") or the Common
11  * Development and Distribution License("CDDL") (collectively, the
12  * "License"). You may not use this file except in compliance with the
13  * License. You can obtain a copy of the License at
14  * http://www.netbeans.org/cddl-gplv2.html
15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16  * specific language governing permissions and limitations under the
17  * License.  When distributing the software, include this License Header
18  * Notice in each file and include the License file at
19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20  * particular file as subject to the "Classpath" exception as provided
21  * by Oracle in the GPL Version 2 section of the License file that
22  * accompanied this code. If applicable, add the following below the
23  * License Header, with the fields enclosed by brackets [] replaced by
24  * your own identifying information:
25  * "Portions Copyrighted [year] [name of copyright owner]"
26  *
27  * If you wish your version of this file to be governed by only the CDDL
28  * or only the GPL Version 2, indicate your decision by adding
29  * "[Contributor] elects to include this software in this distribution
30  * under the [CDDL or GPL Version 2] license." If you do not indicate a
31  * single choice of license, a recipient has the option to distribute
32  * your version of this file under either the CDDL, the GPL Version 2 or
33  * to extend the choice of license to its licensees as provided above.
34  * However, if you add GPL Version 2 code and therefore, elected the GPL
35  * Version 2 license, then the option applies only if the new code is
36  * made subject to such option by the copyright holder.
37  *
38  * Contributor(s):
39  *
40  * Portions Copyrighted 2011 Sun Microsystems, Inc.
41  */
42 package org.netbeans.modules.coherence.xml.pof;
43 
44 import org.w3c.dom.Element;
45 
46 /**
47  *
48  * @author Andrew Hopkinson (Oracle A-Team)
49  */
50 public interface PofConfigComponentFactory {
51     
52     public PofConfigComponent createComponent( Element element, PofConfigComponent context);
53     
54     public PofConfig createPofConfig();
55     public UserTypeList createUserTypeList();
56     public UserType createUserType();
57     public Include createInclude();
58     public TypeId createTypeId();
59     public ClassName createClassName();
60     public SerializerType createSerializerType();
61     public Serializer createSerializer();
62     public DefaultSerializer createDefaultSerializer();
63     public InitParams createInitParams();
64     public InitParam createInitParam();
65     public ParamType createParamType();
66     public ParamValue createParamValue();
67     public AllowInterfaces createAllowInterfaces();
68     public AllowSubclasses createAllowSubclasses();
69 }
70 
71 

PofConfigComponentFactoryImpl

  1 /*
  2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3  *
  4  * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
  5  *
  6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7  * Other names may be trademarks of their respective owners.
  8  *
  9  * The contents of this file are subject to the terms of either the GNU
 10  * General Public License Version 2 only ("GPL") or the Common
 11  * Development and Distribution License("CDDL") (collectively, the
 12  * "License"). You may not use this file except in compliance with the
 13  * License. You can obtain a copy of the License at
 14  * http://www.netbeans.org/cddl-gplv2.html
 15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 16  * specific language governing permissions and limitations under the
 17  * License.  When distributing the software, include this License Header
 18  * Notice in each file and include the License file at
 19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
 20  * particular file as subject to the "Classpath" exception as provided
 21  * by Oracle in the GPL Version 2 section of the License file that
 22  * accompanied this code. If applicable, add the following below the
 23  * License Header, with the fields enclosed by brackets [] replaced by
 24  * your own identifying information:
 25  * "Portions Copyrighted [year] [name of copyright owner]"
 26  *
 27  * If you wish your version of this file to be governed by only the CDDL
 28  * or only the GPL Version 2, indicate your decision by adding
 29  * "[Contributor] elects to include this software in this distribution
 30  * under the [CDDL or GPL Version 2] license." If you do not indicate a
 31  * single choice of license, a recipient has the option to distribute
 32  * your version of this file under either the CDDL, the GPL Version 2 or
 33  * to extend the choice of license to its licensees as provided above.
 34  * However, if you add GPL Version 2 code and therefore, elected the GPL
 35  * Version 2 license, then the option applies only if the new code is
 36  * made subject to such option by the copyright holder.
 37  *
 38  * Contributor(s):
 39  *
 40  * Portions Copyrighted 2011 Sun Microsystems, Inc.
 41  */
 42 package org.netbeans.modules.coherence.xml.pof.impl;
 43 
 44 import org.netbeans.modules.coherence.xml.pof.AllowInterfaces;
 45 import org.netbeans.modules.coherence.xml.pof.AllowSubclasses;
 46 import org.netbeans.modules.coherence.xml.pof.ClassName;
 47 import org.netbeans.modules.coherence.xml.pof.DefaultSerializer;
 48 import org.netbeans.modules.coherence.xml.pof.Include;
 49 import org.netbeans.modules.coherence.xml.pof.InitParam;
 50 import org.netbeans.modules.coherence.xml.pof.InitParams;
 51 import org.netbeans.modules.coherence.xml.pof.ParamType;
 52 import org.netbeans.modules.coherence.xml.pof.ParamValue;
 53 import org.netbeans.modules.coherence.xml.pof.PofConfig;
 54 import org.netbeans.modules.coherence.xml.pof.PofConfigComponent;
 55 import org.netbeans.modules.coherence.xml.pof.PofConfigComponentFactory;
 56 import org.netbeans.modules.coherence.xml.pof.PofConfigVisitor;
 57 import org.netbeans.modules.coherence.xml.pof.Serializer;
 58 import org.netbeans.modules.coherence.xml.pof.SerializerType;
 59 import org.netbeans.modules.coherence.xml.pof.TypeId;
 60 import org.netbeans.modules.coherence.xml.pof.UserType;
 61 import org.netbeans.modules.coherence.xml.pof.UserTypeList;
 62 import org.w3c.dom.Element;
 63 
 64 /**
 65  *
 66  * @author Andrew Hopkinson (Oracle A-Team)
 67  */
 68 public class PofConfigComponentFactoryImpl implements PofConfigComponentFactory {
 69 
 70     private PofConfigModelImpl myModel;
 71     private ThreadLocal<PofConfigVisitorImpl> myBuilder;
 72 
 73     PofConfigComponentFactoryImpl(PofConfigModelImpl model) {
 74         myModel = model;
 75         myBuilder = new ThreadLocal<PofConfigVisitorImpl>();
 76     }
 77 
 78     @Override
 79     public PofConfigComponent createComponent(Element element, PofConfigComponent context) {
 80         PofConfigVisitorImpl visitor = getVisitor();
 81         return visitor.create(context, element);
 82     }
 83 
 84     @Override
 85     public PofConfig createPofConfig() {
 86         return new PofConfigImpl(getModel());
 87     }
 88 
 89     @Override
 90     public UserTypeList createUserTypeList() {
 91         return new UserTypeListImpl(getModel());
 92     }
 93 
 94     @Override
 95     public UserType createUserType() {
 96         return new UserTypeImpl(getModel());
 97     }
 98 
 99     @Override
100     public Include createInclude() {
101         return new IncludeImpl(getModel());
102     }
103 
104     @Override
105     public TypeId createTypeId() {
106         return new TypeIdImpl(getModel());
107     }
108 
109     @Override
110     public ClassName createClassName() {
111         return new ClassNameImpl(getModel());
112     }
113 
114     @Override
115     public SerializerType createSerializerType() {
116         return new  SerializerTypeImpl(getModel());
117     }
118 
119     @Override
120     public Serializer createSerializer() {
121         return new SerializerImpl(getModel());
122     }
123 
124     @Override
125     public DefaultSerializer createDefaultSerializer() {
126         return new DefaultSerializerImpl(getModel());
127     }
128 
129     @Override
130     public InitParams createInitParams() {
131         return new InitParamsImpl(getModel());
132     }
133 
134     @Override
135     public InitParam createInitParam() {
136         return new InitParamImpl(getModel());
137     }
138 
139     @Override
140     public ParamType createParamType() {
141         return new ParamTypeImpl(getModel());
142     }
143 
144     @Override
145     public ParamValue createParamValue() {
146         return new ParamValueImpl(getModel());
147     }
148 
149     @Override
150     public AllowInterfaces createAllowInterfaces() {
151         return new AllowInterfacesImpl(getModel());
152     }
153 
154     @Override
155     public AllowSubclasses createAllowSubclasses() {
156         return new AllowSubclassesImpl(getModel());
157     }
158 
159     public PofConfigVisitorImpl getVisitor() {
160         PofConfigVisitorImpl visitor = myBuilder.get();
161         if (visitor == null) {
162             visitor = new PofConfigVisitorImpl(getModel());
163             myBuilder.set(visitor);
164         }
165         visitor.init();
166         
167         return visitor;
168     }
169 
170     public PofConfigModelImpl getModel() {
171         return myModel;
172     }
173 
174 }
175 
176 

PofConfigModel, PofConfigModelImpl & PofConfigModelFactory

The PofConfigModel extends the standard "org.netbeans.modules.xml.xam.dom.AbstractDocumentModel" for the <PofConfigComponent> class and provides implementations for the required Methods. This provides the Core Model representation of the XSD based around the Classes we have built. The Factory class provides the methods for creating the PofConfigModel based on the ModelSource identified as the a NetBeans FileObject. Once the Model representation has been created the Impl class will all the user the retrieve and create components (Elements) and also access the component updater which will keep the model in sync.

PofConfigModel

 1 /*
 2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 3  *
 4  * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
 5  *
 6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
 7  * Other names may be trademarks of their respective owners.
 8  *
 9  * The contents of this file are subject to the terms of either the GNU
10  * General Public License Version 2 only ("GPL") or the Common
11  * Development and Distribution License("CDDL") (collectively, the
12  * "License"). You may not use this file except in compliance with the
13  * License. You can obtain a copy of the License at
14  * http://www.netbeans.org/cddl-gplv2.html
15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16  * specific language governing permissions and limitations under the
17  * License.  When distributing the software, include this License Header
18  * Notice in each file and include the License file at
19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20  * particular file as subject to the "Classpath" exception as provided
21  * by Oracle in the GPL Version 2 section of the License file that
22  * accompanied this code. If applicable, add the following below the
23  * License Header, with the fields enclosed by brackets [] replaced by
24  * your own identifying information:
25  * "Portions Copyrighted [year] [name of copyright owner]"
26  *
27  * If you wish your version of this file to be governed by only the CDDL
28  * or only the GPL Version 2, indicate your decision by adding
29  * "[Contributor] elects to include this software in this distribution
30  * under the [CDDL or GPL Version 2] license." If you do not indicate a
31  * single choice of license, a recipient has the option to distribute
32  * your version of this file under either the CDDL, the GPL Version 2 or
33  * to extend the choice of license to its licensees as provided above.
34  * However, if you add GPL Version 2 code and therefore, elected the GPL
35  * Version 2 license, then the option applies only if the new code is
36  * made subject to such option by the copyright holder.
37  *
38  * Contributor(s):
39  *
40  * Portions Copyrighted 2011 Sun Microsystems, Inc.
41  */
42 package org.netbeans.modules.coherence.xml.pof;
43 
44 import org.netbeans.modules.xml.xam.dom.DocumentModel;
45 
46 /**
47  *
48  * @author Andrew Hopkinson (Oracle A-Team)
49  */
50 public interface PofConfigModel extends DocumentModel<PofConfigComponent> {
51     
52     public PofConfig getPofConfig();
53     
54     public PofConfigComponentFactory getFactory();
55 }
56 
57 

PofConfigModelImpl

  1 /*
  2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3  *
  4  * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
  5  *
  6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7  * Other names may be trademarks of their respective owners.
  8  *
  9  * The contents of this file are subject to the terms of either the GNU
 10  * General Public License Version 2 only ("GPL") or the Common
 11  * Development and Distribution License("CDDL") (collectively, the
 12  * "License"). You may not use this file except in compliance with the
 13  * License. You can obtain a copy of the License at
 14  * http://www.netbeans.org/cddl-gplv2.html
 15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 16  * specific language governing permissions and limitations under the
 17  * License.  When distributing the software, include this License Header
 18  * Notice in each file and include the License file at
 19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
 20  * particular file as subject to the "Classpath" exception as provided
 21  * by Oracle in the GPL Version 2 section of the License file that
 22  * accompanied this code. If applicable, add the following below the
 23  * License Header, with the fields enclosed by brackets [] replaced by
 24  * your own identifying information:
 25  * "Portions Copyrighted [year] [name of copyright owner]"
 26  *
 27  * If you wish your version of this file to be governed by only the CDDL
 28  * or only the GPL Version 2, indicate your decision by adding
 29  * "[Contributor] elects to include this software in this distribution
 30  * under the [CDDL or GPL Version 2] license." If you do not indicate a
 31  * single choice of license, a recipient has the option to distribute
 32  * your version of this file under either the CDDL, the GPL Version 2 or
 33  * to extend the choice of license to its licensees as provided above.
 34  * However, if you add GPL Version 2 code and therefore, elected the GPL
 35  * Version 2 license, then the option applies only if the new code is
 36  * made subject to such option by the copyright holder.
 37  *
 38  * Contributor(s):
 39  *
 40  * Portions Copyrighted 2011 Sun Microsystems, Inc.
 41  */
 42 package org.netbeans.modules.coherence.xml.pof.impl;
 43 
 44 import org.netbeans.modules.coherence.xml.pof.PofConfig;
 45 import org.netbeans.modules.coherence.xml.pof.PofConfigComponent;
 46 import org.netbeans.modules.coherence.xml.pof.PofConfigComponentFactory;
 47 import org.netbeans.modules.coherence.xml.pof.PofConfigModel;
 48 import org.netbeans.modules.xml.xam.ComponentUpdater;
 49 import org.netbeans.modules.xml.xam.ModelSource;
 50 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
 51 import org.w3c.dom.Element;
 52 
 53 /**
 54  *
 55  * @author Andrew Hopkinson (Oracle A-Team)
 56  */
 57 public class PofConfigModelImpl extends AbstractDocumentModel<PofConfigComponent> implements PofConfigModel {
 58 
 59     private PofConfigImpl myRoot;
 60     private PofConfigComponentFactoryImpl myFactory;
 61     private SyncUpdateVisitor mySyncUpdateVisitor;
 62 
 63     public PofConfigModelImpl(ModelSource source) {
 64         super(source);
 65         myFactory = new PofConfigComponentFactoryImpl(this);
 66     }
 67 
 68     @Override
 69     public PofConfigComponent createRootComponent(Element element) {
 70         PofConfigImpl poc = (PofConfigImpl)getFactory().createComponent(element, null);
 71         
 72         if (poc != null) myRoot = poc;
 73         else return null;
 74         
 75         return getPofConfig();
 76     }
 77 
 78     @Override
 79     protected ComponentUpdater<PofConfigComponent> getComponentUpdater() {
 80         if (mySyncUpdateVisitor == null) mySyncUpdateVisitor = new SyncUpdateVisitor();
 81         return mySyncUpdateVisitor;
 82     }
 83 
 84     @Override
 85     public PofConfigComponent getRootComponent() {
 86         return myRoot;
 87     }
 88 
 89     @Override
 90     public PofConfigComponent createComponent(PofConfigComponent parent, Element element) {
 91         return getFactory().createComponent(element, parent);
 92     }
 93 
 94     @Override
 95     public PofConfig getPofConfig() {
 96         return (PofConfig)getRootComponent();
 97     }
 98 
 99     @Override
100     public PofConfigComponentFactory getFactory() {
101         return myFactory;
102     }
103 }
104 
105 

PofConfigModelFactory

 1 /*
 2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 3  *
 4  * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
 5  *
 6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
 7  * Other names may be trademarks of their respective owners.
 8  *
 9  * The contents of this file are subject to the terms of either the GNU
10  * General Public License Version 2 only ("GPL") or the Common
11  * Development and Distribution License("CDDL") (collectively, the
12  * "License"). You may not use this file except in compliance with the
13  * License. You can obtain a copy of the License at
14  * http://www.netbeans.org/cddl-gplv2.html
15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16  * specific language governing permissions and limitations under the
17  * License. When distributing the software, include this License Header
18  * Notice in each file and include the License file at
19  * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20  * particular file as subject to the "Classpath" exception as provided
21  * by Oracle in the GPL Version 2 section of the License file that
22  * accompanied this code. If applicable, add the following below the
23  * License Header, with the fields enclosed by brackets [] replaced by
24  * your own identifying information:
25  * "Portions Copyrighted [year] [name of copyright owner]"
26  *
27  * If you wish your version of this file to be governed by only the CDDL
28  * or only the GPL Version 2, indicate your decision by adding
29  * "[Contributor] elects to include this software in this distribution
30  * under the [CDDL or GPL Version 2] license." If you do not indicate a
31  * single choice of license, a recipient has the option to distribute
32  * your version of this file under either the CDDL, the GPL Version 2 or
33  * to extend the choice of license to its licensees as provided above.
34  * However, if you add GPL Version 2 code and therefore, elected the GPL
35  * Version 2 license, then the option applies only if the new code is
36  * made subject to such option by the copyright holder.
37  *
38  * Contributor(s):
39  *
40  * Portions Copyrighted 2011 Sun Microsystems, Inc.
41  */
42 package org.netbeans.modules.coherence.xml.pof;
43
44 import org.netbeans.modules.coherence.xml.pof.impl.PofConfigModelImpl;
45 import org.netbeans.modules.xml.xam.AbstractModelFactory;
46 import org.netbeans.modules.xml.xam.ModelSource;
47
48 /**
49  *
50  * @author Andrew Hopkinson (Oracle A-Team)
51 */
52 public class PofConfigModelFactory extends AbstractModelFactory<PofConfigModel> {
53
54 private final static PofConfigModelFactory instance = new PofConfigModelFactory();
55
56 public static PofConfigModelFactory getInstance() {
57 return instance;
58 }
59
60 @Override
61 public PofConfigModel createModel(ModelSource ms) {
62 return new PofConfigModelImpl(ms);
63 }
64
65 @Override
66 public synchronized PofConfigModel getModel(ModelSource source) {
67 return (PofConfigModel) super.getModel(source);
68 }
69 }
70
71

PofConfigVisitor & PofConfigVisitorImpl

The XAM / XDM Model structure implements the Visitor Pattern for all XML Processing and as such the PofConfigVisitor and its associated Impl are implementations of the required Interface and Class. As such it can be seen that a visit method is defined for each of the XSD Element Interface definitions.

PofConfigVisitor

 1 /*
 2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 3  *
 4  * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
 5  *
 6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
 7  * Other names may be trademarks of their respective owners.
 8  *
 9  * The contents of this file are subject to the terms of either the GNU
10  * General Public License Version 2 only ("GPL") or the Common
11  * Development and Distribution License("CDDL") (collectively, the
12  * "License"). You may not use this file except in compliance with the
13  * License. You can obtain a copy of the License at
14  * http://www.netbeans.org/cddl-gplv2.html
15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16  * specific language governing permissions and limitations under the
17  * License.  When distributing the software, include this License Header
18  * Notice in each file and include the License file at
19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20  * particular file as subject to the "Classpath" exception as provided
21  * by Oracle in the GPL Version 2 section of the License file that
22  * accompanied this code. If applicable, add the following below the
23  * License Header, with the fields enclosed by brackets [] replaced by
24  * your own identifying information:
25  * "Portions Copyrighted [year] [name of copyright owner]"
26  *
27  * If you wish your version of this file to be governed by only the CDDL
28  * or only the GPL Version 2, indicate your decision by adding
29  * "[Contributor] elects to include this software in this distribution
30  * under the [CDDL or GPL Version 2] license." If you do not indicate a
31  * single choice of license, a recipient has the option to distribute
32  * your version of this file under either the CDDL, the GPL Version 2 or
33  * to extend the choice of license to its licensees as provided above.
34  * However, if you add GPL Version 2 code and therefore, elected the GPL
35  * Version 2 license, then the option applies only if the new code is
36  * made subject to such option by the copyright holder.
37  *
38  * Contributor(s):
39  *
40  * Portions Copyrighted 2011 Sun Microsystems, Inc.
41  */
42 package org.netbeans.modules.coherence.xml.pof;
43 
44 /**
45  *
46  * @author Andrew Hopkinson (Oracle A-Team)
47  */
48 public interface PofConfigVisitor {
49 
50     // PofConfig Top Level
51     void visit(PofConfig pofConfig);
52 
53     void visit(AllowInterfaces allowInterfaces);
54     void visit(AllowSubclasses allowSubclasses);
55     void visit(ClassName className);
56     void visit(DefaultSerializer defaultSerializer);
57     void visit(Include include);
58     void visit(InitParam initParam);
59     void visit(InitParams initParams);
60     void visit(ParamType paramType);
61     void visit(ParamValue paramValue);
62     void visit(Serializer serializer);
63     void visit(SerializerType serializerType);
64     void visit(TypeId typeId);
65     void visit(UserType userType);
66     void visit(UserTypeList userTypeList);
67 }
68 
69 

PofConfigVisitorImpl

  1 /*
  2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3  *
  4  * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
  5  *
  6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7  * Other names may be trademarks of their respective owners.
  8  *
  9  * The contents of this file are subject to the terms of either the GNU
 10  * General Public License Version 2 only ("GPL") or the Common
 11  * Development and Distribution License("CDDL") (collectively, the
 12  * "License"). You may not use this file except in compliance with the
 13  * License. You can obtain a copy of the License at
 14  * http://www.netbeans.org/cddl-gplv2.html
 15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 16  * specific language governing permissions and limitations under the
 17  * License.  When distributing the software, include this License Header
 18  * Notice in each file and include the License file at
 19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
 20  * particular file as subject to the "Classpath" exception as provided
 21  * by Oracle in the GPL Version 2 section of the License file that
 22  * accompanied this code. If applicable, add the following below the
 23  * License Header, with the fields enclosed by brackets [] replaced by
 24  * your own identifying information:
 25  * "Portions Copyrighted [year] [name of copyright owner]"
 26  *
 27  * If you wish your version of this file to be governed by only the CDDL
 28  * or only the GPL Version 2, indicate your decision by adding
 29  * "[Contributor] elects to include this software in this distribution
 30  * under the [CDDL or GPL Version 2] license." If you do not indicate a
 31  * single choice of license, a recipient has the option to distribute
 32  * your version of this file under either the CDDL, the GPL Version 2 or
 33  * to extend the choice of license to its licensees as provided above.
 34  * However, if you add GPL Version 2 code and therefore, elected the GPL
 35  * Version 2 license, then the option applies only if the new code is
 36  * made subject to such option by the copyright holder.
 37  *
 38  * Contributor(s):
 39  *
 40  * Portions Copyrighted 2011 Sun Microsystems, Inc.
 41  */
 42 package org.netbeans.modules.coherence.xml.pof.impl;
 43 
 44 import javax.xml.namespace.QName;
 45 import org.netbeans.modules.coherence.xml.pof.AllowInterfaces;
 46 import org.netbeans.modules.coherence.xml.pof.AllowSubclasses;
 47 import org.netbeans.modules.coherence.xml.pof.ClassName;
 48 import org.netbeans.modules.coherence.xml.pof.DefaultSerializer;
 49 import org.netbeans.modules.coherence.xml.pof.Include;
 50 import org.netbeans.modules.coherence.xml.pof.InitParam;
 51 import org.netbeans.modules.coherence.xml.pof.InitParams;
 52 import org.netbeans.modules.coherence.xml.pof.ParamType;
 53 import org.netbeans.modules.coherence.xml.pof.ParamValue;
 54 import org.netbeans.modules.coherence.xml.pof.PofConfig;
 55 import org.netbeans.modules.coherence.xml.pof.PofConfigComponent;
 56 import org.netbeans.modules.coherence.xml.pof.PofConfigVisitor;
 57 import org.netbeans.modules.coherence.xml.pof.Serializer;
 58 import org.netbeans.modules.coherence.xml.pof.SerializerType;
 59 import org.netbeans.modules.coherence.xml.pof.TypeId;
 60 import org.netbeans.modules.coherence.xml.pof.UserType;
 61 import org.netbeans.modules.coherence.xml.pof.UserTypeList;
 62 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
 63 import org.w3c.dom.Element;
 64 
 65 /**
 66  *
 67  * @author Andrew Hopkinson (Oracle A-Team)
 68  */
 69 public class PofConfigVisitorImpl implements PofConfigVisitor {
 70 
 71     private PofConfigComponent myResult;
 72     private Element myElement;
 73     private PofConfigModelImpl myModel;
 74 
 75     private PofConfigModelImpl getModel() {
 76         return myModel;
 77     }
 78 
 79     private Element getElement() {
 80         return myElement;
 81     }
 82     
 83     private String getLocalName() {
 84         return getElement().getLocalName();
 85     }
 86 
 87     public void setResult(PofConfigComponent myResult) {
 88         this.myResult = myResult;
 89     }
 90     
 91     private boolean isOk(PofConfigElements elements) {
 92         return elements.getName().equals(getLocalName());
 93     }
 94     
 95     public PofConfigVisitorImpl(PofConfigModelImpl model) {
 96         myModel = model;
 97     }
 98 
 99     public void init() {
100         myResult = null;
101         myElement = null;
102     }
103 
104     PofConfigComponent create(PofConfigComponent context, Element element) {
105         QName qName = AbstractDocumentComponent.getQName(element);
106         if (!PofConfigComponent.NAMESPACE.equals(qName.getNamespaceURI())) {
107             return null;
108         }
109         if (context == null) {
110             return new PofConfigImpl(getModel(), element);
111         } else {
112             myElement = element;
113             context.accept(this);
114         }
115         return myResult;
116     }
117 
118     @Override
119     public void visit(PofConfig pofConfig) {
120         if (isOk(PofConfigElements.ALLOWINTERFACES)) setResult(new AllowInterfacesImpl(getModel(), getElement()));
121         else if (isOk(PofConfigElements.ALLOWSUBCLASSES)) setResult(new AllowSubclassesImpl(getModel(), getElement()));
122         else if (isOk(PofConfigElements.USERTYPELIST)) setResult(new UserTypeListImpl(getModel(), getElement()));
123         else if (isOk(PofConfigElements.DEFAULTSERIALIZER)) setResult(new DefaultSerializerImpl(getModel(), getElement()));
124     }
125 
126     @Override
127     public void visit(AllowInterfaces allowInterfaces) {
128     }
129 
130     @Override
131     public void visit(AllowSubclasses allowSubclasses) {
132     }
133 
134     @Override
135     public void visit(ClassName className) {
136     }
137 
138     @Override
139     public void visit(DefaultSerializer defaultSerializer) {
140         visit((SerializerType)defaultSerializer);
141     }
142 
143     @Override
144     public void visit(Include include) {
145     }
146 
147     @Override
148     public void visit(InitParam initParam) {
149         if (isOk(PofConfigElements.PARAMTYPE)) setResult(new ParamTypeImpl(getModel(), getElement()));
150         else if (isOk(PofConfigElements.PARAMVALUE)) setResult(new ParamValueImpl(getModel(), getElement()));
151     }
152 
153     @Override
154     public void visit(InitParams initParams) {
155         if (isOk(PofConfigElements.INITPARAM)) setResult(new InitParamImpl(getModel(), getElement()));
156     }
157 
158     @Override
159     public void visit(ParamType paramType) {
160     }
161 
162     @Override
163     public void visit(ParamValue paramValue) {
164     }
165 
166     @Override
167     public void visit(Serializer serializer) {
168         visit((SerializerType)serializer);
169     }
170 
171     @Override
172     public void visit(SerializerType serializerType) {
173         if (isOk(PofConfigElements.CLASSNAME)) setResult(new ClassNameImpl(getModel(), getElement()));
174         else if (isOk(PofConfigElements.INITPARAMS)) setResult(new InitParamsImpl(getModel(), getElement()));
175     }
176 
177     @Override
178     public void visit(TypeId typeId) {
179     }
180 
181     @Override
182     public void visit(UserType userType) {
183         if (isOk(PofConfigElements.TYPEID)) setResult(new TypeIdImpl(getModel(), getElement()));
184         else if (isOk(PofConfigElements.CLASSNAME)) setResult(new ClassNameImpl(getModel(), getElement()));
185         else if (isOk(PofConfigElements.SERIALIZER)) setResult(new SerializerImpl(getModel(), getElement()));
186     }
187 
188     @Override
189     public void visit(UserTypeList userTypeList) {
190         if (isOk(PofConfigElements.USERTYPE)) setResult(new UserTypeImpl(getModel(), getElement()));
191         else if (isOk(PofConfigElements.INCLUDE)) setResult(new IncludeImpl(getModel(), getElement()));
192     }
193 
194 }
195 
196 

SyncUpdateVisitor

Used to Synchronise DocumentObject and Model. It can be seen from the code that this Visitor also contains a set of visit methods, one for each Emelement, and that each of them execute roughly the same functionality. Firstly they test the the Parent is the correct Element / Component and then check to see if we are Adding or Deleting. If Adding the we set the appropriate component to the value passed whilst if deleting we simply pass null which will be taken by the implementing class to mean delete. The exception to this are the UserType and Include which are sub elements of the UserTypeList which has a repeating Choice element and as such we created the additional UserTypeListElement Interface. Therefore the UserType and Include visit methods call a common visitUserTypeListElement.

SyncUpdateVisitor

  1 /*
  2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3  *
  4  * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
  5  *
  6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7  * Other names may be trademarks of their respective owners.
  8  *
  9  * The contents of this file are subject to the terms of either the GNU
 10  * General Public License Version 2 only ("GPL") or the Common
 11  * Development and Distribution License("CDDL") (collectively, the
 12  * "License"). You may not use this file except in compliance with the
 13  * License. You can obtain a copy of the License at
 14  * http://www.netbeans.org/cddl-gplv2.html
 15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 16  * specific language governing permissions and limitations under the
 17  * License.  When distributing the software, include this License Header
 18  * Notice in each file and include the License file at
 19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
 20  * particular file as subject to the "Classpath" exception as provided
 21  * by Oracle in the GPL Version 2 section of the License file that
 22  * accompanied this code. If applicable, add the following below the
 23  * License Header, with the fields enclosed by brackets [] replaced by
 24  * your own identifying information:
 25  * "Portions Copyrighted [year] [name of copyright owner]"
 26  *
 27  * If you wish your version of this file to be governed by only the CDDL
 28  * or only the GPL Version 2, indicate your decision by adding
 29  * "[Contributor] elects to include this software in this distribution
 30  * under the [CDDL or GPL Version 2] license." If you do not indicate a
 31  * single choice of license, a recipient has the option to distribute
 32  * your version of this file under either the CDDL, the GPL Version 2 or
 33  * to extend the choice of license to its licensees as provided above.
 34  * However, if you add GPL Version 2 code and therefore, elected the GPL
 35  * Version 2 license, then the option applies only if the new code is
 36  * made subject to such option by the copyright holder.
 37  *
 38  * Contributor(s):
 39  *
 40  * Portions Copyrighted 2011 Sun Microsystems, Inc.
 41  */
 42 package org.netbeans.modules.coherence.xml.pof.impl;
 43 
 44 import org.netbeans.modules.coherence.xml.pof.AllowInterfaces;
 45 import org.netbeans.modules.coherence.xml.pof.AllowSubclasses;
 46 import org.netbeans.modules.coherence.xml.pof.ClassName;
 47 import org.netbeans.modules.coherence.xml.pof.DefaultSerializer;
 48 import org.netbeans.modules.coherence.xml.pof.Include;
 49 import org.netbeans.modules.coherence.xml.pof.InitParam;
 50 import org.netbeans.modules.coherence.xml.pof.InitParams;
 51 import org.netbeans.modules.coherence.xml.pof.ParamType;
 52 import org.netbeans.modules.coherence.xml.pof.ParamValue;
 53 import org.netbeans.modules.coherence.xml.pof.PofConfig;
 54 import org.netbeans.modules.coherence.xml.pof.PofConfigComponent;
 55 import org.netbeans.modules.coherence.xml.pof.PofConfigVisitor;
 56 import org.netbeans.modules.coherence.xml.pof.Serializer;
 57 import org.netbeans.modules.coherence.xml.pof.SerializerType;
 58 import org.netbeans.modules.coherence.xml.pof.TypeId;
 59 import org.netbeans.modules.coherence.xml.pof.UserType;
 60 import org.netbeans.modules.coherence.xml.pof.UserTypeList;
 61 import org.netbeans.modules.coherence.xml.pof.UserTypeListElement;
 62 import org.netbeans.modules.xml.xam.ComponentUpdater;
 63 import org.netbeans.modules.xml.xam.ComponentUpdater.Operation;
 64 
 65 /**
 66  *
 67  * @author Andrew Hopkinson (Oracle A-Team)
 68  */
 69 public class SyncUpdateVisitor implements ComponentUpdater<PofConfigComponent>, PofConfigVisitor {
 70 
 71     private PofConfigComponent myParent;
 72     private int myIndex;
 73     private Operation myOperation;
 74 
 75     public Operation getOperation() {
 76         return myOperation;
 77     }
 78 
 79     public int getIndex() {
 80         return myIndex;
 81     }
 82 
 83     public PofConfigComponent getParent() {
 84         return myParent;
 85     }
 86 
 87     private boolean isAdd() {
 88         return getOperation() == Operation.ADD;
 89     }
 90 
 91     private boolean isRemove() {
 92         return getOperation() == Operation.REMOVE;
 93     }
 94 
 95     @Override
 96     public void update(PofConfigComponent target, PofConfigComponent child, Operation operation) {
 97         update(target, child, -1, operation);
 98     }
 99 
100     @Override
101     public void update(PofConfigComponent target, PofConfigComponent child, int index, Operation operation) {
102         if (target != null && child != null
103                 && (operation == null || operation == Operation.ADD || operation == Operation.REMOVE)) {
104             myIndex = index;
105             myOperation = operation;
106             myParent = target;
107             child.accept(this);
108         }
109     }
110 
111     @Override
112     public void visit(PofConfig pofConfig) {
113         throw new UnsupportedOperationException("Not supported yet.");
114     }
115 
116     @Override
117     public void visit(AllowInterfaces element) {
118         if (getParent() instanceof PofConfig) {
119             PofConfig parent = (PofConfig) getParent();
120             if (isAdd()) {
121                 parent.setAllowInterfaces(element);
122             } else if (isRemove()) {
123                 parent.setAllowInterfaces(null);
124             }
125         }
126     }
127 
128     @Override
129     public void visit(AllowSubclasses element) {
130         if (getParent() instanceof PofConfig) {
131             PofConfig parent = (PofConfig) getParent();
132             if (isAdd()) {
133                 parent.setAllowSubclasses(element);
134             } else if (isRemove()) {
135                 parent.setAllowSubclasses(null);
136             }
137         }
138     }
139 
140     @Override
141     public void visit(ClassName element) {
142         if (getParent() instanceof SerializerType) {
143             SerializerType parent = (SerializerType) getParent();
144             if (isAdd()) {
145                 parent.setClassName(element);
146             } else if (isRemove()) {
147                 parent.setClassName(null);
148             }
149         } else if (getParent() instanceof UserType) {
150             UserType parent = (UserType) getParent();
151             if (isAdd()) {
152                 parent.setClassName(element);
153             } else if (isRemove()) {
154                 parent.setClassName(null);
155             }
156         }
157 
158     }
159 
160     @Override
161     public void visit(DefaultSerializer element) {
162         if (getParent() instanceof PofConfig) {
163             PofConfig parent = (PofConfig) getParent();
164             if (isAdd()) {
165                 parent.setDefaultSerializer(element);
166             } else if (isRemove()) {
167                 parent.setDefaultSerializer(null);
168             }
169         }
170     }
171 
172     @Override
173     public void visit(Include include) {
174         visitUserTypeListElement(include);
175     }
176 
177     @Override
178     public void visit(InitParam element) {
179         if (getParent() instanceof InitParams) {
180             InitParams parent = (InitParams) getParent();
181             if (isAdd()) {
182                 parent.addInitParam(getIndex(), element);
183             } else if (isRemove()) {
184                 parent.removeInitParam(element);
185             }
186         }
187     }
188 
189     @Override
190     public void visit(InitParams element) {
191         if (getParent() instanceof SerializerType) {
192             SerializerType parent = (SerializerType) getParent();
193             if (isAdd()) {
194                 parent.setInitParams(element);
195             } else if (isRemove()) {
196                 parent.setInitParams(null);
197             }
198         }
199     }
200 
201     @Override
202     public void visit(ParamType element) {
203         if (getParent() instanceof InitParam) {
204             InitParam parent = (InitParam) getParent();
205             if (isAdd()) {
206                 parent.setParamType(element);
207             } else if (isRemove()) {
208                 parent.setParamType(null);
209             }
210         }
211     }
212 
213     @Override
214     public void visit(ParamValue element) {
215         if (getParent() instanceof InitParam) {
216             InitParam parent = (InitParam) getParent();
217             if (isAdd()) {
218                 parent.setParamValue(element);
219             } else if (isRemove()) {
220                 parent.setParamValue(null);
221             }
222         }
223     }
224 
225     @Override
226     public void visit(Serializer element) {
227         if (getParent() instanceof UserType) {
228             UserType parent = (UserType) getParent();
229             if (isAdd()) {
230                 parent.setSerializer(element);
231             } else if (isRemove()) {
232                 parent.setSerializer(null);
233             }
234         }
235     }
236 
237     @Override
238     public void visit(SerializerType serializerType) {
239         throw new UnsupportedOperationException("Not supported yet.");
240     }
241 
242     @Override
243     public void visit(TypeId element) {
244         if (getParent() instanceof UserType) {
245             UserType parent = (UserType) getParent();
246             if (isAdd()) {
247                 parent.setTypeId(element);
248             } else if (isRemove()) {
249                 parent.setTypeId(null);
250             }
251         }
252     }
253 
254     @Override
255     public void visit(UserType userType) {
256         visitUserTypeListElement(userType);
257     }
258 
259     @Override
260     public void visit(UserTypeList element) {
261         if (getParent() instanceof PofConfig) {
262             PofConfig parent = (PofConfig) getParent();
263             if (isAdd()) {
264                 parent.setUserTypeList(element);
265             } else if (isRemove()) {
266                 parent.setUserTypeList(null);
267             }
268         }
269     }
270 
271     private void visitUserTypeListElement(UserTypeListElement element) {
272         try {
273             if (getParent() instanceof UserTypeList) {
274                 UserTypeList parent = (UserTypeList) getParent();
275                 if (isAdd()) {
276                     parent.addElement(getIndex(), element);
277                 } else if (isRemove()) {
278                     parent.removeElement(element);
279                 }
280             }
281         } catch (Exception e) {
282         }
283     }
284 }
285 
286 

PofConfigComponent & PofConfigComponentImpl

This Interface and its associated Abstract Class Implementation are the common underlying PofConfig Component. They contain a number of key methods and constants that allow all inheriting components to access the Namespace, for the XML, and Model. In addition they mandate that all inheriting classes implement a getTagName that will be used by the common PofConfigTextComponent etc to set the value of the Element.

PofConfigComponent

 1 /*
 2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 3  *
 4  * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
 5  *
 6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
 7  * Other names may be trademarks of their respective owners.
 8  *
 9  * The contents of this file are subject to the terms of either the GNU
10  * General Public License Version 2 only ("GPL") or the Common
11  * Development and Distribution License("CDDL") (collectively, the
12  * "License"). You may not use this file except in compliance with the
13  * License. You can obtain a copy of the License at
14  * http://www.netbeans.org/cddl-gplv2.html
15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16  * specific language governing permissions and limitations under the
17  * License.  When distributing the software, include this License Header
18  * Notice in each file and include the License file at
19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20  * particular file as subject to the "Classpath" exception as provided
21  * by Oracle in the GPL Version 2 section of the License file that
22  * accompanied this code. If applicable, add the following below the
23  * License Header, with the fields enclosed by brackets [] replaced by
24  * your own identifying information:
25  * "Portions Copyrighted [year] [name of copyright owner]"
26  *
27  * If you wish your version of this file to be governed by only the CDDL
28  * or only the GPL Version 2, indicate your decision by adding
29  * "[Contributor] elects to include this software in this distribution
30  * under the [CDDL or GPL Version 2] license." If you do not indicate a
31  * single choice of license, a recipient has the option to distribute
32  * your version of this file under either the CDDL, the GPL Version 2 or
33  * to extend the choice of license to its licensees as provided above.
34  * However, if you add GPL Version 2 code and therefore, elected the GPL
35  * Version 2 license, then the option applies only if the new code is
36  * made subject to such option by the copyright holder.
37  *
38  * Contributor(s):
39  *
40  * Portions Copyrighted 2011 Sun Microsystems, Inc.
41  */
42 package org.netbeans.modules.coherence.xml.pof;
43 
44 import org.netbeans.modules.xml.xam.dom.DocumentComponent;
45 
46 /**
47  *
48  * @author Andrew Hopkinson (Oracle A-Team)
49  */
50 public interface PofConfigComponent extends DocumentComponent<PofConfigComponent> {
51 
52     public String NAMESPACE = "http://xmlns.oracle.com/coherence/coherence-pof-config";
53     
54     public String getTagName();
55 
56     public PofConfigModel getModel();
57 
58     public Class<? extends PofConfigComponent> getComponentType();
59 
60     public void accept(PofConfigVisitor visitor);
61 }
62 
63 

PofConfigComponentImpl

 1 /*
 2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 3  *
 4  * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
 5  *
 6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
 7  * Other names may be trademarks of their respective owners.
 8  *
 9  * The contents of this file are subject to the terms of either the GNU
10  * General Public License Version 2 only ("GPL") or the Common
11  * Development and Distribution License("CDDL") (collectively, the
12  * "License"). You may not use this file except in compliance with the
13  * License. You can obtain a copy of the License at
14  * http://www.netbeans.org/cddl-gplv2.html
15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16  * specific language governing permissions and limitations under the
17  * License.  When distributing the software, include this License Header
18  * Notice in each file and include the License file at
19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20  * particular file as subject to the "Classpath" exception as provided
21  * by Oracle in the GPL Version 2 section of the License file that
22  * accompanied this code. If applicable, add the following below the
23  * License Header, with the fields enclosed by brackets [] replaced by
24  * your own identifying information:
25  * "Portions Copyrighted [year] [name of copyright owner]"
26  *
27  * If you wish your version of this file to be governed by only the CDDL
28  * or only the GPL Version 2, indicate your decision by adding
29  * "[Contributor] elects to include this software in this distribution
30  * under the [CDDL or GPL Version 2] license." If you do not indicate a
31  * single choice of license, a recipient has the option to distribute
32  * your version of this file under either the CDDL, the GPL Version 2 or
33  * to extend the choice of license to its licensees as provided above.
34  * However, if you add GPL Version 2 code and therefore, elected the GPL
35  * Version 2 license, then the option applies only if the new code is
36  * made subject to such option by the copyright holder.
37  *
38  * Contributor(s):
39  *
40  * Portions Copyrighted 2011 Sun Microsystems, Inc.
41  */
42 package org.netbeans.modules.coherence.xml.pof.impl;
43 
44 import java.util.List;
45 import org.netbeans.modules.coherence.xml.pof.PofConfigComponent;
46 import org.netbeans.modules.coherence.xml.pof.PofConfigModel;
47 import org.netbeans.modules.coherence.xml.pof.PofConfigVisitor;
48 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
49 import org.netbeans.modules.xml.xam.dom.Attribute;
50 import org.w3c.dom.Element;
51 import org.w3c.dom.Node;
52 import org.w3c.dom.NodeList;
53 
54 /**
55  *
56  * @author Andrew Hopkinson (Oracle A-Team)
57  */
58 public abstract class PofConfigComponentImpl extends AbstractDocumentComponent<PofConfigComponent> implements PofConfigComponent {
59 
60     public PofConfigComponentImpl(PofConfigModelImpl model, Element e) {
61         super(model, e);
62     }
63 
64     @Override
65     protected void populateChildren(List<PofConfigComponent> children) {
66         NodeList nl = getPeer().getChildNodes();
67         if (nl != null) {
68             for (int i = 0; i < nl.getLength(); i++) {
69                 Node n = nl.item(i);
70                 if (n instanceof Element) {
71                     PofConfigComponent comp = (PofConfigComponent) getModel().getFactory().createComponent((Element) n, this);
72                     if (comp != null) {
73                         children.add(comp);
74                     }
75                 }
76             }
77         }
78     }
79 
80     @Override
81     protected Object getAttributeValueOf(Attribute atrbt, String string) {
82         return null;
83     }
84 
85     @Override
86     public PofConfigModelImpl getModel() {
87         return (PofConfigModelImpl)super.getModel();
88     }
89 
90     protected static Element createNewElement(String name, PofConfigModelImpl model){
91         return model.getDocument().createElementNS( NAMESPACE, name );
92     }
93     
94 }
95 
96 

No comments:

Post a Comment