fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.net.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. /*
  12.  * Copyright (C) 2007 The Android Open Source Project
  13.  *
  14.  * Licensed under the Apache License, Version 2.0 (the "License");
  15.  * you may not use this file except in compliance with the License.
  16.  * You may obtain a copy of the License at
  17.  *
  18.  * http://w...content-available-to-author-only...e.org/licenses/LICENSE-2.0
  19.  *
  20.  * Unless required by applicable law or agreed to in writing, software
  21.  * distributed under the License is distributed on an "AS IS" BASIS,
  22.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23.  * See the License for the specific language governing permissions and
  24.  * limitations under the License.
  25.  */
  26. public static class UriMatcher
  27. {
  28. public static final int NO_MATCH = -1;
  29. /**
  30.   * Creates the root node of the URI tree.
  31.   *
  32.   * @param code the code to match for the root URI
  33.   */
  34. public UriMatcher(int code)
  35. {
  36. mCode = code;
  37. mWhich = -1;
  38. mChildren = new ArrayList<UriMatcher>();
  39. mText = null;
  40. }
  41. private UriMatcher()
  42. {
  43. mCode = NO_MATCH;
  44. mWhich = -1;
  45. mChildren = new ArrayList<UriMatcher>();
  46. mText = null;
  47. }
  48. /**
  49.   * Add a URI to match, and the code to return when this URI is
  50.   * matched. URI nodes may be exact match string, the token "*"
  51.   * that matches any text, or the token "#" that matches only
  52.   * numbers.
  53.   * <p>
  54.   * Starting from API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
  55.   * this method will accept a leading slash in the path.
  56.   *
  57.   * @param authority the authority to match
  58.   * @param path the path to match. * may be used as a wild card for
  59.   * any text, and # may be used as a wild card for numbers.
  60.   * @param code the code that is returned when a URI is matched
  61.   * against the given components. Must be positive.
  62.   */
  63. public void addURI(String authority, String path, int code)
  64. {
  65. if (code < 0) {
  66. throw new IllegalArgumentException("code " + code + " is invalid: it must be positive");
  67. }
  68. String[] tokens = null;
  69. if (path != null) {
  70. String newPath = path;
  71. // Strip leading slash if present.
  72. if (path.length() > 0 && path.charAt(0) == '/') {
  73. newPath = path.substring(1);
  74. }
  75. tokens = newPath.split("/");
  76. }
  77. int numTokens = tokens != null ? tokens.length : 0;
  78. UriMatcher node = this;
  79. for (int i = -1; i < numTokens; i++) {
  80. String token = i < 0 ? authority : tokens[i];
  81. ArrayList<UriMatcher> children = node.mChildren;
  82. int numChildren = children.size();
  83. UriMatcher child;
  84. int j;
  85. for (j = 0; j < numChildren; j++) {
  86. child = children.get(j);
  87. if (token.equals(child.mText)) {
  88. node = child;
  89. break;
  90. }
  91. }
  92. if (j == numChildren) {
  93. // Child not found, create it
  94. child = new UriMatcher();
  95. if (token.equals("#")) {
  96. child.mWhich = NUMBER;
  97. } else if (token.equals("*")) {
  98. child.mWhich = TEXT;
  99. } else {
  100. child.mWhich = EXACT;
  101. }
  102. child.mText = token;
  103. node.mChildren.add(child);
  104. node = child;
  105. }
  106. }
  107. node.mCode = code;
  108. }
  109. /**
  110.   * Try to match against the path in a url.
  111.   *
  112.   * @param uri The url whose path we will match against.
  113.   *
  114.   * @return The code for the matched node (added using addURI),
  115.   * or -1 if there is no matched node.
  116.   */
  117. public int match(URI uri)
  118. {
  119. final String[] pathSegments = uri.getPath().substring(1).split("/");
  120.  
  121. final int li = pathSegments.length;
  122. UriMatcher node = this;
  123.  
  124. if (li == 0 && uri.getAuthority() == null) {
  125. return this.mCode;
  126. }
  127.  
  128. for (int i=-1; i<li; i++) {
  129. String u = i < 0 ? uri.getAuthority() : pathSegments[i];
  130. ArrayList<UriMatcher> list = node.mChildren;
  131. if (list == null) {
  132. break;
  133. }
  134. node = null;
  135. int lj = list.size();
  136. for (int j=0; j<lj; j++) {
  137. UriMatcher n = list.get(j);
  138. which_switch:
  139. switch (n.mWhich) {
  140. case EXACT:
  141. if (n.mText.equals(u)) {
  142. node = n;
  143. }
  144. break;
  145. case NUMBER:
  146. int lk = u.length();
  147. for (int k=0; k<lk; k++) {
  148. char c = u.charAt(k);
  149. if (c < '0' || c > '9') {
  150. break which_switch;
  151. }
  152. }
  153. node = n;
  154. break;
  155. case TEXT:
  156. node = n;
  157. break;
  158. }
  159. if (node != null) {
  160. break;
  161. }
  162. }
  163. if (node == null) {
  164. return NO_MATCH;
  165. }
  166. }
  167. return node.mCode;
  168. }
  169. private static final int EXACT = 0;
  170. private static final int NUMBER = 1;
  171. private static final int TEXT = 2;
  172. private int mCode;
  173. private int mWhich;
  174. private String mText;
  175. private ArrayList<UriMatcher> mChildren;
  176. }
  177. public static final String PATH_FILE = "file";
  178. public static final String PATH_DRIVE = "drive";
  179. public static final String CONTENT_AUTHORITY = "olexiimuraviov.ua.onespace_diplomaproject";
  180.  
  181.  
  182. static UriMatcher buildUriMatcherWithout() {
  183. UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  184.  
  185. // Bind uriMatcher int constants and uris
  186. mUriMatcher.addURI(CONTENT_AUTHORITY, PATH_DRIVE, 1);
  187. mUriMatcher.addURI(CONTENT_AUTHORITY, PATH_DRIVE + "/#", 2);
  188. mUriMatcher.addURI(CONTENT_AUTHORITY, PATH_FILE, 3);
  189. //
  190. mUriMatcher.addURI(CONTENT_AUTHORITY, PATH_FILE + "/drive/#", 5);
  191. mUriMatcher.addURI(CONTENT_AUTHORITY, PATH_FILE + "/file/*", 6);
  192. mUriMatcher.addURI(CONTENT_AUTHORITY, PATH_FILE + "/drive/#/*", 7);
  193. mUriMatcher.addURI(CONTENT_AUTHORITY, PATH_FILE + "/*", 4);
  194.  
  195. // Return the new matcher!
  196. return mUriMatcher;
  197. }
  198.  
  199. static UriMatcher buildUriMatcherWith() {
  200. UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  201.  
  202. // Bind uriMatcher int constants and uris
  203. mUriMatcher.addURI(CONTENT_AUTHORITY, PATH_DRIVE, 1);
  204. mUriMatcher.addURI(CONTENT_AUTHORITY, PATH_DRIVE + "/#", 2);
  205. mUriMatcher.addURI(CONTENT_AUTHORITY, PATH_FILE, 3);
  206. mUriMatcher.addURI(CONTENT_AUTHORITY, PATH_FILE + "/*", 4);
  207. mUriMatcher.addURI(CONTENT_AUTHORITY, PATH_FILE + "/drive/#", 5);
  208. mUriMatcher.addURI(CONTENT_AUTHORITY, PATH_FILE + "/file/*", 6);
  209. mUriMatcher.addURI(CONTENT_AUTHORITY, PATH_FILE + "/drive/#/*", 7);
  210.  
  211. // Return the new matcher!
  212. return mUriMatcher;
  213. }
  214. public static void main (String[] args) throws java.lang.Exception
  215. {
  216. UriMatcher without = buildUriMatcherWithout();
  217. System.out.println("" + without.match(new URI("content://olexiimuraviov.ua.onespace_diplomaproject/file/drive/11/root")));
  218. System.out.println("" + without.match(new URI("content://olexiimuraviov.ua.onespace_diplomaproject/file/file/11")));
  219. System.out.println("" + without.match(new URI("content://olexiimuraviov.ua.onespace_diplomaproject/file/file/jk")));
  220. System.out.println("" + without.match(new URI("content://olexiimuraviov.ua.onespace_diplomaproject/drive/1")));
  221. System.out.println("" + without.match(new URI("content://olexiimuraviov.ua.onespace_diplomaproject/drive")));
  222. System.out.println("" + without.match(new URI("content://olexiimuraviov.ua.onespace_diplomaproject/file/aaaa")));
  223. System.out.println("=========");
  224. UriMatcher with = buildUriMatcherWith();
  225. System.out.println("" + with.match(new URI("content://olexiimuraviov.ua.onespace_diplomaproject/file/drive/11/root")));
  226. System.out.println("" + with.match(new URI("content://olexiimuraviov.ua.onespace_diplomaproject/file/file/11")));
  227. System.out.println("" + with.match(new URI("content://olexiimuraviov.ua.onespace_diplomaproject/file/file/jk")));
  228. System.out.println("" + with.match(new URI("content://olexiimuraviov.ua.onespace_diplomaproject/drive/1")));
  229. System.out.println("" + with.match(new URI("content://olexiimuraviov.ua.onespace_diplomaproject/drive")));
  230. System.out.println("" + with.match(new URI("content://olexiimuraviov.ua.onespace_diplomaproject/file/aaaa")));
  231. }
  232. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
7
6
6
2
1
4
=========
-1
-1
-1
2
1
4