public ParsedDistinguishedNameImpl()

in taverna-credential-manager-impl/src/main/java/org/apache/taverna/security/credentialmanager/impl/ParsedDistinguishedNameImpl.java [116:227]


	public ParsedDistinguishedNameImpl(String DNstr) {
		// ///////////////////////////////////////////////////////////////////////////////////////////////////
		// Parse the DN String and put into variables. First, tokenise using a
		// "," character as a delimiter
		// UNLESS escaped with a "\" character. Put the tokens into an
		// ArrayList. These should be name value pairs
		// separated by "=". Tokenise these using a StringTokenizer class, test
		// for the name, and if one of the
		// recognised names, copy into the correct variable. The reason
		// StringTokenizer is not used for the major
		// token list is that the StringTokenizer class does not handle escaped
		// delimiters so an escaped delimiter
		// in the code would be treated as a valid one.

		int i = 0;

		char majorListDelimiter = ',';
		char majorListEscapeChar = '\\';

		// String minorListDelimiter = "=";

		String DNchars = DNstr;

		int startIndex = 0;
		int endIndex = 0;
		boolean ignoreThisChar = false;

		boolean inQuotes = false;

		ArrayList<String> majorTokenList = new ArrayList<String>();

		for (i = 0; i < DNchars.length(); i++) {
			if (ignoreThisChar == true) {
				ignoreThisChar = false;
			} else if ((inQuotes == false) && (DNchars.charAt(i) == '\"')) {
				inQuotes = true;
			} else if ((inQuotes == true) && (DNchars.charAt(i) == '\"')) {
				inQuotes = false;
			} else if (inQuotes == true) {
				continue;
			} else if (DNchars.charAt(i) == majorListEscapeChar) {
				ignoreThisChar = true;
			} else if ((DNchars.charAt(i) == majorListDelimiter)
					&& (ignoreThisChar == false)) {
				endIndex = i;
				majorTokenList.add(DNchars.substring(startIndex, endIndex));
				startIndex = i + 1;
			}
		}

		// Add last token - after the last delimiter
		endIndex = DNchars.length();
		majorTokenList.add(DNchars.substring(startIndex, endIndex));

		for (String currentToken : majorTokenList) {
			currentToken = currentToken.trim();

			// split on first equals only, as value can contain an equals char
			String[] minorTokenList = currentToken.split("=", 2);

			if (minorTokenList.length == 2) {
				// there had better be a key and a value only
				String DNTokenName = minorTokenList[0].toUpperCase();
				String DNTokenValue = minorTokenList[1];

				if (DNTokenName.equals("CN")
						|| DNTokenName.equals("COMMONNAME")) {
					CN = DNTokenValue;
				} else if (DNTokenName.equals("EMAIL")
						|| DNTokenName.equals("EMAILADDRESS")) {
					emailAddress = DNTokenValue;
				} else if (DNTokenName.equals("OU")
						|| DNTokenName.equals("ORGANIZATIONALUNITNAME")) {
					OU = DNTokenValue;
				} else if (DNTokenName.equals("O")
						|| DNTokenName.equals("ORGANIZATIONNAME")) {
					O = DNTokenValue;
				} else if (DNTokenName.equals("L")
						|| DNTokenName.equals("LOCALITYNAME")) {
					L = DNTokenValue;
				} else if (DNTokenName.equals("ST")
						|| DNTokenName.equals("STATEORPROVINCENAME")) {
					ST = DNTokenValue;
				} else if (DNTokenName.equals("C")
						|| DNTokenName.equals("COUNTRYNAME")) {
					C = DNTokenValue;
				}
			}
			// else we have a key with no value, so skip processing the key
		}

		if (CN == null)
			CN = "none";

		if (emailAddress == null)
			emailAddress = "none";

		if (OU == null)
			OU = "none";

		if (O == null)
			O = "none";

		if (L == null)
			L = "none";

		if (ST == null)
			ST = "none";

		if (C == null)
			C = "none";
	}