001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.naming.deployment.jsr88;
017:
018: import javax.xml.namespace.QName;
019: import org.apache.geronimo.deployment.plugin.XmlBeanSupport;
020: import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
021: import org.apache.xmlbeans.XmlObject;
022: import org.apache.xmlbeans.impl.values.XmlObjectBase;
023:
024: /**
025: * Represents an element in a Geronimo dployment plan that has a child
026: * of type Pattern. This handles patterns that are a member of a choice as
027: * well as singleton patterns.
028: * <p>
029: * Has 1 JavaBean Properties <br />
030: * - pattern (type Pattern) </p>
031: *
032: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
033: */
034: public class HasPattern extends XmlBeanSupport {
035: public HasPattern() {
036: super (null);
037: }
038:
039: public HasPattern(XmlObject xmlObject) {
040: super (xmlObject);
041: }
042:
043: /**
044: * JavaBean getter for the Pattern property. Gets a JavaBean of type
045: * Pattern for the pattern child of this element, or null if there is no
046: * pattern child.
047: */
048: public Pattern getPattern() {
049: GerPatternType patternType = findPattern();
050: if (patternType == null)
051: return null;
052: Pattern group = new Pattern();
053: group.setGroupId(patternType.getGroupId());
054: group.setArtifactId(patternType.getArtifactId());
055: group.setVersion(patternType.getVersion());
056: group.setModule(patternType.getModule());
057: group.setName(patternType.getName());
058: return group.empty() ? null : group;
059: }
060:
061: /**
062: * JavaBean setter for the Pattern property. Calls the helper
063: * clearNonPatternFromChoice if a non-null Pattern is set.
064: */
065: public void setPattern(Pattern group) {
066: Pattern old = getPattern();
067: if (group != null) {
068: GerPatternType patternType;
069: if (old == null) {
070: patternType = (GerPatternType) ((XmlObjectBase) getXmlObject())
071: .get_store()
072: .add_element_user(
073: new QName(
074: "http://geronimo.apache.org/xml/ns/naming-1.2",
075: "pattern"));
076: } else {
077: patternType = findPattern();
078: }
079: if (!isEmpty(group.getGroupId())) {
080: patternType.setGroupId(group.getGroupId());
081: } else {
082: if (patternType.isSetGroupId())
083: patternType.unsetGroupId();
084: }
085: if (!isEmpty(group.getArtifactId())) {
086: patternType.setArtifactId(group.getArtifactId());
087: } else {
088: if (patternType.isSetArtifactId())
089: patternType.unsetArtifactId();
090: }
091: if (!isEmpty(group.getModule())) {
092: patternType.setModule(group.getModule());
093: } else {
094: if (patternType.isSetModule())
095: patternType.unsetModule();
096: }
097: patternType.setName(group.getName());
098: if (!isEmpty(group.getVersion())) {
099: patternType.setVersion(group.getVersion());
100: } else {
101: if (patternType.isSetVersion())
102: patternType.unsetVersion();
103: }
104: clearNonPatternFromChoice();
105: } else {
106: if (old != null) {
107: ((XmlObjectBase) getXmlObject())
108: .get_store()
109: .remove_element(
110: new QName(
111: "http://geronimo.apache.org/xml/ns/naming-1.2",
112: "pattern"), 0);
113: }
114: }
115: pcs.firePropertyChange("objectNameComponents", old, group);
116: }
117:
118: /**
119: * Should be overridden to remove any non-pattern elements if this
120: * element has a pattern that's part of a choice. If this is called, it
121: * means a non-null Pattern is in the process of being set. This method
122: * should fire property change events on any elements it removes.
123: */
124: protected void clearNonPatternFromChoice() {
125: }
126:
127: /**
128: * Should be called to remove any pattern child element if the pattern is
129: * part of a choice and some other element in the choice was set to a
130: * non-null value. This will clear the pattern and send a property change
131: * event on the "pattern" property if the pattern was set.
132: */
133: protected void clearPatternFromChoice() {
134: Pattern pattern = getPattern();
135: if (pattern != null) {
136: ((XmlObjectBase) getXmlObject())
137: .get_store()
138: .remove_element(
139: new QName(
140: "http://geronimo.apache.org/xml/ns/naming-1.2",
141: "pattern"), 0);
142: pcs.firePropertyChange("pattern", pattern, null);
143: }
144: }
145:
146: /**
147: * Gets the pattern child of this element, or null if there is none.
148: */
149: protected GerPatternType findPattern() {
150: XmlObject[] patterns = getXmlObject().selectChildren(
151: new QName(GerPatternType.type.getName()
152: .getNamespaceURI(), "pattern"));
153: if (patterns.length == 0) {
154: return null;
155: }
156: return (GerPatternType) patterns[0];
157: }
158: }
|