001: /*****************************************************************************
002: * Java Plug-in Framework (JPF)
003: * Copyright (C) 2004-2007 Dmitry Olshansky
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *****************************************************************************/package org.java.plugin.registry.xml;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.java.plugin.registry.Documentation;
023: import org.java.plugin.registry.ManifestProcessingException;
024: import org.java.plugin.registry.MatchingRule;
025: import org.java.plugin.registry.PluginDescriptor;
026: import org.java.plugin.registry.PluginFragment;
027: import org.java.plugin.registry.PluginPrerequisite;
028: import org.java.plugin.registry.Version;
029:
030: /**
031: * @version $Id$
032: */
033: class PluginPrerequisiteImpl implements PluginPrerequisite {
034: private static Log log = LogFactory
035: .getLog(PluginPrerequisiteImpl.class);
036:
037: static boolean matches(final Version source, final Version target,
038: final MatchingRule match) {
039: if (source == null) {
040: return true;
041: }
042: switch (match) {
043: case EQUAL:
044: return target.equals(source);
045: case EQUIVALENT:
046: return target.isEquivalentTo(source);
047: case COMPATIBLE:
048: return target.isCompatibleWith(source);
049: case GREATER_OR_EQUAL:
050: return target.isGreaterOrEqualTo(source);
051: }
052: return target.isCompatibleWith(source);
053: }
054:
055: private final PluginDescriptorImpl descriptor;
056: private final PluginFragmentImpl fragment;
057: private final ModelPrerequisite model;
058: private DocumentationImpl<PluginPrerequisite> doc;
059:
060: PluginPrerequisiteImpl(final PluginDescriptorImpl descr,
061: final PluginFragmentImpl aFragment,
062: final ModelPrerequisite aModel)
063: throws ManifestProcessingException {
064: super ();
065: descriptor = descr;
066: fragment = aFragment;
067: model = aModel;
068: if ((model.getPluginId() == null)
069: || (model.getPluginId().trim().length() == 0)) {
070: throw new ManifestProcessingException(
071: PluginRegistryImpl.PACKAGE_NAME,
072: "prerequisitePliginIdIsBlank", descr.getId()); //$NON-NLS-1$
073: }
074: if (descr.getId().equals(model.getPluginId())) {
075: throw new ManifestProcessingException(
076: PluginRegistryImpl.PACKAGE_NAME,
077: "invalidPrerequisitePluginId", descr.getId()); //$NON-NLS-1$
078: }
079: if ((model.getId() == null)
080: || (model.getId().trim().length() == 0)) {
081: model.setId("prerequisite:" + model.getPluginId()); //$NON-NLS-1$
082: }
083: if (model.getDocumentation() != null) {
084: doc = new DocumentationImpl<PluginPrerequisite>(this , model
085: .getDocumentation());
086: }
087: if (log.isDebugEnabled()) {
088: log.debug("object instantiated: " + this ); //$NON-NLS-1$
089: }
090: }
091:
092: /**
093: * @see org.java.plugin.registry.PluginPrerequisite#getPluginId()
094: */
095: public String getPluginId() {
096: return model.getPluginId();
097: }
098:
099: /**
100: * @see org.java.plugin.registry.PluginPrerequisite#getPluginVersion()
101: */
102: public Version getPluginVersion() {
103: return model.getPluginVersion();
104: }
105:
106: /**
107: * @see org.java.plugin.registry.PluginPrerequisite#getDeclaringPluginDescriptor()
108: */
109: public PluginDescriptor getDeclaringPluginDescriptor() {
110: return descriptor;
111: }
112:
113: /**
114: * @see org.java.plugin.registry.PluginPrerequisite#getDeclaringPluginFragment()
115: */
116: public PluginFragment getDeclaringPluginFragment() {
117: return fragment;
118: }
119:
120: /**
121: * @see org.java.plugin.registry.PluginPrerequisite#isOptional()
122: */
123: public boolean isOptional() {
124: return model.isOptional();
125: }
126:
127: /**
128: * @see org.java.plugin.registry.PluginPrerequisite#isReverseLookup()
129: */
130: public boolean isReverseLookup() {
131: return model.isReverseLookup();
132: }
133:
134: /**
135: * @see org.java.plugin.registry.PluginPrerequisite#matches()
136: */
137: public boolean matches() {
138: PluginDescriptor descr = null;
139: try {
140: descr = this .descriptor.getRegistry().getPluginDescriptor(
141: model.getPluginId());
142: } catch (IllegalArgumentException iae) {
143: return false;
144: }
145: return matches(model.getPluginVersion(), descr.getVersion(),
146: model.getMatchingRule());
147: }
148:
149: /**
150: * @see org.java.plugin.registry.PluginPrerequisite#getMatchingRule()
151: */
152: public MatchingRule getMatchingRule() {
153: return model.getMatchingRule();
154: }
155:
156: /**
157: * @see org.java.plugin.registry.PluginPrerequisite#isExported()
158: */
159: public boolean isExported() {
160: return model.isExported();
161: }
162:
163: /**
164: * @see org.java.plugin.registry.Identity#getId()
165: */
166: public String getId() {
167: return model.getId();
168: }
169:
170: /**
171: * @see org.java.plugin.registry.Documentable#getDocsPath()
172: */
173: public String getDocsPath() {
174: return (fragment != null) ? fragment.getDocsPath() : descriptor
175: .getDocsPath();
176: }
177:
178: /**
179: * @see org.java.plugin.registry.Documentable#getDocumentation()
180: */
181: public Documentation<PluginPrerequisite> getDocumentation() {
182: return doc;
183: }
184:
185: /**
186: * @see org.java.plugin.registry.UniqueIdentity#getUniqueId()
187: */
188: public String getUniqueId() {
189: return descriptor.getRegistry().makeUniqueId(
190: descriptor.getId(), getId());
191: }
192:
193: /**
194: * @see java.lang.Object#toString()
195: */
196: @Override
197: public String toString() {
198: return "{Prerequisite: uid=" + getUniqueId() + "}"; //$NON-NLS-1$ //$NON-NLS-2$
199: }
200: }
|