001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.xjc.model;
038:
039: import java.util.ArrayList;
040: import java.util.Collection;
041:
042: import com.sun.tools.xjc.Plugin;
043:
044: /**
045: * Represents the list of {@link CPluginCustomization}s attached to a JAXB model component.
046: *
047: * <p>
048: * When {@link Plugin}s register the customization namespace URIs through {@link Plugin#getCustomizationURIs()},
049: * XJC will treat those URIs just like XJC's own extension "http://java.sun.com/xml/ns/xjc" and make them
050: * available as DOM nodes through {@link CPluginCustomization}. A {@link Plugin} can then access
051: * this information to change its behavior.
052: *
053: * @author Kohsuke Kawaguchi
054: */
055: public final class CCustomizations extends
056: ArrayList<CPluginCustomization> {
057:
058: /**
059: * All {@link CCustomizations} used by a {@link Model} form a single linked list
060: * so that we can look for unacknowledged customizations later.
061: *
062: * @see CPluginCustomization#markAsAcknowledged()
063: * @see #setParent(Model,CCustomizable)
064: */
065: /*package*/CCustomizations next;
066:
067: /**
068: * The owner model component that carries these customizations.
069: */
070: private CCustomizable owner;
071:
072: public CCustomizations() {
073: }
074:
075: public CCustomizations(
076: Collection<? extends CPluginCustomization> cPluginCustomizations) {
077: super (cPluginCustomizations);
078: }
079:
080: /*package*/void setParent(Model model, CCustomizable owner) {
081: if (this .owner != null)
082: return;
083:
084: // // loop check
085: // for( CCustomizations c = model.customizations; c!=null; c=c.next )
086: // assert c!=this;
087:
088: this .next = model.customizations;
089: model.customizations = this ;
090: assert owner != null;
091: this .owner = owner;
092: }
093:
094: /**
095: * Gets the model component that carries this customization.
096: *
097: * @return never null.
098: */
099: public CCustomizable getOwner() {
100: assert owner != null;
101: return owner;
102: }
103:
104: /**
105: * Finds the first {@link CPluginCustomization} that belongs to the given namespace URI.
106: * @return null if not found
107: */
108: public CPluginCustomization find(String nsUri) {
109: for (CPluginCustomization p : this ) {
110: if (fixNull(p.element.getNamespaceURI()).equals(nsUri))
111: return p;
112: }
113: return null;
114: }
115:
116: /**
117: * Finds the first {@link CPluginCustomization} that belongs to the given namespace URI and the local name.
118: * @return null if not found
119: */
120: public CPluginCustomization find(String nsUri, String localName) {
121: for (CPluginCustomization p : this ) {
122: if (fixNull(p.element.getNamespaceURI()).equals(nsUri)
123: && fixNull(p.element.getLocalName()).equals(
124: localName))
125: return p;
126: }
127: return null;
128: }
129:
130: private String fixNull(String s) {
131: if (s == null)
132: return "";
133: else
134: return s;
135: }
136:
137: /**
138: * Convenient singleton instance that represents an empty {@link CCustomizations}.
139: */
140: public static final CCustomizations EMPTY = new CCustomizations();
141:
142: /**
143: * Merges two {@link CCustomizations} objects into one.
144: */
145: public static CCustomizations merge(CCustomizations lhs,
146: CCustomizations rhs) {
147: if (lhs == null || lhs.isEmpty())
148: return rhs;
149: if (rhs == null || rhs.isEmpty())
150: return lhs;
151:
152: CCustomizations r = new CCustomizations(lhs);
153: r.addAll(rhs);
154: return r;
155: }
156:
157: public boolean equals(Object o) {
158: return this == o;
159: }
160:
161: public int hashCode() {
162: return System.identityHashCode(this);
163: }
164: }
|