001: package org.apache.maven.settings.validation;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.maven.settings.Profile;
023: import org.apache.maven.settings.Repository;
024: import org.apache.maven.settings.Settings;
025:
026: import java.util.Iterator;
027: import java.util.List;
028:
029: /**
030: * @author Milos Kleint
031: */
032: public class DefaultSettingsValidator implements SettingsValidator {
033:
034: public SettingsValidationResult validate(Settings model) {
035: SettingsValidationResult result = new SettingsValidationResult();
036:
037: List profiles = model.getProfiles();
038: if (profiles != null) {
039: Iterator it = profiles.iterator();
040: while (it.hasNext()) {
041: Profile prof = (Profile) it.next();
042: validateRepositories(result, prof.getRepositories(),
043: "repositories.repository");
044: validateRepositories(result, prof
045: .getPluginRepositories(),
046: "pluginRepositories.pluginRepository");
047: }
048:
049: }
050:
051: return result;
052: }
053:
054: private void validateRepositories(SettingsValidationResult result,
055: List repositories, String prefix) {
056: for (Iterator it = repositories.iterator(); it.hasNext();) {
057: Repository repository = (Repository) it.next();
058:
059: validateStringNotEmpty(prefix + ".id", result, repository
060: .getId());
061:
062: validateStringNotEmpty(prefix + ".url", result, repository
063: .getUrl());
064: }
065: }
066:
067: // ----------------------------------------------------------------------
068: // Field validation
069: // ----------------------------------------------------------------------
070:
071: private boolean validateStringNotEmpty(String fieldName,
072: SettingsValidationResult result, String string) {
073: return validateStringNotEmpty(fieldName, result, string, null);
074: }
075:
076: /**
077: * Asserts:
078: * <p/>
079: * <ul>
080: * <li><code>string.length != null</code>
081: * <li><code>string.length > 0</code>
082: * </ul>
083: */
084: private boolean validateStringNotEmpty(String fieldName,
085: SettingsValidationResult result, String string,
086: String sourceHint) {
087: if (!validateNotNull(fieldName, result, string, sourceHint)) {
088: return false;
089: }
090:
091: if (string.length() > 0) {
092: return true;
093: }
094:
095: if (sourceHint != null) {
096: result.addMessage("'" + fieldName + "' is missing for "
097: + sourceHint);
098: } else {
099: result.addMessage("'" + fieldName + "' is missing.");
100: }
101:
102: return false;
103: }
104:
105: /**
106: * Asserts:
107: * <p/>
108: * <ul>
109: * <li><code>string != null</code>
110: * </ul>
111: */
112: private boolean validateNotNull(String fieldName,
113: SettingsValidationResult result, Object object,
114: String sourceHint) {
115: if (object != null) {
116: return true;
117: }
118:
119: if (sourceHint != null) {
120: result.addMessage("'" + fieldName + "' is missing for "
121: + sourceHint);
122: } else {
123: result.addMessage("'" + fieldName + "' is missing.");
124: }
125:
126: return false;
127: }
128:
129: }
|