W3C home > Mailing lists > Public > www-validator-cvs@w3.org > February 2008

2006/unicorn/org/w3c/unicorn/response/parser ResponseParserFactory.java,NONE,1.1 ResponseParser.java,NONE,1.1 DefaultParser.java,NONE,1.1

From: Hiep Duong via cvs-syncmail <cvsmail@w3.org>
Date: Wed, 20 Feb 2008 15:10:00 +0000
To: www-validator-cvs@w3.org
Message-Id: <E1JRqa8-0007zM-5n@lionel-hutz.w3.org>

Update of /sources/public/2006/unicorn/org/w3c/unicorn/response/parser
In directory hutz:/tmp/cvs-serv30553/org/w3c/unicorn/response/parser

Added Files:
	ResponseParserFactory.java ResponseParser.java 
	DefaultParser.java 
Log Message:
simplified observationresponse and generalize output format (now we can map a format type what defnied in the contract to a ResponseParser)

--- NEW FILE: ResponseParserFactory.java ---
package org.w3c.unicorn.response.parser;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.unicorn.Framework;
import org.w3c.unicorn.response.Response;
import org.xml.sax.SAXException;

public class ResponseParserFactory {
	public static ResponseParser createResponseParser(String responseType) throws SAXException, ParserConfigurationException, IOException {
		ResponseParser rsp = Framework.mapOfReponseParser.get(responseType);
		if (rsp==null)
			rsp = new DefaultParser();
		return rsp;
	}
	
	public static Response parse(InputStream is, String responseType) {
		try {
			Response response = createResponseParser(responseType).parse(is);
			return response;
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	} 
}

--- NEW FILE: ResponseParser.java ---
package org.w3c.unicorn.response.parser;

import java.io.InputStream;

import org.w3c.unicorn.response.Response;

public interface ResponseParser {
	public Response parse(InputStream inputStream);
}

--- NEW FILE: DefaultParser.java ---
package org.w3c.unicorn.response.parser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.unicorn.generated.observationresponse.Errorlist;
import org.w3c.unicorn.generated.observationresponse.Errors;
import org.w3c.unicorn.generated.observationresponse.Infolist;
import org.w3c.unicorn.generated.observationresponse.Informations;
import org.w3c.unicorn.generated.observationresponse.Observationresponse;
import org.w3c.unicorn.generated.observationresponse.Warninglist;
import org.w3c.unicorn.generated.observationresponse.Warnings;
import org.w3c.unicorn.response.A;
import org.w3c.unicorn.response.Code;
import org.w3c.unicorn.response.Error;
import org.w3c.unicorn.response.Img;
import org.w3c.unicorn.response.Info;
import org.w3c.unicorn.response.Longmessage;
import org.w3c.unicorn.response.Warning;
import org.w3c.unicorn.response.Response;
import org.w3c.unicorn.response.Result;
import org.w3c.unicorn.util.LocalizedString;

public class DefaultParser implements ResponseParser {
	protected static final Log logger = LogFactory.getLog("org.w3c.unicorn.response.parser.DefaultParser");
	private static JAXBContext aJAXBContext = null;
	private static Unmarshaller aUnmarshaller = null;
	
	static {
		try {
			aJAXBContext = JAXBContext.newInstance("org.w3c.unicorn.generated.observationresponse");
			aUnmarshaller = aJAXBContext.createUnmarshaller();
		}
		catch (final JAXBException e) {
			logger.error("JAXBException : " + e.getMessage(), e);
			e.printStackTrace();
		}
	}
	
	public Response parse(InputStream inputStream) {
		try {
			return swap((Observationresponse)(aUnmarshaller.unmarshal(inputStream)));
		} catch (JAXBException e) {
			logger.error("JAXBException : " + e.getMessage(), e);
			e.printStackTrace();
			return null;
		}
	}
	
	private List<LocalizedString> swapListMessage(List<String> x, String lang) {
		List<LocalizedString> y = new ArrayList<LocalizedString>();
		for (Object ox : x) {
			String cox = (String)ox;
			LocalizedString coy = new LocalizedString(cox, lang);
			y.add(coy);
		}
		return y;
	}

	private List<Object> swap(List<Object> x, String lang) {
		List<Object> y = new ArrayList<Object>();
		for (Object ox : x) {
			if (ox instanceof String) {
				String cox = (String)ox;
				LocalizedString coy = new LocalizedString(cox, lang);
				y.add(coy);
			} 
			else if (ox instanceof org.w3c.unicorn.generated.observationresponse.A) {
				org.w3c.unicorn.generated.observationresponse.A cox = (org.w3c.unicorn.generated.observationresponse.A)ox;
				A coy = new A();
				coy.setHref(cox.getHref());
				coy.setContent(swap(cox.getContent(), lang));
				y.add(coy);
			}
			else if (ox instanceof org.w3c.unicorn.generated.observationresponse.Code) {
				org.w3c.unicorn.generated.observationresponse.Code cox = (org.w3c.unicorn.generated.observationresponse.Code)ox;
				Code coy = new Code();
				coy.setContent(swap(cox.getContent(), lang));
				y.add(coy);
			}
			else if (ox instanceof org.w3c.unicorn.generated.observationresponse.Img) {
				org.w3c.unicorn.generated.observationresponse.Img cox = (org.w3c.unicorn.generated.observationresponse.Img)ox;
				Img coy = new Img();
				coy.setAlt(cox.getAlt());
				coy.setHeight(cox.getHeight());
				coy.setLongdesc(cox.getLongdesc());
				coy.setName(cox.getName());
				coy.setSrc(cox.getSrc());
				coy.setWidth(cox.getWidth());
				y.add(coy);
			}
		}
		return y;
	}
	
	private Longmessage swap(org.w3c.unicorn.generated.observationresponse.Longmessage x, String lang) {
		Longmessage y = new Longmessage();
		y.setContent(swap(x.getContent(),lang));
		return y;
	}
	
	private List<Longmessage> swapListLongmessage(List<org.w3c.unicorn.generated.observationresponse.Longmessage> x, String lang) {
		List<Longmessage> y = new ArrayList<Longmessage>();
		for (Object ox : x) {
			org.w3c.unicorn.generated.observationresponse.Longmessage cox = (org.w3c.unicorn.generated.observationresponse.Longmessage)ox;
			Longmessage coy = swap(cox, lang);
			y.add(coy);
		}
		return y;
	}

	private Warning swap(org.w3c.unicorn.generated.observationresponse.Warning x, String lang) {
		Warning y = new Warning();
		y.setLine(x.getLine());
		y.setColumn(x.getColumn());
		y.setContext(x.getContext());
		y.setLevel(x.getLevel());
		y.setMessage(swapListMessage(x.getMessage(), lang));
		y.setLongmessage(swapListLongmessage(x.getLongmessage(), lang));
		return y;
	}

	private Error swap(org.w3c.unicorn.generated.observationresponse.Error x, String lang) {
		Error y = new Error();
		y.setLine(x.getLine());
		y.setColumn(x.getColumn());
		y.setErrortype(x.getErrortype());
		y.setContext(x.getContext());
		y.setMessage(swapListMessage(x.getMessage(), lang));
		y.setLongmessage(swapListLongmessage(x.getLongmessage(), lang));
		return y;
	}
	
	private Info swap(org.w3c.unicorn.generated.observationresponse.Info x, String lang) {
		Info y = new Info();
		y.setLine(x.getLine());
		y.setColumn(x.getColumn());
		y.setContext(x.getContext());
		y.setMessage(swapListMessage(x.getMessage(), lang));
		y.setLongmessage(swapListLongmessage(x.getLongmessage(), lang));
		return y;
	}
	
	private Response swap(Observationresponse or) {
		Response res = new Response();
		res.setUri(or.getUri());
		res.setCheckedby(or.getCheckedby());
		res.setVersion(or.getVersion());
		res.setDate(or.getDate());
		res.setPassed(or.isPassed());
		
		//Fill res.result
		org.w3c.unicorn.generated.observationresponse.Result rrr = or.getResult();
		if (rrr!=null) {
			Warnings warnings = rrr.getWarnings();
			if (warnings!=null && warnings.getWarninglist()!=null) {
				for (Warninglist wl : warnings.getWarninglist()) {
					String lang = warnings.getLang();
					Result r = new Result(lang, wl.getUri());
					for (org.w3c.unicorn.generated.observationresponse.Warning w : wl.getWarning()) {
						r.getWarnings().add(swap(w, lang));
					}
					res.addResult(r);
				}
			}
			
			Errors errors = rrr.getErrors();
			if (errors!=null && errors.getErrorlist()!=null) {
				for (Errorlist wl : errors.getErrorlist()) {
					String lang = errors.getLang();
					Result r = new Result(errors.getLang(),wl.getUri());
					for (org.w3c.unicorn.generated.observationresponse.Error w : wl.getError()) {
						r.getErrors().add(swap(w, lang));
					}
					res.addResult(r);
				}		
			}
		
			Informations informations = rrr.getInformations();
			if (informations!=null && informations.getInfolist()!=null) {
				String lang = informations.getLang();
				for (Infolist wl : informations.getInfolist()) {
					Result r = new Result(informations.getLang(), wl.getUri());
					for (org.w3c.unicorn.generated.observationresponse.Info w : wl.getInfo()) {
						r.getInfos().add(swap(w, lang));
					}
					res.addResult(r);
				}
			}
		}
		
		
		return res;
	}
	
}
Received on Wednesday, 20 February 2008 15:10:07 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Thursday, 26 April 2012 12:55:00 GMT