001/* 002 * Copyright (C) Christian Schulte <cs@schulte.it>, 2005-206 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without 006 * modification, are permitted provided that the following conditions 007 * are met: 008 * 009 * o Redistributions of source code must retain the above copyright 010 * notice, this list of conditions and the following disclaimer. 011 * 012 * o Redistributions in binary form must reproduce the above copyright 013 * notice, this list of conditions and the following disclaimer in 014 * the documentation and/or other materials provided with the 015 * distribution. 016 * 017 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 018 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 019 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 020 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, 021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 022 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 023 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 024 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 026 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 027 * 028 * $JOMC: ModelHelper.java 5043 2015-05-27 07:03:39Z schulte $ 029 * 030 */ 031package org.jomc.model.modlet; 032 033import java.text.MessageFormat; 034import java.util.Locale; 035import java.util.ResourceBundle; 036import javax.xml.bind.JAXBElement; 037import org.jomc.model.ModelObject; 038import org.jomc.model.Modules; 039import org.jomc.model.ObjectFactory; 040import org.jomc.modlet.Model; 041 042/** 043 * Object management and configuration {@code Model} helper. 044 * 045 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 046 * @version $JOMC: ModelHelper.java 5043 2015-05-27 07:03:39Z schulte $ 047 */ 048public abstract class ModelHelper 049{ 050 051 /** 052 * Creates a new {@code ModelHelper} instance. 053 */ 054 public ModelHelper() 055 { 056 super(); 057 } 058 059 /** 060 * Gets the {@code Modules} of a {@code Model}. 061 * 062 * @param model The {@code Model} to get {@code Modules} of. 063 * 064 * @return The {@code Modules} of {@code Model} or {@code null}. 065 * 066 * @throws NullPointerException if {@code model} is {@code null}. 067 * 068 * @see #addModules(org.jomc.modlet.Model, org.jomc.model.Modules) 069 * @see #setModules(org.jomc.modlet.Model, org.jomc.model.Modules) 070 */ 071 public static Modules getModules( final Model model ) 072 { 073 if ( model == null ) 074 { 075 throw new NullPointerException( "model" ); 076 } 077 078 final JAXBElement<Modules> e = model.getAnyElement( ModelObject.MODEL_PUBLIC_ID, "modules", Modules.class ); 079 return e != null ? e.getValue() : null; 080 } 081 082 /** 083 * Sets the {@code Modules} of a {@code Model}. 084 * 085 * @param model The {@code Model} to set {@code modules} of. 086 * @param modules The {@code Modules} to set. 087 * 088 * @throws NullPointerException if {@code model} or {@code modules} is {@code null}. 089 * @throws IllegalStateException if {@code model} already holds {@code Modules}. 090 * 091 * @see #addModules(org.jomc.modlet.Model, org.jomc.model.Modules) 092 * @see #removeModules(org.jomc.modlet.Model) 093 */ 094 public static void setModules( final Model model, final Modules modules ) 095 { 096 if ( model == null ) 097 { 098 throw new NullPointerException( "model" ); 099 } 100 if ( modules == null ) 101 { 102 throw new NullPointerException( "modules" ); 103 } 104 if ( getModules( model ) != null ) 105 { 106 throw new IllegalStateException( getMessage( "illegalState", model.getIdentifier() ) ); 107 } 108 109 model.getAny().add( new ObjectFactory().createModules( modules ) ); 110 } 111 112 /** 113 * Adds {@code Modules} to a {@code Model}. 114 * 115 * @param model The {@code Model} to add {@code modules} to. 116 * @param modules The {@code Modules} to add to {@code model}. 117 * 118 * @throws NullPointerException if {@code model} or {@code modules} is {@code null}. 119 * 120 * @see #removeModules(org.jomc.modlet.Model) 121 * @see #setModules(org.jomc.modlet.Model, org.jomc.model.Modules) 122 */ 123 public static void addModules( final Model model, final Modules modules ) 124 { 125 if ( model == null ) 126 { 127 throw new NullPointerException( "model" ); 128 } 129 if ( modules == null ) 130 { 131 throw new NullPointerException( "modules" ); 132 } 133 134 final Modules current = getModules( model ); 135 136 if ( current != null ) 137 { 138 current.getModule().addAll( modules.getModule() ); 139 } 140 else 141 { 142 setModules( model, modules ); 143 } 144 } 145 146 /** 147 * Removes {@code Modules} from a {@code Model}. 148 * 149 * @param model The {@code Model} to remove {@code modules} from. 150 * 151 * @throws NullPointerException if {@code model} is {@code null}. 152 * 153 * @see #addModules(org.jomc.modlet.Model, org.jomc.model.Modules) 154 * @see #setModules(org.jomc.modlet.Model, org.jomc.model.Modules) 155 */ 156 public static void removeModules( final Model model ) 157 { 158 if ( model == null ) 159 { 160 throw new NullPointerException( "model" ); 161 } 162 163 final JAXBElement<Modules> e = model.getAnyElement( ModelObject.MODEL_PUBLIC_ID, "modules", Modules.class ); 164 165 if ( e != null ) 166 { 167 model.getAny().remove( e ); 168 } 169 } 170 171 private static String getMessage( final String key, final Object... args ) 172 { 173 return MessageFormat.format( ResourceBundle.getBundle( 174 ModelHelper.class.getName().replace( '.', '/' ), Locale.getDefault() ).getString( key ), args ); 175 176 } 177 178}