1 /*
   2  * Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jnlp.converter.parser;
  25 
  26 import java.util.List;
  27 import java.util.ArrayList;
  28 import java.util.StringTokenizer;
  29 
  30 /*
  31  * Utility class that knows to handle version strings
  32  * A version string is of the form:
  33  *
  34  *  (version-id ('+'?) ' ') *
  35  *
  36  */
  37 public class VersionString {
  38     private final ArrayList<VersionID> _versionIds;
  39 
  40     /** Constructs a VersionString object from string */
  41     public VersionString(String vs) {
  42         _versionIds = new ArrayList<>();
  43         if (vs != null) {
  44             StringTokenizer st = new StringTokenizer(vs, " ", false);
  45             while (st.hasMoreElements()) {
  46                 // Note: The VersionID class takes care of a postfixed '+'
  47                 _versionIds.add(new VersionID(st.nextToken()));
  48             }
  49         }
  50     }
  51 
  52     public VersionString(VersionID id) {
  53         _versionIds = new ArrayList<>();
  54         if (id != null) {
  55             _versionIds.add(id);
  56         }
  57     }
  58 
  59     public boolean isSimpleVersion() {
  60         if (_versionIds.size() == 1) {
  61             return _versionIds.get(0).isSimpleVersion();
  62         }
  63         return false;
  64     }
  65 
  66     /** Check if this VersionString object contains the VersionID m */
  67     public boolean contains(VersionID m) {
  68         for (int i = 0; i < _versionIds.size(); i++) {
  69             VersionID vi = _versionIds.get(i);
  70             boolean check = vi.match(m);
  71             if (check) {
  72                 return true;
  73             }
  74         }
  75         return false;
  76     }
  77 
  78     /** Check if this VersionString object contains the VersionID m, given as a string */
  79     public boolean contains(String versionid) {
  80         return contains(new VersionID(versionid));
  81     }
  82 
  83     /** Check if this VersionString object contains anything greater than m */
  84     public boolean containsGreaterThan(VersionID m) {
  85         for (int i = 0; i < _versionIds.size(); i++) {
  86             VersionID vi = _versionIds.get(i);
  87             boolean check = vi.isGreaterThan(m);
  88             if (check) {
  89                 return true;
  90             }
  91         }
  92         return false;
  93     }
  94 
  95     /** Check if this VersionString object contains anything greater than the VersionID m, given as a string */
  96     public boolean containsGreaterThan(String versionid) {
  97         return containsGreaterThan(new VersionID(versionid));
  98     }
  99 
 100     /** Check if the versionString 'vs' contains the VersionID 'vi' */
 101     public static boolean contains(String vs, String vi) {
 102         return (new VersionString(vs)).contains(vi);
 103     }
 104 
 105     /** Pretty-print object */
 106     @Override
 107     public String toString() {
 108         StringBuilder sb = new StringBuilder();
 109         for (int i = 0; i < _versionIds.size(); i++) {
 110             sb.append(_versionIds.get(i).toString());
 111             sb.append(' ');
 112         }
 113         return sb.toString();
 114     }
 115 
 116     public List<VersionID> getAllVersionIDs() {
 117         return _versionIds;
 118     }
 119 }