components/visual-panels/coreadm/src/java/vpanels/app/coreadm/com/oracle/solaris/vp/panels/coreadm/client/swing/CoreUtil.java
changeset 3553 f1d133b09a8c
parent 3552 077ebe3d0d24
child 3554 ef58713bafc4
equal deleted inserted replaced
3552:077ebe3d0d24 3553:f1d133b09a8c
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.panels.coreadm.client.swing;
       
    27 
       
    28 import java.util.*;
       
    29 import java.util.regex.Pattern;
       
    30 
       
    31 /**
       
    32  * Contains utility routines for managing core files and patterns.
       
    33  */
       
    34 public class CoreUtil
       
    35 {
       
    36     /*
       
    37      * Though I'm a card-carrying enum whore, I have to admit using
       
    38      * one doesn't make sense here.
       
    39      */
       
    40     public static final char FMT_DIR = 'd';
       
    41     public static final char FMT_FILE = 'f';
       
    42     public static final char FMT_GID = 'g';
       
    43     public static final char FMT_MACHINE = 'm';
       
    44     public static final char FMT_NODE = 'n';
       
    45     public static final char FMT_PID = 'p';
       
    46     public static final char FMT_TIME = 't';
       
    47     public static final char FMT_UID = 'u';
       
    48     public static final char FMT_ZONE = 'z';
       
    49     public static final char FMT_PERCENT = '%';
       
    50 
       
    51     private static final HashMap<Character, String> MAP_REGEX =
       
    52 	new HashMap<Character, String>(10);
       
    53     static {
       
    54 	MAP_REGEX.put(FMT_DIR, ".+");
       
    55 	MAP_REGEX.put(FMT_FILE, "[^/]+");
       
    56 	MAP_REGEX.put(FMT_GID, "[0-9]+");
       
    57 	MAP_REGEX.put(FMT_MACHINE, "[^/]+");
       
    58 	MAP_REGEX.put(FMT_NODE, "[^/]+");
       
    59 	MAP_REGEX.put(FMT_PID, "[0-9]+");
       
    60 	MAP_REGEX.put(FMT_TIME, "[0-9]+");
       
    61 	MAP_REGEX.put(FMT_UID, "[0-9]+");
       
    62 	MAP_REGEX.put(FMT_ZONE, "[^/]+");
       
    63 	MAP_REGEX.put(FMT_PERCENT, Pattern.quote("%"));
       
    64     }
       
    65 
       
    66     public static String mapPath(Map<Character, String> mappings, String path)
       
    67     {
       
    68 	/* assert(mappings.keySet().containsAll(MAP_REGEX.keySet())); */
       
    69 
       
    70 	StringBuilder buffer = new StringBuilder();
       
    71 	char[] chars = path.toCharArray();
       
    72 	for (int i = 0; i < chars.length; i++) {
       
    73 	    if (i != chars.length - 1 && chars[i] == '%') {
       
    74 		String sub = mappings.get(chars[i + 1]);
       
    75 		if (sub != null) {
       
    76 		    buffer.append(sub);
       
    77 		    i++;
       
    78 		    continue;
       
    79 		}
       
    80 	    }
       
    81 	    buffer.append(chars[i]);
       
    82 	}
       
    83 
       
    84 	return (buffer.toString());
       
    85     }
       
    86 
       
    87     public static class PathElement
       
    88     {
       
    89 	public String element_;	    /** the path component string */
       
    90 	public boolean isRegex_;    /** if this element is a regex */
       
    91 	public boolean isDir_;	    /** if this element has a %d */
       
    92 
       
    93 	private PathElement(String e, boolean r, boolean d)
       
    94 	{
       
    95 	    element_ = e;
       
    96 	    isRegex_ = r;
       
    97 	    isDir_ = d;
       
    98 	}
       
    99     }
       
   100 
       
   101     public static PathElement[] pathToRegex(String path)
       
   102     {
       
   103 	/*
       
   104 	 * We need to armor the path against coincidental regular
       
   105 	 * expressions.  Unfortunately, we can neither quote the
       
   106 	 * entire path (quoting could break up percent-letter pairs)
       
   107 	 * or the post substitution path (we'd we quoting the regular
       
   108 	 * expressions we added).  Instead, we must split at and
       
   109 	 * quote between token boundaries.
       
   110 	 */
       
   111 	String[] elements = path.split("/", -1);
       
   112 	PathElement[] result = new PathElement[elements.length];
       
   113 
       
   114 	for (int e = 0; e < elements.length; e++) {
       
   115 	    String[] bits = elements[e].split("%[dfgmnptuz%]", -1);
       
   116 	    if (bits.length == 1) {
       
   117 		result[e] = new PathElement(elements[e], false, false);
       
   118 		continue;
       
   119 	    }
       
   120 
       
   121 	    StringBuilder buffer = new StringBuilder();
       
   122 	    char[] chars = elements[e].toCharArray();
       
   123 	    int pos = 0;
       
   124 	    boolean isDir = false;
       
   125 	    for (String b : bits) {
       
   126 		buffer.append(Pattern.quote(b));
       
   127 		pos += b.length();
       
   128 		if (pos < chars.length) {
       
   129 		    char format = chars[pos + 1];
       
   130 		    if (format == FMT_DIR)
       
   131 			isDir = true;
       
   132 		    buffer.append(MAP_REGEX.get(format));
       
   133 		    pos += 2;
       
   134 		}
       
   135 	    }
       
   136 	    result[e] = new PathElement(buffer.toString(), true, isDir);
       
   137 	}
       
   138 
       
   139 	return (result);
       
   140     }
       
   141 }