- From: Thomas Gambet via cvs-syncmail <cvsmail@w3.org>
- Date: Fri, 16 Oct 2009 16:24:34 +0000
- To: www-validator-cvs@w3.org
Update of /sources/public/2006/unicorn/src/org/w3c/unicorn/response/impl In directory hutz:/tmp/cvs-serv21004/src/org/w3c/unicorn/response/impl Added Files: DefaultGroupXBeans.java DefaultResponseXBeans.java DefaultMessageXBeans.java DefaultContextXBeans.java Log Message: response objects implementation for xmlbeans and observationresponse format --- NEW FILE: DefaultMessageXBeans.java --- // $Id: DefaultMessageXBeans.java,v 1.1 2009/10/16 16:24:31 tgambet Exp $ // Author: Thomas Gambet // (c) COPYRIGHT MIT, ERCIM and Keio, 2009. // Please first read the full copyright statement in file COPYRIGHT.html package org.w3c.unicorn.response.impl; import java.util.ArrayList; import java.util.List; import org.apache.xmlbeans.XmlOptions; import org.w3.unicorn.x2009.x10.observationresponse.ContextType; import org.w3.unicorn.x2009.x10.observationresponse.MessageType; import org.w3c.unicorn.response.Context; import org.w3c.unicorn.response.Message; import org.w3c.unicorn.response.impl.DefaultContextXBeans; public class DefaultMessageXBeans implements Message { private List<Context> contexts = new ArrayList<Context>(); private String description; private String title; private int level; private int type; private String uri; private String group; public DefaultMessageXBeans(MessageType message) { title = message.getTitle(); if (message.isSetRef()) uri = message.getRef(); if (message.isSetLevel()) level = message.getLevel(); if (message.getType().equalsIgnoreCase("error")) type = Message.ERROR; if (message.getType().equalsIgnoreCase("warning")) type = Message.WARNING; if (message.getType().equalsIgnoreCase("info")) type = Message.INFO; if (message.isSetGroup()) group = message.getGroup(); if (message.isSetDescription()) description = message.getDescription().xmlText(new XmlOptions().setUseDefaultNamespace() .setSavePrettyPrint()).replaceAll("</?xml-fragment[^>]*>", "").replaceAll("xmlns=\".*\"", ""); for (ContextType context : message.getContextList()) contexts.add(new DefaultContextXBeans(context)); System.out.println("Message ----------------------"); System.out.println("Title: " + title); System.out.println("Description: " + description); System.out.println("level: " + level); System.out.println("type: " + type); System.out.println("uri: " + uri); System.out.println("group: " + group); System.out.println("contexts.size(): " + contexts.size()); System.out.println("------------------------------"); } public List<Context> getContexts() { return contexts; } public String getDescription() { return description; } public int getLevel() { return level; } public String getTitle() { return title; } public int getType() { return type; } public String getURI() { return uri; } public void setURI(String uri) { this.uri = uri; } public String getGroupName() { return group; } public void setGroupName(String group) { this.group = group; } } --- NEW FILE: DefaultResponseXBeans.java --- // $Id: DefaultResponseXBeans.java,v 1.1 2009/10/16 16:24:31 tgambet Exp $ // Author: Thomas Gambet // (c) COPYRIGHT MIT, ERCIM and Keio, 2009. // Please first read the full copyright statement in file COPYRIGHT.html package org.w3c.unicorn.response.impl; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.GregorianCalendar; import java.util.List; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.XmlOptions; import org.w3.unicorn.x2009.x10.observationresponse.GroupType; import org.w3.unicorn.x2009.x10.observationresponse.ListType; import org.w3.unicorn.x2009.x10.observationresponse.MessageType; import org.w3.unicorn.x2009.x10.observationresponse.ObservationresponseDocument; import org.w3.unicorn.x2009.x10.observationresponse.ObservationresponseDocument.Observationresponse; import org.w3c.unicorn.Framework; import org.w3c.unicorn.response.Group; import org.w3c.unicorn.response.Message; import org.w3c.unicorn.response.Response; import org.w3c.unicorn.exceptions.UnicornException; public class DefaultResponseXBeans implements Response { private ObservationresponseDocument ord; private Observationresponse or; private List<Message> messages = new ArrayList<Message>(); private List<Message> errorMessages = new ArrayList<Message>(); private List<Message> warningMessages = new ArrayList<Message>(); private List<Message> infoMessages = new ArrayList<Message>(); private List<Group> groups = new ArrayList<Group>(); private String requestURI; private String observerID; public DefaultResponseXBeans(InputStream is, String charset) throws UnicornException { if (charset == null) charset = "UTF-8"; try { ord = ObservationresponseDocument.Factory.parse(is, new XmlOptions().setCharacterEncoding(charset)); or = ord.getObservationresponse(); if (!or.validate()) throw new UnicornException(new org.w3c.unicorn.util.Message(2, "$message_response_validation_error")); System.out.println("Response ----------------------"); System.out.println("Date: " + getDate()); System.out.println("URI: " + getURI()); System.out.println("Status: " + getStatus()); System.out.println("Rating: " + getRating()); System.out.println("-------------------------------"); } catch (XmlException e) { if (e.getMessage().contains("is not a valid observationresponse")) throw new UnicornException(new org.w3c.unicorn.util.Message(org.w3c.unicorn.util.Message.ERROR, "$message_observer_invalid_response_schema")); else throw new UnicornException(new org.w3c.unicorn.util.Message(e)); } catch (IOException e) { throw new UnicornException(new org.w3c.unicorn.util.Message(e)); } for (GroupType group : or.getGroupList()) groups.add(new DefaultGroupXBeans(group)); for (ListType list : or.getListList()) { for (MessageType message : list.getMessageList()) { DefaultMessageXBeans m = new DefaultMessageXBeans(message); if (m.getURI() == null) { if (list.isSetRef()) m.setURI(list.getRef()); else m.setURI(or.getRef().getStringValue()); } if (list.isSetGroup() && m.getGroupName() == null) m.setGroupName(list.getGroup()); switch (m.getType()) { case Message.ERROR: errorMessages.add(m); break; case Message.WARNING: warningMessages.add(m); break; case Message.INFO: infoMessages.add(m); break; } messages.add(m); } } for (MessageType message : or.getMessageList()) { DefaultMessageXBeans m = new DefaultMessageXBeans(message); if (m.getURI() == null) m.setURI(getURI()); switch (m.getType()) { case Message.ERROR: errorMessages.add(m); break; case Message.WARNING: warningMessages.add(m); break; case Message.INFO: infoMessages.add(m); break; } messages.add(m); } } public Date getDate() { String[] s = or.getDate().getStringValue().split("-"); int year = Integer.parseInt(s[0]); int month = Integer.parseInt(s[1]); int day = Integer.parseInt(s[2]); return new GregorianCalendar(year, month - 1, day).getTime(); } public String getURI() { return or.getRef().getStringValue(); } public int getStatus() { if (or.isSetStatus() && or.getStatus().getValue().equalsIgnoreCase("passed")) { return PASSED; } else if (or.isSetStatus() && or.getStatus().getValue().equalsIgnoreCase("failed") || getErrorCount() > 0) { return FAILED; } else { return UNDEF; } } public Integer getRating() { if(or.isSetStatus() && or.getStatus().isSetRating()) return or.getStatus().getRating(); return null; } public List<Message> getMessages() { return messages; } public List<Message> getErrorMessages() { return errorMessages; } public List<Message> getInfoMessages() { return infoMessages; } public List<Message> getWarningMessages() { return warningMessages; } public int getErrorCount() { return errorMessages.size(); } public int getWarningCount() { return warningMessages.size(); } public int getInfoCount() { return infoMessages.size(); } public List<Group> getGroups() { return groups; } public boolean isSetRating() { if (getRating() == null) return false; return true; } public void setRequestUri(String uri) { requestURI = uri; } public String getRequestUri() { return requestURI; } public String getHTMLRequestUri() { if (requestURI != null) { String outputParamName = Framework.mapOfObserver.get(observerID).getParamOutputName(); return requestURI.replaceAll("&?" + outputParamName + "=[^&]*", ""); } else { return null; } } public String getObserverID() { return observerID; } public void setObserverId(String obsId) { observerID = obsId; } public boolean hasGroups() { if (groups.size() > 0) return true; return false; } public String[] execQuery(String query) { XmlObject[] objects = or.execQuery(query); String[] result = new String[objects.length]; int i = 0; for (XmlObject obj : objects) { result[i] = obj.toString(); i++; } return result; } public String[] selectPath(String xpath) { XmlObject[] objects = or.selectPath(xpath); String[] result = new String[objects.length]; int i = 0; for (XmlObject obj : objects) { result[i] = obj.toString(); i++; } return result; } public boolean evaluateXPath(String xpathQuery) { XmlObject[] objects = or.selectPath(xpathQuery); if (objects.length > 0) return true; return false; } public List<Group> getGroupChildren(Group group) { String groupName = group.getName(); List<Group> children = new ArrayList<Group>(); for (Group g : groups) { if (g.hasParent() && g.getParentName().equals(groupName)) { children.add(g); } } return children; } } --- NEW FILE: DefaultContextXBeans.java --- // $Id: DefaultContextXBeans.java,v 1.1 2009/10/16 16:24:32 tgambet Exp $ // Author: Thomas Gambet // (c) COPYRIGHT MIT, ERCIM and Keio, 2009. // Please first read the full copyright statement in file COPYRIGHT.html package org.w3c.unicorn.response.impl; import org.w3.unicorn.x2009.x10.observationresponse.ContextType; import org.w3c.unicorn.response.Context; public class DefaultContextXBeans implements Context { private ContextType context; private String value; private Integer lineMin; private Integer lineMax; private Integer colMin; private Integer colMax; public DefaultContextXBeans(ContextType context) { this.context = context; if (context.isSetLineRange()) { String[] values = context.getLineRange().split("-"); lineMin = Integer.parseInt(values[0]); lineMax = Integer.parseInt(values[1]); } if (context.isSetColumnRange()) { String[] values = context.getColumnRange().split("-"); colMin = Integer.parseInt(values[0]); colMax = Integer.parseInt(values[1]); } value = context.xmlText().replaceAll("</?xml-fragment[^>]*>", ""); System.out.println("Context ----------------------"); System.out.println(getLine()); System.out.println(getColumn()); System.out.println(getLineMin()); System.out.println(getLineMax()); System.out.println(getColumnMin()); System.out.println(getColumnMax()); System.out.println(getContext()); System.out.println(getOffset()); System.out.println(getPosition()); System.out.println(getURI()); System.out.println("------------------------------"); } public Integer getLine() { if (context.isSetLine()) return context.getLine().intValue(); return null; } public Integer getColumn() { if (context.isSetColumn()) return context.getColumn().intValue(); return null; } public Integer getLineMin() { return lineMin; } public Integer getLineMax() { return lineMax; } public Integer getColumnMin() { return colMin; } public Integer getColumnMax() { return colMax; } public String getContext() { return value; } public Integer getOffset() { if (context.isSetOffset()) return context.getOffset().intValue(); return null; } public String getPosition() { return context.getPosition(); } public String getURI() { return context.getRef(); } } --- NEW FILE: DefaultGroupXBeans.java --- // $Id: DefaultGroupXBeans.java,v 1.1 2009/10/16 16:24:31 tgambet Exp $ // Author: Thomas Gambet // (c) COPYRIGHT MIT, ERCIM and Keio, 2009. // Please first read the full copyright statement in file COPYRIGHT.html package org.w3c.unicorn.response.impl; import org.apache.xmlbeans.XmlOptions; import org.w3.unicorn.x2009.x10.observationresponse.GroupType; import org.w3c.unicorn.response.Group; public class DefaultGroupXBeans implements Group { private String title; private String description; private String name; private String parent; public DefaultGroupXBeans(GroupType group) { if (group.isSetDescription() && group.getDescription().validate()) description = group.getDescription().xmlText(new XmlOptions().setUseDefaultNamespace() .setSavePrettyPrint()).replaceAll("</?xml-fragment[^>]*>", "").replaceAll("xmlns=\".*\"", ""); title = group.getTitle(); name = group.getName(); if (group.isSetParent()) parent = group.getParent(); System.out.println("Group ----------------------"); System.out.println("Title: " + title); System.out.println("Description: " + description); System.out.println("Name: " + name); System.out.println("Parent: " + parent); System.out.println("----------------------------"); } public String getDescription() { return description; } public String getName() { return name; } public String getParentName() { return parent; } public String getTitle() { return title; } public boolean hasParent() { if (parent != null) return true; return false; } }
Received on Friday, 16 October 2009 16:24:35 UTC