View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.jcr;
25  
26  import javax.jcr.nodetype.ConstraintViolationException;
27  import javax.jcr.nodetype.NodeType;
28  import net.jcip.annotations.NotThreadSafe;
29  import org.modeshape.graph.ExecutionContext;
30  import org.modeshape.graph.property.Name;
31  import org.modeshape.graph.property.NameFactory;
32  import org.modeshape.graph.property.NamespaceRegistry;
33  import org.modeshape.graph.property.ValueFormatException;
34  import org.modeshape.jcr.nodetype.NodeDefinitionTemplate;
35  
36  /**
37   * ModeShape implementation of the JCR 2 NodeDefinitionTemplate interface
38   */
39  @NotThreadSafe
40  class JcrNodeDefinitionTemplate extends JcrItemDefinitionTemplate implements NodeDefinitionTemplate {
41  
42      private Name defaultPrimaryType;
43      private Name[] requiredPrimaryTypes;
44      private boolean allowSameNameSiblings;
45  
46      JcrNodeDefinitionTemplate( ExecutionContext context ) {
47          super(context);
48      }
49  
50      JcrNodeDefinitionTemplate( JcrNodeDefinitionTemplate original,
51                                 ExecutionContext context ) {
52          super(original, context);
53          this.defaultPrimaryType = original.defaultPrimaryType;
54          this.requiredPrimaryTypes = original.requiredPrimaryTypes;
55          this.allowSameNameSiblings = original.allowSameNameSiblings;
56          JcrItemDefinitionTemplate.registerMissingNamespaces(original.getContext(), context, this.defaultPrimaryType);
57          JcrItemDefinitionTemplate.registerMissingNamespaces(original.getContext(), context, this.requiredPrimaryTypes);
58      }
59  
60      JcrNodeDefinitionTemplate with( ExecutionContext context ) {
61          return context == super.getContext() ? this : new JcrNodeDefinitionTemplate(this, context);
62      }
63  
64      /**
65       * {@inheritDoc}
66       * <p>
67       * Passing a null or blank name is equivalent to "unsetting" (or removing) the primary item name.
68       * </p>
69       * 
70       * @see org.modeshape.jcr.nodetype.NodeDefinitionTemplate#setDefaultPrimaryType(String)
71       */
72      public void setDefaultPrimaryType( String defaultPrimaryType ) throws ConstraintViolationException {
73          setDefaultPrimaryTypeName(defaultPrimaryType);
74      }
75  
76      /**
77       * Set the name of the primary type that should be used by default when creating children using this node definition
78       * 
79       * @param defaultPrimaryType the default primary type for this child node, or null if there is to be no default primary type
80       * @throws ConstraintViolationException
81       */
82      public void setDefaultPrimaryTypeName( String defaultPrimaryType ) throws ConstraintViolationException {
83          if (defaultPrimaryType == null || defaultPrimaryType.trim().length() == 0) {
84              this.defaultPrimaryType = null;
85          } else {
86              try {
87                  this.defaultPrimaryType = getContext().getValueFactories().getNameFactory().create(defaultPrimaryType);
88              } catch (ValueFormatException vfe) {
89                  throw new ConstraintViolationException(vfe);
90              }
91          }
92      }
93  
94      /**
95       * {@inheritDoc}
96       * 
97       * @see org.modeshape.jcr.nodetype.NodeDefinitionTemplate#setRequiredPrimaryTypes(java.lang.String[])
98       * @deprecated As of ModeShape 2.0, use {@link #setRequiredPrimaryTypeNames(String[])} instead
99       */
100     @SuppressWarnings( "dep-ann" )
101     public void setRequiredPrimaryTypes( String[] requiredPrimaryTypes ) throws ConstraintViolationException {
102         setRequiredPrimaryTypeNames(requiredPrimaryTypes);
103     }
104 
105     /**
106      * {@inheritDoc}
107      * 
108      * @see NodeDefinitionTemplate#setRequiredPrimaryTypeNames(String[])
109      */
110     public void setRequiredPrimaryTypeNames( String[] requiredPrimaryTypes ) throws ConstraintViolationException {
111         if (requiredPrimaryTypes == null) {
112             throw new ConstraintViolationException(JcrI18n.badNodeTypeName.text("requiredPrimaryTypes"));
113         }
114 
115         NameFactory nameFactory = getContext().getValueFactories().getNameFactory();
116         Name[] rpts = new Name[requiredPrimaryTypes.length];
117         for (int i = 0; i < requiredPrimaryTypes.length; i++) {
118             try {
119                 rpts[i] = nameFactory.create(requiredPrimaryTypes[i]);
120             } catch (ValueFormatException vfe) {
121                 throw new ConstraintViolationException(vfe);
122             }
123         }
124 
125         this.requiredPrimaryTypes = rpts;
126     }
127 
128     /**
129      * {@inheritDoc}
130      * 
131      * @see org.modeshape.jcr.nodetype.NodeDefinitionTemplate#setSameNameSiblings(boolean)
132      */
133     public void setSameNameSiblings( boolean allowSameNameSiblings ) {
134         this.allowSameNameSiblings = allowSameNameSiblings;
135     }
136 
137     /**
138      * {@inheritDoc}
139      * 
140      * @see javax.jcr.nodetype.NodeDefinition#allowsSameNameSiblings()
141      */
142     public boolean allowsSameNameSiblings() {
143         return allowSameNameSiblings;
144     }
145 
146     /**
147      * {@inheritDoc}
148      * 
149      * @see javax.jcr.nodetype.NodeDefinition#getDefaultPrimaryType()
150      */
151     public NodeType getDefaultPrimaryType() {
152         return null;
153     }
154 
155     public String getDefaultPrimaryTypeName() {
156         if (defaultPrimaryType == null) return null;
157         return defaultPrimaryType.getString(getContext().getNamespaceRegistry());
158     }
159 
160     /**
161      * {@inheritDoc}
162      * 
163      * @see javax.jcr.nodetype.NodeDefinition#getRequiredPrimaryTypes()
164      */
165     public NodeType[] getRequiredPrimaryTypes() {
166         // This method should return null since it's not attached to a registered node type ...
167         return null;
168     }
169 
170     /**
171      * {@inheritDoc}
172      * 
173      * @see javax.jcr.nodetype.NodeDefinition#getRequiredPrimaryTypeNames()
174      */
175     public String[] getRequiredPrimaryTypeNames() {
176         if (requiredPrimaryTypes == null) return null;
177 
178         NamespaceRegistry registry = getContext().getNamespaceRegistry();
179         String[] rpts = new String[requiredPrimaryTypes.length];
180         for (int i = 0; i < requiredPrimaryTypes.length; i++) {
181             rpts[i] = requiredPrimaryTypes[i].getString(registry);
182         }
183         return rpts;
184     }
185 
186     /**
187      * {@inheritDoc}
188      * 
189      * @see javax.jcr.nodetype.NodeDefinitionTemplate#setName(java.lang.String)
190      */
191     @Override
192     public void setName( String name ) throws ConstraintViolationException {
193         super.setName(name);
194     }
195 
196     /**
197      * {@inheritDoc}
198      * 
199      * @see javax.jcr.nodetype.NodeDefinitionTemplate#setAutoCreated(boolean)
200      */
201     @Override
202     public void setAutoCreated( boolean autoCreated ) {
203         super.setAutoCreated(autoCreated);
204     }
205 
206     /**
207      * {@inheritDoc}
208      * 
209      * @see javax.jcr.nodetype.NodeDefinitionTemplate#setMandatory(boolean)
210      */
211     @Override
212     public void setMandatory( boolean mandatory ) {
213         super.setMandatory(mandatory);
214     }
215 
216     /**
217      * {@inheritDoc}
218      * 
219      * @see javax.jcr.nodetype.NodeDefinitionTemplate#setOnParentVersion(int)
220      */
221     @Override
222     public void setOnParentVersion( int onParentVersion ) {
223         super.setOnParentVersion(onParentVersion);
224     }
225 
226     /**
227      * {@inheritDoc}
228      * 
229      * @see javax.jcr.nodetype.NodeDefinitionTemplate#setProtected(boolean)
230      */
231     @Override
232     public void setProtected( boolean isProtected ) {
233         super.setProtected(isProtected);
234     }
235 }