Coverage Report - org.apache.myfaces.shared.resource.ResourceLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
ResourceLoader
0%
0/14
0%
0/4
1.714
ResourceLoader$VersionComparator
0%
0/32
0%
0/16
1.714
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.myfaces.shared.resource;
 20  
 
 21  
 import java.io.InputStream;
 22  
 import java.net.URL;
 23  
 import java.util.Comparator;
 24  
 
 25  
 /**
 26  
  * Base class for resource loaders.  Resource loaders can lookup resources 
 27  
  * as URLs from arbitrary locations, including JAR files.
 28  
  */
 29  
 public abstract class ResourceLoader
 30  
 {
 31  
     
 32  
     public static final String VERSION_INVALID = "INVALID";
 33  
     
 34  
     private String _prefix;
 35  
     
 36  
     public ResourceLoader(String prefix)
 37  0
     {
 38  0
         _prefix = prefix;
 39  0
     }
 40  
 
 41  
     public abstract String getResourceVersion(String path);
 42  
 
 43  
     /**
 44  
      * Return the max available version found (if exists) or
 45  
      * return null if no version available. 
 46  
      */
 47  
     public abstract String getLibraryVersion(String path);
 48  
 
 49  
     /**
 50  
      * Return the max available version found (if exists) or
 51  
      * return null if no version available. 
 52  
      */
 53  
     public abstract URL getResourceURL(ResourceMeta resourceMeta);
 54  
 
 55  
     public abstract InputStream getResourceInputStream(ResourceMeta resourceMeta);
 56  
     
 57  
     public abstract ResourceMeta createResourceMeta(String prefix, String libraryName, String libraryVersion,
 58  
             String resourceName, String resourceVersion);
 59  
 
 60  
     public ResourceMeta createResourceMeta(String prefix, String libraryName, 
 61  
             String libraryVersion, String resourceName, String resourceVersion, String contractName)
 62  
     {
 63  0
         return createResourceMeta(prefix, libraryName, libraryVersion, resourceName, resourceVersion);
 64  
     }
 65  
     
 66  
     public abstract boolean libraryExists(String libraryName);
 67  
     
 68  
     public boolean resourceExists(ResourceMeta resourceMeta)
 69  
     {
 70  0
         return (getResourceURL(resourceMeta) != null);
 71  
     }
 72  
 
 73  
     /*
 74  
     public URL getResourceURL(String resourceId)
 75  
     {
 76  
         throw new UnsupportedOperationException(
 77  
             "An implementation for getResourceURL(String resourceId) method is required for JSF 2.2");
 78  
     }
 79  
     
 80  
     public boolean resourceIdExists(String resourceId)
 81  
     {
 82  
         return (getResourceURL(resourceId) != null);
 83  
     }*/
 84  
     
 85  0
     private Comparator<String> _versionComparator = null;
 86  
 
 87  
     protected Comparator<String> getVersionComparator()
 88  
     {
 89  0
         if (_versionComparator == null)
 90  
         {
 91  0
             _versionComparator = new VersionComparator();
 92  
         }
 93  0
         return _versionComparator;
 94  
     }
 95  
 
 96  
     protected void setVersionComparator(Comparator<String> versionComparator)
 97  
     {
 98  0
         _versionComparator = versionComparator;
 99  0
     }
 100  
 
 101  0
     public static class VersionComparator implements Comparator<String>
 102  
     {
 103  
 
 104  
         public int compare(String s1, String s2)
 105  
         {
 106  0
             int n1 = 0;
 107  0
             int n2 = 0;
 108  0
             String o1 = s1;
 109  0
             String o2 = s2;
 110  
 
 111  0
             boolean p1 = true;
 112  0
             boolean p2 = true;
 113  
 
 114  0
             while (n1 == n2 && (p1 || p2))
 115  
             {
 116  0
                 int i1 = o1.indexOf('_');
 117  0
                 int i2 = o2.indexOf('_');
 118  0
                 if (i1 < 0)
 119  
                 {
 120  0
                     if (o1.length() > 0)
 121  
                     {
 122  0
                         p1 = false;
 123  0
                         n1 = Integer.valueOf(o1);
 124  0
                         o1 = "";
 125  
                     }
 126  
                     else
 127  
                     {
 128  0
                         p1 = false;
 129  0
                         n1 = 0;
 130  
                     }
 131  
                 }
 132  
                 else
 133  
                 {
 134  0
                     n1 = Integer.valueOf(o1.substring(0, i1));
 135  0
                     o1 = o1.substring(i1 + 1);
 136  
                 }
 137  0
                 if (i2 < 0)
 138  
                 {
 139  0
                     if (o2.length() > 0)
 140  
                     {
 141  0
                         p2 = false;
 142  0
                         n2 = Integer.valueOf(o2);
 143  0
                         o2 = "";
 144  
                     }
 145  
                     else
 146  
                     {
 147  0
                         p2 = false;
 148  0
                         n2 = 0;
 149  
                     }
 150  
                 }
 151  
                 else
 152  
                 {
 153  0
                     n2 = Integer.valueOf(o2.substring(0, i2));
 154  0
                     o2 = o2.substring(i2 + 1);
 155  
                 }
 156  0
             }
 157  
 
 158  0
             if (n1 == n2)
 159  
             {
 160  0
                 return s1.length() - s2.length();
 161  
             }
 162  0
             return n1 - n2;
 163  
         }
 164  
     }
 165  
     
 166  
     public String getPrefix()
 167  
     {
 168  0
         return _prefix;
 169  
     }
 170  
 
 171  
     public void setPrefix(String prefix)
 172  
     {
 173  0
         _prefix = prefix;
 174  0
     }
 175  
 }