1 /*
2 * Copyright (C) Christian Schulte, 2005-206
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * o Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * o Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $JOMC: ModelHelper.java 3838 2011-10-08 20:15:41Z schulte2005 $
29 *
30 */
31 package org.jomc.model.modlet;
32
33 import java.text.MessageFormat;
34 import java.util.Locale;
35 import java.util.ResourceBundle;
36 import javax.xml.bind.JAXBElement;
37 import org.jomc.model.ModelObject;
38 import org.jomc.model.Modules;
39 import org.jomc.model.ObjectFactory;
40 import org.jomc.modlet.Model;
41
42 /**
43 * Object management and configuration {@code Model} helper.
44 *
45 * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
46 * @version $JOMC: ModelHelper.java 3838 2011-10-08 20:15:41Z schulte2005 $
47 */
48 public abstract class ModelHelper
49 {
50
51 /** Creates a new {@code ModelHelper} instance. */
52 public ModelHelper()
53 {
54 super();
55 }
56
57 /**
58 * Gets the {@code Modules} of a {@code Model}.
59 *
60 * @param model The {@code Model} to get {@code Modules} of.
61 *
62 * @return The {@code Modules} of {@code Model} or {@code null}.
63 *
64 * @throws NullPointerException if {@code model} is {@code null}.
65 *
66 * @see #addModules(org.jomc.modlet.Model, org.jomc.model.Modules)
67 * @see #setModules(org.jomc.modlet.Model, org.jomc.model.Modules)
68 */
69 public static Modules getModules( final Model model )
70 {
71 if ( model == null )
72 {
73 throw new NullPointerException( "model" );
74 }
75
76 final JAXBElement<Modules> e = model.getAnyElement( ModelObject.MODEL_PUBLIC_ID, "modules", Modules.class );
77 return e != null ? e.getValue() : null;
78 }
79
80 /**
81 * Sets the {@code Modules} of a {@code Model}.
82 *
83 * @param model The {@code Model} to set {@code modules} of.
84 * @param modules The {@code Modules} to set.
85 *
86 * @throws NullPointerException if {@code model} or {@code modules} is {@code null}.
87 * @throws IllegalStateException if {@code model} already holds {@code Modules}.
88 *
89 * @see #addModules(org.jomc.modlet.Model, org.jomc.model.Modules)
90 * @see #removeModules(org.jomc.modlet.Model)
91 */
92 public static void setModules( final Model model, final Modules modules )
93 {
94 if ( model == null )
95 {
96 throw new NullPointerException( "model" );
97 }
98 if ( modules == null )
99 {
100 throw new NullPointerException( "modules" );
101 }
102 if ( getModules( model ) != null )
103 {
104 throw new IllegalStateException( getMessage( "illegalState", model.getIdentifier() ) );
105 }
106
107 model.getAny().add( new ObjectFactory().createModules( modules ) );
108 }
109
110 /**
111 * Adds {@code Modules} to a {@code Model}.
112 *
113 * @param model The {@code Model} to add {@code modules} to.
114 * @param modules The {@code Modules} to add to {@code model}.
115 *
116 * @throws NullPointerException if {@code model} or {@code modules} is {@code null}.
117 *
118 * @see #removeModules(org.jomc.modlet.Model)
119 * @see #setModules(org.jomc.modlet.Model, org.jomc.model.Modules)
120 */
121 public static void addModules( final Model model, final Modules modules )
122 {
123 if ( model == null )
124 {
125 throw new NullPointerException( "model" );
126 }
127 if ( modules == null )
128 {
129 throw new NullPointerException( "modules" );
130 }
131
132 final Modules current = getModules( model );
133
134 if ( current != null )
135 {
136 current.getModule().addAll( modules.getModule() );
137 }
138 else
139 {
140 setModules( model, modules );
141 }
142 }
143
144 /**
145 * Removes {@code Modules} from a {@code Model}.
146 *
147 * @param model The {@code Model} to remove {@code modules} from.
148 *
149 * @throws NullPointerException if {@code model} is {@code null}.
150 *
151 * @see #addModules(org.jomc.modlet.Model, org.jomc.model.Modules)
152 * @see #setModules(org.jomc.modlet.Model, org.jomc.model.Modules)
153 */
154 public static void removeModules( final Model model )
155 {
156 if ( model == null )
157 {
158 throw new NullPointerException( "model" );
159 }
160
161 final JAXBElement<Modules> e = model.getAnyElement( ModelObject.MODEL_PUBLIC_ID, "modules", Modules.class );
162
163 if ( e != null )
164 {
165 model.getAny().remove( e );
166 }
167 }
168
169 private static String getMessage( final String key, final Object... args )
170 {
171 return MessageFormat.format( ResourceBundle.getBundle(
172 ModelHelper.class.getName().replace( '.', '/' ), Locale.getDefault() ).getString( key ), args );
173
174 }
175
176 }