package com.orange.analysis.headers;

/*
 * #%L
 * Matos
 * %%
 * Copyright (C) 2004 - 2014 Orange SA
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.util.Map.Entry;
import java.util.regex.Pattern;

import com.orange.matos.core.Alert;
import com.orange.matos.core.Configuration;

/**
 * @author piac6784
 * Check the ids used in attributes describing the use of CHAPI (Content
 * Handler API). It is an AFSCM checker.
 */
public class ChapiIdChecker extends RegexpAttributeChecker {
	
	final private Configuration config;
	
	ChapiIdChecker(Configuration config) { 
		super("MicroEdition-Handler-([0-9][0-9]?)-ID");
		this.config = config;
	}
	
	@Override
	public void check() throws Alert{
		String chapiIdRegexpSpec = config.string("descriptor.chapiIdRegexp");
		Pattern chapiIdRegexp = null;
		if (chapiIdRegexpSpec != null && chapiIdRegexpSpec.length() > 0) {
			chapiIdRegexp = Pattern.compile(chapiIdRegexpSpec);
			for(Entry<String,String> e : jadMap.entrySet()) {
				if(!chapiIdRegexp.matcher(e.getValue()).matches()) {
					addProblem("Identifier '" + e.getValue() + "' for content-handler " + e.getKey() 
							   + " in the JAD does not respect the imposed constaints.","");
				}
			}
			for(Entry<String,String> e : jarMap.entrySet()) {
				if(!chapiIdRegexp.matcher(e.getValue()).matches()) {
					addProblem("Identifier '" + e.getValue() + "' for content-handler " + e.getKey() 
							   + " in the JAR manifest does not respect the imposed constaints.","");
				}
			}
		}
	}
}

@@ -931,98 +931,265 @@
 I). 
-It is an AFSCM checker.%0A */%0Apublic class ChapiIdChecker extends RegexpAttributeChecker %7B%0A%09
+The expected format is described by a regular expression%0A * specified in the profile (descriptor.chapiIdRegexp) %0A */%0Apublic class ChapiIdChecker extends RegexpAttributeChecker %7B%0A%09%0A%09private final static String KEY_CHAPIID_REGEXP = %22descriptor.chapiIdRegexp%22;
 %0A%09fi
@@ -1437,34 +1437,26 @@
 ing(
-%22descriptor.chapiIdRegexp%22
+KEY_CHAPIID_REGEXP
 );%0A%09

Clean ChapiId header handling.