1 /*
   2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.internal.xjc.model;
  27 
  28 import java.util.ArrayList;
  29 import java.util.Collection;
  30 
  31 import com.sun.tools.internal.xjc.Plugin;
  32 
  33 /**
  34  * Represents the list of {@link CPluginCustomization}s attached to a JAXB model component.
  35  *
  36  * <p>
  37  * When {@link Plugin}s register the customization namespace URIs through {@link Plugin#getCustomizationURIs()},
  38  * XJC will treat those URIs just like XJC's own extension "http://java.sun.com/xml/ns/xjc" and make them
  39  * available as DOM nodes through {@link CPluginCustomization}. A {@link Plugin} can then access
  40  * this information to change its behavior.
  41  *
  42  * @author Kohsuke Kawaguchi
  43  */
  44 public final class CCustomizations extends ArrayList<CPluginCustomization> {
  45 
  46     /**
  47      * All {@link CCustomizations} used by a {@link Model} form a single linked list
  48      * so that we can look for unacknowledged customizations later.
  49      *
  50      * @see CPluginCustomization#markAsAcknowledged()
  51      * @see #setParent(Model,CCustomizable)
  52      */
  53     /*package*/ CCustomizations next;
  54 
  55     /**
  56      * The owner model component that carries these customizations.
  57      */
  58     private CCustomizable owner;
  59 
  60     public CCustomizations() {
  61     }
  62 
  63     public CCustomizations(Collection<? extends CPluginCustomization> cPluginCustomizations) {
  64         super(cPluginCustomizations);
  65     }
  66 
  67     /*package*/ void setParent(Model model,CCustomizable owner) {
  68         if(this.owner!=null)     return;
  69 
  70 //        // loop check
  71 //        for( CCustomizations c = model.customizations; c!=null; c=c.next )
  72 //            assert c!=this;
  73 
  74         this.next = model.customizations;
  75         model.customizations = this;
  76         assert owner!=null;
  77         this.owner = owner;
  78     }
  79 
  80     /**
  81      * Gets the model component that carries this customization.
  82      *
  83      * @return never null.
  84      */
  85     public CCustomizable getOwner() {
  86         assert owner!=null;
  87         return owner;
  88     }
  89 
  90     /**
  91      * Finds the first {@link CPluginCustomization} that belongs to the given namespace URI.
  92      * @return null if not found
  93      */
  94     public CPluginCustomization find( String nsUri ) {
  95         for (CPluginCustomization p : this) {
  96             if(fixNull(p.element.getNamespaceURI()).equals(nsUri))
  97                 return p;
  98         }
  99         return null;
 100     }
 101 
 102     /**
 103      * Finds the first {@link CPluginCustomization} that belongs to the given namespace URI and the local name.
 104      * @return null if not found
 105      */
 106     public CPluginCustomization find( String nsUri, String localName ) {
 107         for (CPluginCustomization p : this) {
 108             if(fixNull(p.element.getNamespaceURI()).equals(nsUri)
 109             && fixNull(p.element.getLocalName()).equals(localName))
 110                 return p;
 111         }
 112         return null;
 113     }
 114 
 115     private String fixNull(String s) {
 116         if(s==null) return "";
 117         else        return s;
 118     }
 119 
 120     /**
 121      * Convenient singleton instance that represents an empty {@link CCustomizations}.
 122      */
 123     public static final CCustomizations EMPTY = new CCustomizations();
 124 
 125     /**
 126      * Merges two {@link CCustomizations} objects into one.
 127      */
 128     public static CCustomizations merge(CCustomizations lhs, CCustomizations rhs) {
 129         if(lhs==null || lhs.isEmpty())   return rhs;
 130         if(rhs==null || rhs.isEmpty())   return lhs;
 131 
 132         CCustomizations r = new CCustomizations(lhs);
 133         r.addAll(rhs);
 134         return r;
 135     }
 136 
 137     public boolean equals(Object o) {
 138         return this==o;
 139     }
 140 
 141     public int hashCode() {
 142         return System.identityHashCode(this);
 143     }
 144 }