W3C home > Mailing lists > Public > www-validator-cvs@w3.org > August 2009

2006/unicorn/src/org/w3c/unicorn/action ObserveAction.java,1.1.2.7,1.1.2.8

From: Thomas Gambet via cvs-syncmail <cvsmail@w3.org>
Date: Mon, 24 Aug 2009 11:17:02 +0000
To: www-validator-cvs@w3.org
Message-Id: <E1MfXXq-0002W6-Rg@lionel-hutz.w3.org>
Update of /sources/public/2006/unicorn/src/org/w3c/unicorn/action
In directory hutz:/tmp/cvs-serv9647/src/org/w3c/unicorn/action

Modified Files:
      Tag: dev2
	ObserveAction.java 
Log Message:
added language negociation

Index: ObserveAction.java
===================================================================
RCS file: /sources/public/2006/unicorn/src/org/w3c/unicorn/action/Attic/ObserveAction.java,v
retrieving revision 1.1.2.7
retrieving revision 1.1.2.8
diff -u -d -r1.1.2.7 -r1.1.2.8
--- ObserveAction.java	19 Aug 2009 08:14:12 -0000	1.1.2.7
+++ ObserveAction.java	24 Aug 2009 11:17:00 -0000	1.1.2.8
@@ -32,11 +32,10 @@
 import org.w3c.unicorn.UnicornCall;
 import org.w3c.unicorn.contract.EnumInputMethod;
 import org.w3c.unicorn.exceptions.NoTaskException;
-import org.w3c.unicorn.index.IndexGenerator;
+import org.w3c.unicorn.language.Language;
 import org.w3c.unicorn.output.OutputFactory;
 import org.w3c.unicorn.output.OutputFormater;
 import org.w3c.unicorn.output.OutputModule;
-import org.w3c.unicorn.util.LocalizedString;
 import org.w3c.unicorn.util.Property;
 
 /**
@@ -47,8 +46,7 @@
  */
 public class ObserveAction extends HttpServlet {
 
-	private static final Log logger = LogFactory
-			.getLog("org.w3c.unicorn.servlet");
+	private static final Log logger = LogFactory.getLog(ObserveAction.class);
 
 	private static final long serialVersionUID = -1375355420965607571L;
 
@@ -73,22 +71,6 @@
 		ObserveAction.factory.setRepository(new File(Property
 				.get("UPLOADED_FILES_REPOSITORY")));
 
-		try {
-			IndexGenerator.generateIndexes();
-		} catch (final ResourceNotFoundException e) {
-			ObserveAction.logger.error("ResourceNotFoundException : "
-					+ e.getMessage(), e);
-			e.printStackTrace();
-		} catch (final ParseErrorException e) {
-			ObserveAction.logger.error(
-					"ParseErrorException : " + e.getMessage(), e);
-			e.printStackTrace();
-		} catch (final Exception e) {
-			ObserveAction.logger.error("Exception : " + e.getMessage(), e);
-			e.printStackTrace();
-		}
-		ObserveAction.logger.info("End of initialisation of servlet.");
-
 	}
 
 	/*
@@ -98,11 +80,17 @@
 	 *      javax.servlet.http.HttpServletResponse)
 	 */
 	@Override
-	protected void doGet(final HttpServletRequest aHttpServletRequest,
-			final HttpServletResponse aHttpServletResponse)
+	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
 			throws ServletException, IOException {
 		ObserveAction.logger.trace("doGet");
 
+		// Language negotiation
+		String langParameter = req.getParameter(Property.get("UNICORN_PARAMETER_PREFIX") + "lang");
+		if (langParameter == null || !Framework.getLanguageProperties().containsKey(langParameter))
+			langParameter = Language.negociate(req.getLocales());
+		
+		//velocityContext = new VelocityContext(Language.getContext(langParameter));
+		
 		// Variables related to the output
 		final Map<String, String[]> mapOfSpecificParameter = new Hashtable<String, String[]>();
 		final Map<String, String> mapOfOutputParameter = new Hashtable<String, String>();
@@ -110,39 +98,20 @@
 		mapOfOutputParameter.put("format", "xhtml10");
 		mapOfOutputParameter.put("charset", "UTF-8");
 		mapOfOutputParameter.put("mimetype", "text/html");
-
-		// Returns the preferred Locale that the client will accept content in,
-		// based on the Accept-Language header
-		final String aLocale = convertEnumerationToString(aHttpServletRequest
-				.getLocales());
+		mapOfOutputParameter.put("lang", langParameter);
+		
 		final UnicornCall aUnicornCall = new UnicornCall();
-
-		// Language of the template
-		// ucn_lang is a parameter which is define in xx_index.html.vm.
-		// It is an hidden parameter of a form.
-		String templateLang = null;
-		if (aHttpServletRequest.getParameterValues(Property
-				.get("UNICORN_PARAMETER_PREFIX")
-				+ "lang") != null) {
-			templateLang = aHttpServletRequest.getParameterValues(Property
-					.get("UNICORN_PARAMETER_PREFIX")
-					+ "lang")[0];
-		} else {
-			templateLang = chooseTemplateLang(aLocale);
-		}
-
-		mapOfOutputParameter.put("lang", templateLang);
-
+		final String aLocale = convertEnumerationToString(req.getLocales());
 		if (null == aLocale) {
-			aUnicornCall.setLang(Property.get("DEFAULT_LANGUAGE"));
+			aUnicornCall.setLang(langParameter + "," + Property.get("DEFAULT_LANGUAGE"));
 		} else {
-			aUnicornCall.setLang(templateLang + "," + aLocale);
+			aUnicornCall.setLang(langParameter + "," + aLocale);
 		}
 
-		for (final Enumeration<?> aEnumParamName = aHttpServletRequest
+		for (final Enumeration<?> aEnumParamName = req
 				.getParameterNames(); aEnumParamName.hasMoreElements();) {
 			final String sParamName = (String) aEnumParamName.nextElement();
-			final String[] tStringParamValue = aHttpServletRequest
+			final String[] tStringParamValue = req
 					.getParameterValues(sParamName);
 
 			this.addParameter(sParamName, tStringParamValue, aUnicornCall,
@@ -151,7 +120,7 @@
 
 		if (aUnicornCall.getTask() == null) {
 			ObserveAction.logger.error("No task selected.");
-			this.createError(aHttpServletResponse, new NoTaskException(),
+			this.createError(resp, new NoTaskException(),
 					mapOfSpecificParameter, mapOfOutputParameter);
 			return;
 		}
@@ -159,12 +128,12 @@
 		try {
 			aUnicornCall.doTask();
 
-			this.createOutput(aHttpServletResponse, aUnicornCall,
+			this.createOutput(resp, aUnicornCall,
 					mapOfSpecificParameter, mapOfOutputParameter);
 		} catch (final Exception aException) {
 			ObserveAction.logger.error("Exception : " + aException.getMessage(),
 					aException);
-			this.createError(aHttpServletResponse, aException,
+			this.createError(resp, aException,
 					mapOfSpecificParameter, mapOfOutputParameter);
 		}
 	}
@@ -176,42 +145,42 @@
 	 *      javax.servlet.http.HttpServletResponse)
 	 */
 	@Override
-	protected void doPost(final HttpServletRequest aHttpServletRequest,
-			final HttpServletResponse aHttpServletResponse)
+	protected void doPost(final HttpServletRequest req,
+			final HttpServletResponse resp)
 			throws ServletException, IOException {
 		ObserveAction.logger.trace("doPost");
-
+		
 		// Check that we have a file upload request
-		final boolean bIsMultipart = ServletFileUpload
-				.isMultipartContent(new ServletRequestContext(
-						aHttpServletRequest));
+		final boolean bIsMultipart = ServletFileUpload.isMultipartContent(new ServletRequestContext(req));
 
 		if (!bIsMultipart) {
-			this.doGet(aHttpServletRequest, aHttpServletResponse);
+			this.doGet(req, resp);
 			return;
 		}
-
-		// Parse the request
-		final List<?> listOfItem;
-
-		final UnicornCall aUnicornCall = new UnicornCall();
-
+		
+		// Language negotiation
+		String langParameter = req.getParameter(Property.get("UNICORN_PARAMETER_PREFIX") + "lang");
+		if (langParameter == null || !Framework.getLanguageProperties().containsKey(langParameter))
+			langParameter = Language.negociate(req.getLocales());
+		
 		// Variables related to the output
 		final Map<String, String> mapOfOutputParameter = new Hashtable<String, String>();
 		mapOfOutputParameter.put("output", "simple");
 		mapOfOutputParameter.put("format", "xhtml10");
 		mapOfOutputParameter.put("charset", "UTF-8");
 		mapOfOutputParameter.put("mimetype", "text/html");
-
-		// Returns the preferred Locale that the client will accept content in,
-		// based on the Accept-Language header
-		final String aLocale = convertEnumerationToString(aHttpServletRequest
-				.getLocales());
-
-		// Language of the template
-		// ucn_lang is a parameter which is define in xx_index.html.vm.
-		// It is an hidden parameter of a form.
-		String templateLang = null;
+		mapOfOutputParameter.put("lang", langParameter);
+		
+		final UnicornCall aUnicornCall = new UnicornCall();
+		final String aLocale = convertEnumerationToString(req.getLocales());
+		if (null == aLocale) {
+			aUnicornCall.setLang(langParameter + "," + Property.get("DEFAULT_LANGUAGE"));
+		} else {
+			aUnicornCall.setLang(langParameter + "," + aLocale);
+		}
+		
+		// Parse the request
+		final List<?> listOfItem;
 
 		ObserveAction.logger.trace("doPost");
 
@@ -220,7 +189,7 @@
 		FileItem aFileItemUploaded = null;
 
 		try {
-			listOfItem = ObserveAction.upload.parseRequest(aHttpServletRequest);
+			listOfItem = ObserveAction.upload.parseRequest(req);
 
 			// Process the uploaded items
 			for (final Iterator<?> aIterator = listOfItem.iterator(); aIterator
@@ -228,10 +197,6 @@
 				final FileItem aFileItem = (FileItem) aIterator.next();
 				if (aFileItem.isFormField()) {
 
-					if (aFileItem.getFieldName().equals("ucn_lang")) {
-						templateLang = aFileItem.getString();
-					}
-
 					addParameter(aFileItem.getFieldName(), aFileItem
 							.getString(), aUnicornCall, mapOfSpecificParameter,
 							mapOfOutputParameter);
@@ -244,36 +209,24 @@
 					aUnicornCall.setEnumInputMethod(EnumInputMethod.UPLOAD);
 				}
 			}
-
-			if (templateLang == null) {
-				templateLang = chooseTemplateLang(aLocale);
-			}
-
-			if (null == aLocale) {
-				aUnicornCall.setLang(Property.get("DEFAULT_LANGUAGE"));
-			} else {
-				aUnicornCall.setLang(templateLang + "," + aLocale);
-			}
-			mapOfOutputParameter.put("lang", templateLang);
-
 		}
 
 		catch (final FileUploadException aFileUploadException) {
 			ObserveAction.logger.error("FileUploadException : "
 					+ aFileUploadException.getMessage(), aFileUploadException);
-			this.createError(aHttpServletResponse, aFileUploadException,
+			this.createError(resp, aFileUploadException,
 					mapOfSpecificParameter, mapOfOutputParameter);
 		}
 
 		try {
 			aUnicornCall.doTask();
 
-			this.createOutput(aHttpServletResponse, aUnicornCall,
+			this.createOutput(resp, aUnicornCall,
 					mapOfSpecificParameter, mapOfOutputParameter);
 		} catch (final Exception aException) {
 			ObserveAction.logger.error("Exception : " + aException.getMessage(),
 					aException);
-			this.createError(aHttpServletResponse, aException,
+			this.createError(resp, aException,
 					mapOfSpecificParameter, mapOfOutputParameter);
 		} finally {
 			if ("true".equals(Property.get("DELETE_UPLOADED_FILES"))
@@ -460,26 +413,6 @@
 	}
 
 	/**
-	 * This method returns the first language of the accept language list which
-	 * is equal to one of available index template language
-	 * 
-	 * @param aLocale
-	 * @return The selected language or the default language.
-	 */
-	private String chooseTemplateLang(String aLocale) {
-		String[] tabLang = aLocale.split(";|,");
-		for (int i = 0; i < tabLang.length; i++) {
-			if (Framework.outputLang.contains(tabLang[i])) {
-				return tabLang[i];
-			} else if (Framework.outputLang.contains(tabLang[i].split("-")[0])) {
-				return tabLang[i].split("-")[0];
-			}
-		}
-
-		return Property.get("DEFAULT_LANGUAGE");
-	}
-
-	/**
 	 * Converts an Enumeration object to a string, the terms being separated by
 	 * a coma.
 	 * 
Received on Monday, 24 August 2009 11:17:18 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 7 January 2015 15:17:38 UTC