001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017:
018: package org.apache.harmony.tools.jarsigner;
019:
020: /**
021: * Class to build the base file names for .SF and .DSA files.
022: * It is designed to be used only in JSParameters.
023: */
024: class FileNameGenerator {
025: private static final int fileNameLength = 8;
026: private static final int maxExtLength = 3;
027:
028: /**
029: * Generates the file name for .SF and .DSA files using
030: * sigFileName or alias given on the command line.
031: *
032: * @param param
033: * @return
034: * @throws NullPointerException - if both parameters are null
035: */
036: static String generateFileName(String sigFileName, String alias) {
037: if (sigFileName != null) {
038: return convertString(sigFileName.toUpperCase());
039: }
040: if (alias == null) {
041: throw new NullPointerException("Alias is null.");
042: }
043: int length = alias.length();
044: if (length > fileNameLength) {
045: alias = alias.substring(0, fileNameLength);
046: length = fileNameLength;
047: }
048:
049: alias = convertString(alias);
050: return alias.toUpperCase();
051: }
052:
053: /**
054: * Generates signature block file name, based on key algorithm.
055: *
056: * @param sigFileName
057: * @param keyAlg
058: * @return
059: */
060: static String generateSigBlockName(String sigFileName, String keyAlg) {
061: // make an extension
062: String sigBlockFileExt;
063: // max allowed extension length is 3 symbols
064: if (keyAlg.length() > maxExtLength) {
065: sigBlockFileExt = "."
066: + (keyAlg.substring(0, maxExtLength)).toUpperCase();
067: } else {
068: sigBlockFileExt = "." + keyAlg.toUpperCase();
069: }
070:
071: // add a prefix if necessary
072: if (keyAlg.equalsIgnoreCase("DSA")
073: || keyAlg.equalsIgnoreCase("RSA")) {
074: // no prefix
075: return sigFileName + sigBlockFileExt;
076: } else {
077: // add prefix "SIG-"
078: return "SIG-" + sigFileName + sigBlockFileExt;
079: }
080: }
081:
082: // Finds disallowed letters in input String and converts
083: // them to underscores ("_"). Allowed characters are letters, digits,
084: // hyphens and underscores. If no changes are made, the input string itself
085: // is returned (not a copy!).
086: private static String convertString(String input) {
087: char[] chars = input.toCharArray();
088: boolean isChanged = false;
089: for (int i = 0; i < chars.length; i++) {
090: char current = chars[i];
091: if ((current >= 'A' && current <= 'Z')
092: || (current >= 'a' && current <= 'z')
093: || (current >= '0' && current <= '9')
094: || current == '-' || current == '_') {
095: continue;
096: }
097:
098: isChanged = true;
099: chars[i] = '_';
100: }
101: if (isChanged) {
102: return new String(chars);
103: } else {
104: return input;
105: }
106: }
107: }
|