ModelHelper.java

  1. /*
  2.  *   Copyright (C) Christian Schulte <cs@schulte.it>, 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 5043 2015-05-27 07:03:39Z schulte $
  29.  *
  30.  */
  31. package org.jomc.model.modlet;

  32. import java.text.MessageFormat;
  33. import java.util.Locale;
  34. import java.util.ResourceBundle;
  35. import javax.xml.bind.JAXBElement;
  36. import org.jomc.model.ModelObject;
  37. import org.jomc.model.Modules;
  38. import org.jomc.model.ObjectFactory;
  39. import org.jomc.modlet.Model;

  40. /**
  41.  * Object management and configuration {@code Model} helper.
  42.  *
  43.  * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
  44.  * @version $JOMC: ModelHelper.java 5043 2015-05-27 07:03:39Z schulte $
  45.  */
  46. public abstract class ModelHelper
  47. {

  48.     /**
  49.      * Creates a new {@code ModelHelper} instance.
  50.      */
  51.     public ModelHelper()
  52.     {
  53.         super();
  54.     }

  55.     /**
  56.      * Gets the {@code Modules} of a {@code Model}.
  57.      *
  58.      * @param model The {@code Model} to get {@code Modules} of.
  59.      *
  60.      * @return The {@code Modules} of {@code Model} or {@code null}.
  61.      *
  62.      * @throws NullPointerException if {@code model} is {@code null}.
  63.      *
  64.      * @see #addModules(org.jomc.modlet.Model, org.jomc.model.Modules)
  65.      * @see #setModules(org.jomc.modlet.Model, org.jomc.model.Modules)
  66.      */
  67.     public static Modules getModules( final Model model )
  68.     {
  69.         if ( model == null )
  70.         {
  71.             throw new NullPointerException( "model" );
  72.         }

  73.         final JAXBElement<Modules> e = model.getAnyElement( ModelObject.MODEL_PUBLIC_ID, "modules", Modules.class );
  74.         return e != null ? e.getValue() : null;
  75.     }

  76.     /**
  77.      * Sets the {@code Modules} of a {@code Model}.
  78.      *
  79.      * @param model The {@code Model} to set {@code modules} of.
  80.      * @param modules The {@code Modules} to set.
  81.      *
  82.      * @throws NullPointerException if {@code model} or {@code modules} is {@code null}.
  83.      * @throws IllegalStateException if {@code model} already holds {@code Modules}.
  84.      *
  85.      * @see #addModules(org.jomc.modlet.Model, org.jomc.model.Modules)
  86.      * @see #removeModules(org.jomc.modlet.Model)
  87.      */
  88.     public static void setModules( final Model model, final Modules modules )
  89.     {
  90.         if ( model == null )
  91.         {
  92.             throw new NullPointerException( "model" );
  93.         }
  94.         if ( modules == null )
  95.         {
  96.             throw new NullPointerException( "modules" );
  97.         }
  98.         if ( getModules( model ) != null )
  99.         {
  100.             throw new IllegalStateException( getMessage( "illegalState", model.getIdentifier() ) );
  101.         }

  102.         model.getAny().add( new ObjectFactory().createModules( modules ) );
  103.     }

  104.     /**
  105.      * Adds {@code Modules} to a {@code Model}.
  106.      *
  107.      * @param model The {@code Model} to add {@code modules} to.
  108.      * @param modules The {@code Modules} to add to {@code model}.
  109.      *
  110.      * @throws NullPointerException if {@code model} or {@code modules} is {@code null}.
  111.      *
  112.      * @see #removeModules(org.jomc.modlet.Model)
  113.      * @see #setModules(org.jomc.modlet.Model, org.jomc.model.Modules)
  114.      */
  115.     public static void addModules( final Model model, final Modules modules )
  116.     {
  117.         if ( model == null )
  118.         {
  119.             throw new NullPointerException( "model" );
  120.         }
  121.         if ( modules == null )
  122.         {
  123.             throw new NullPointerException( "modules" );
  124.         }

  125.         final Modules current = getModules( model );

  126.         if ( current != null )
  127.         {
  128.             current.getModule().addAll( modules.getModule() );
  129.         }
  130.         else
  131.         {
  132.             setModules( model, modules );
  133.         }
  134.     }

  135.     /**
  136.      * Removes {@code Modules} from a {@code Model}.
  137.      *
  138.      * @param model The {@code Model} to remove {@code modules} from.
  139.      *
  140.      * @throws NullPointerException if {@code model} is {@code null}.
  141.      *
  142.      * @see #addModules(org.jomc.modlet.Model, org.jomc.model.Modules)
  143.      * @see #setModules(org.jomc.modlet.Model, org.jomc.model.Modules)
  144.      */
  145.     public static void removeModules( final Model model )
  146.     {
  147.         if ( model == null )
  148.         {
  149.             throw new NullPointerException( "model" );
  150.         }

  151.         final JAXBElement<Modules> e = model.getAnyElement( ModelObject.MODEL_PUBLIC_ID, "modules", Modules.class );

  152.         if ( e != null )
  153.         {
  154.             model.getAny().remove( e );
  155.         }
  156.     }

  157.     private static String getMessage( final String key, final Object... args )
  158.     {
  159.         return MessageFormat.format( ResourceBundle.getBundle(
  160.             ModelHelper.class.getName().replace( '.', '/' ), Locale.getDefault() ).getString( key ), args );

  161.     }

  162. }