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: *
017: */
018: package org.apache.ivy.plugins.namespace;
019:
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.apache.ivy.core.module.id.ModuleRevisionId;
025:
026: public class Namespace {
027: public static final Namespace SYSTEM_NAMESPACE;
028: static {
029: SYSTEM_NAMESPACE = new Namespace();
030: }
031:
032: private List rules = new ArrayList();
033:
034: private String name;
035:
036: private boolean chainRules = false;
037:
038: private NamespaceTransformer fromSystemTransformer = new NamespaceTransformer() {
039: public ModuleRevisionId transform(ModuleRevisionId mrid) {
040: if (mrid == null) {
041: return null;
042: }
043: for (Iterator iter = rules.iterator(); iter.hasNext();) {
044: NamespaceRule rule = (NamespaceRule) iter.next();
045: ModuleRevisionId nmrid = rule.getFromSystem()
046: .transform(mrid);
047: if (chainRules) {
048: mrid = nmrid;
049: } else if (!nmrid.equals(mrid)) {
050: return nmrid;
051: }
052: }
053: return mrid;
054: }
055:
056: public boolean isIdentity() {
057: return rules.isEmpty();
058: }
059: };
060:
061: private NamespaceTransformer toSystemTransformer = new NamespaceTransformer() {
062: public ModuleRevisionId transform(ModuleRevisionId mrid) {
063: if (mrid == null) {
064: return null;
065: }
066: for (Iterator iter = rules.iterator(); iter.hasNext();) {
067: NamespaceRule rule = (NamespaceRule) iter.next();
068: ModuleRevisionId nmrid = rule.getToSystem().transform(
069: mrid);
070: if (chainRules) {
071: mrid = nmrid;
072: } else if (!nmrid.equals(mrid)) {
073: return nmrid;
074: }
075: }
076: return mrid;
077: }
078:
079: public boolean isIdentity() {
080: return rules.isEmpty();
081: }
082: };
083:
084: public void addRule(NamespaceRule rule) {
085: rules.add(rule);
086: }
087:
088: public String getName() {
089: return name;
090: }
091:
092: public void setName(String name) {
093: this .name = name;
094: }
095:
096: public NamespaceTransformer getFromSystemTransformer() {
097: return fromSystemTransformer;
098: }
099:
100: public NamespaceTransformer getToSystemTransformer() {
101: return toSystemTransformer;
102: }
103:
104: public boolean isChainrules() {
105: return chainRules;
106: }
107:
108: public void setChainrules(boolean chainRules) {
109: this.chainRules = chainRules;
110: }
111: }
|