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
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039: package org.netbeans.modules.ruby.platform.gems;
040:
041: import java.io.File;
042: import java.util.ArrayList;
043: import java.util.HashMap;
044: import java.util.List;
045: import java.util.Map;
046: import java.util.logging.Logger;
047: import java.util.regex.Matcher;
048: import java.util.regex.Pattern;
049: import org.openide.util.Parameters;
050:
051: /**
052: * A helper class for parsing the names and version numbers of
053: * files representing Gems and choosing the newest versions of those.
054: *
055: * @author Erno Mononen
056: */
057: class GemFilesParser {
058:
059: /**
060: * Extension of files containing gems specification residing in {@link
061: * #SPECIFICATIONS}.
062: */
063: private static final String DOT_GEM_SPEC = ".gemspec"; // NOI18N
064: /**
065: * The pattern for capturing the gem name and version from file names.
066: */
067: private static final Pattern PATTERN = Pattern
068: .compile("([\\w-]+)\\-([\\d.]+)"); //NOI18N
069: private static final Logger LOGGER = Logger
070: .getLogger(GemFilesParser.class.getName());
071: /**
072: * The files to check for gems.
073: */
074: private final File[] specFiles;
075:
076: /**
077: * Key => gem name, value => (key => gem version, value => gem file).
078: */
079: private Map<String, Map<String, File>> resultMap;
080:
081: /**
082: * Constructs a new GemFilesParser for the given <code>files</code>.
083: */
084: public GemFilesParser(File... specFiles) {
085: Parameters.notNull("files", specFiles); //NOI18N
086: this .specFiles = specFiles;
087: }
088:
089: /**
090: * Finds the files representing gems and chooses the newest version of each.
091: * Use {@link getFiles()} and {@link getFiles()} to retrieve the results.
092: */
093: public void chooseGems() {
094:
095: resultMap = new HashMap<String, Map<String, File>>();
096:
097: for (File spec : specFiles) {
098: // See if it looks like a gem
099: String fileName = spec.getName();
100: if (!fileName.endsWith(DOT_GEM_SPEC)) {
101: continue;
102: }
103:
104: fileName = fileName.substring(0, fileName.length()
105: - DOT_GEM_SPEC.length());
106:
107: GemInfo gemInfo = parseInfo(fileName);
108: if (gemInfo == null) {
109: LOGGER
110: .fine("Could not resolve the name and version for "
111: + fileName);
112: continue;
113: }
114:
115: Map<String, File> nameMap = resultMap
116: .get(gemInfo.getName());
117:
118: if (nameMap == null) {
119: nameMap = new HashMap<String, File>();
120: resultMap.put(gemInfo.getName(), nameMap);
121: nameMap.put(gemInfo.getVersion(), spec);
122: } else {
123: // Decide whether this version is more recent than the one already there
124: String oldVersion = nameMap.keySet().iterator().next();
125:
126: if (GemManager.compareGemVersions(gemInfo.getVersion(),
127: oldVersion) > 0) {
128: // New version is higher
129: nameMap.clear();
130: nameMap.put(gemInfo.getVersion(), spec);
131: }
132: }
133: }
134: }
135:
136: private void checkInitialiazed() {
137: if (resultMap == null) {
138: throw new IllegalStateException(
139: "Not initialized, you must run the chooseGems method first");
140: }
141: }
142:
143: // todo: javadoc + rename
144: public Map<String, Map<String, File>> getGemMap() {
145: checkInitialiazed();
146: return resultMap;
147: }
148:
149: /**
150: * @return the found gem files.
151: */
152: public File[] getFiles() {
153:
154: checkInitialiazed();
155:
156: List<File> resultList = new ArrayList<File>();
157:
158: for (Map<String, File> map : resultMap.values()) {
159: for (File f : map.values()) {
160: resultList.add(f);
161: }
162: }
163: return resultList.toArray(new File[resultList.size()]);
164:
165: }
166:
167: // not private because used in tests
168: static GemInfo parseInfo(String fileName) {
169: Matcher m = PATTERN.matcher(fileName);
170: if (!m.find() || m.groupCount() < 2) {
171: //XXX: can there be gems without a version number?
172: return null;
173: }
174: return new GemInfo(m.group(1), m.group(2));
175: }
176:
177: static final class GemInfo {
178:
179: private final String name;
180: private final String version;
181:
182: public GemInfo(String name, String version) {
183: this .name = name;
184: this .version = version;
185: }
186:
187: public String getName() {
188: return name;
189: }
190:
191: public String getVersion() {
192: return version;
193: }
194: }
195: }
|