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

2006/unicorn/src/org/w3c/unicorn/response/impl DefaultResponseXBeans.java,1.2,1.3 OldResponseXBeans.java,1.1,1.2

From: Thomas Gambet via cvs-syncmail <cvsmail@w3.org>
Date: Mon, 19 Oct 2009 16:20:20 +0000
To: www-validator-cvs@w3.org
Message-Id: <E1Mzuy4-0004ae-JI@lionel-hutz.w3.org>
Update of /sources/public/2006/unicorn/src/org/w3c/unicorn/response/impl
In directory hutz:/tmp/cvs-serv17630/src/org/w3c/unicorn/response/impl

Modified Files:
	DefaultResponseXBeans.java OldResponseXBeans.java 
Log Message:
implements a private Iterable<Message> class to iterate over the messages based on a level and/or uri constraint

Index: DefaultResponseXBeans.java
===================================================================
RCS file: /sources/public/2006/unicorn/src/org/w3c/unicorn/response/impl/DefaultResponseXBeans.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- DefaultResponseXBeans.java	19 Oct 2009 12:42:07 -0000	1.2
+++ DefaultResponseXBeans.java	19 Oct 2009 16:20:18 -0000	1.3
@@ -8,7 +8,10 @@
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Date;
+import java.util.Hashtable;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.xmlbeans.XmlException;
 import org.apache.xmlbeans.XmlObject;
@@ -30,9 +33,9 @@
 	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 int errorCount = 0;
+	private int warningCount = 0;
+	private int infoCount = 0;
 	
 	private List<Group> groups = new ArrayList<Group>();
 	
@@ -48,17 +51,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"));
@@ -84,13 +78,13 @@
 					m.setGroupName(list.getGroup());
 				switch (m.getType()) {
 				case Message.ERROR:
-					errorMessages.add(m);
+					errorCount++;
 					break;
 				case Message.WARNING:
-					warningMessages.add(m);
+					warningCount++;
 					break;
 				case Message.INFO:
-					infoMessages.add(m);
+					infoCount++;
 					break;
 				}
 				messages.add(m);
@@ -105,17 +99,18 @@
 			
 			switch (m.getType()) {
 			case Message.ERROR:
-				errorMessages.add(m);
+				errorCount++;
 				break;
 			case Message.WARNING:
-				warningMessages.add(m);
+				warningCount++;
 				break;
 			case Message.INFO:
-				infoMessages.add(m);
+				infoCount++;
 				break;
 			}
 			messages.add(m);
 		}
+		
 	}
 	
 	public Date getDate() {
@@ -142,32 +137,32 @@
 		return null;
 	}
 	
-	public List<Message> getMessages() {
+	public Iterable<Message> getMessages() {
 		return messages;
 	}
 	
-	public List<Message> getErrorMessages() {
-		return errorMessages;
+	public Iterable<Message> getErrorMessages() {
+		return getMessages(null, Message.ERROR);
 	}
 	
-	public List<Message> getInfoMessages() {
-		return infoMessages;
+	public Iterable<Message> getInfoMessages() {
+		return getMessages(null, Message.INFO);
 	}
 	
-	public List<Message> getWarningMessages() {
-		return warningMessages;
+	public Iterable<Message> getWarningMessages() {
+		return getMessages(null, Message.WARNING);
 	}
 	
 	public int getErrorCount() {
-		return errorMessages.size();
+		return errorCount;
 	}
 	
 	public int getWarningCount() {
-		return warningMessages.size();
+		return warningCount;
 	}
 	
 	public int getInfoCount() {
-		return infoMessages.size();
+		return infoCount;
 	}
 
 	public List<Group> getGroups() {		
@@ -257,4 +252,80 @@
 		return false;
 	}
 
+	public Iterable<Message> getMessages(String uri, int type) {
+		return new MessageIterable(uri, type);
+	}
+	
+	private class MessageIterable implements Iterable<Message> {
+
+		private int index = 0;
+		private Integer type;
+		private String uri;
+		
+		public MessageIterable(String uri, Integer type) {
+			this.uri = uri;
+			this.type = type;
+		}
+		
+		public Iterator<Message> iterator() {
+			return new Iterator<Message>() {
+				public boolean hasNext() {
+					int x = index;
+					while (x < messages.size()) {
+						if ((uri == null || messages.get(x).getURI().equals(uri)) && 
+							(type == null || messages.get(x).getType() == type))
+							return true;
+						x++;
+					}
+					return false;
+				}
+
+				public Message next() {
+					while (index < messages.size()) {
+						if ((uri == null || messages.get(index).getURI().equals(uri)) && 
+							(type == null || messages.get(index).getType() == type)) {
+							index++;
+							return messages.get(index - 1);
+						}
+						index++;
+					}
+					return null;
+				}
+
+				public void remove() {
+					return;
+				}
+			};
+		}
+	}
+
+	public Iterable<Message> getErrorMessages(String uri) {
+		return getMessages(uri, Message.ERROR);
+	}
+
+	public Iterable<Message> getInfoMessages(String uri) {
+		return getMessages(uri, Message.INFO);
+	}
+
+	public Iterable<Message> getMessages(String uri, Integer type) {
+		return new MessageIterable(uri, type);
+	}
+
+	public Map<String, Iterable<Message>> getURISortedMessages(int type) {
+		List<String> uris = new ArrayList<String>();
+		for (Message mess : getMessages(null, type)) {
+			if (!uris.contains(mess.getURI()))
+				uris.add(mess.getURI());
+		}
+		Map<String, Iterable<Message>> sortedMap = new Hashtable<String, Iterable<Message>>();
+		for (String uri : uris) {
+			sortedMap.put(uri, getMessages(uri, type));
+		}
+		return sortedMap;
+	}
+
+	public Iterable<Message> getWarningMessages(String uri) {
+		return getMessages(uri, Message.WARNING);
+	}
+
 }

Index: OldResponseXBeans.java
===================================================================
RCS file: /sources/public/2006/unicorn/src/org/w3c/unicorn/response/impl/OldResponseXBeans.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- OldResponseXBeans.java	19 Oct 2009 12:41:24 -0000	1.1
+++ OldResponseXBeans.java	19 Oct 2009 16:20:18 -0000	1.2
@@ -4,7 +4,10 @@
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Date;
+import java.util.Hashtable;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.xmlbeans.XmlException;
 import org.apache.xmlbeans.XmlOptions;
@@ -28,9 +31,13 @@
 	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 int errorCount = 0;
+	private int warningCount = 0;
+	private int infoCount = 0;
+	
+	//private List<Message> errorMessages = new ArrayList<Message>();
+	//private List<Message> warningMessages = new ArrayList<Message>();
+	//private List<Message> infoMessages = new ArrayList<Message>();
 	
 	private String requestURI;
 	
@@ -66,7 +73,8 @@
 					else
 						mess.setURI(or.getUri());
 					messages.add(mess);
-					errorMessages.add(mess);
+					errorCount++;
+					//errorMessages.add(mess);
 				}
 			}
 		}
@@ -79,7 +87,8 @@
 					else
 						mess.setURI(or.getUri());
 					messages.add(mess);
-					infoMessages.add(mess);
+					infoCount++;
+					//infoMessages.add(mess);
 				}
 			}
 		}
@@ -92,7 +101,8 @@
 					else
 						mess.setURI(or.getUri());
 					messages.add(mess);
-					warningMessages.add(mess);
+					warningCount++;
+					//warningMessages.add(mess);
 				}
 			}
 		}
@@ -113,14 +123,6 @@
 		return null;
 	}
 
-	public int getErrorCount() {
-		return errorMessages.size();
-	}
-
-	public List<Message> getErrorMessages() {
-		return errorMessages;
-	}
-
 	public List<Group> getGroupChildren(Group group) {
 		return null;
 	}
@@ -138,14 +140,30 @@
 		}
 	}
 
+	public int getErrorCount() {
+		return errorCount;
+	}
+
+	public Iterable<Message> getErrorMessages() {
+		return getMessages(null, Message.ERROR);
+	}
+	
 	public int getInfoCount() {
-		return infoMessages.size();
+		return infoCount;
 	}
 
-	public List<Message> getInfoMessages() {
-		return infoMessages;
+	public Iterable<Message> getInfoMessages() {
+		return getMessages(null, Message.INFO);
+	}
+
+	public int getWarningCount() {
+		return warningCount;
 	}
 
+	public Iterable<Message> getWarningMessages() {
+		return getMessages(null, Message.WARNING);
+	}
+	
 	public List<Message> getMessages() {
 		return messages;
 	}
@@ -168,14 +186,6 @@
 		return or.getUri();
 	}
 
-	public int getWarningCount() {
-		return warningMessages.size();
-	}
-
-	public List<Message> getWarningMessages() {
-		return warningMessages;
-	}
-
 	public boolean hasGroups() {
 		return false;
 	}
@@ -190,7 +200,6 @@
 
 	public void setObserverId(String obsId) {
 		observerID = obsId;
-		
 	}
 
 	public String getObserverID() {
@@ -206,5 +215,77 @@
 			return true;
 		return false;
 	}
+	
+	public Iterable<Message> getMessages(String uri, Integer type) {
+		return new MessageIterable(uri, type);
+	}
+
+	protected class MessageIterable implements Iterable<Message> {
 
+		private int index = 0;
+		private Integer type;
+		private String uri;
+		
+		public MessageIterable(String uri, Integer type) {
+			this.uri = uri;
+			this.type = type;
+		}
+		
+		public Iterator<Message> iterator() {
+			return new Iterator<Message>() {
+				public boolean hasNext() {
+					int x = index;
+					while (x < messages.size()) {
+						if ((uri == null || messages.get(x).getURI().equals(uri)) && 
+							(type == null || messages.get(x).getType() == type))
+							return true;
+						x++;
+					}
+					return false;
+				}
+
+				public Message next() {
+					while (index < messages.size()) {
+						if ((uri == null || messages.get(index).getURI().equals(uri)) && 
+							(type == null || messages.get(index).getType() == type)) {
+							index++;
+							return messages.get(index - 1);
+						}
+						index++;
+					}
+					return null;
+				}
+
+				public void remove() {
+					return;
+				}
+			};
+		}
+	}
+
+	public Iterable<Message> getErrorMessages(String uri) {
+		return getMessages(uri, Message.ERROR);
+	}
+
+	public Iterable<Message> getInfoMessages(String uri) {
+		return getMessages(uri, Message.INFO);
+	}
+	
+	public Iterable<Message> getWarningMessages(String uri) {
+		return getMessages(uri, Message.WARNING);
+	}
+
+	public Map<String, Iterable<Message>> getURISortedMessages(int type) {
+		List<String> uris = new ArrayList<String>();
+		for (Message mess : getMessages(null, type)) {
+			if (!uris.contains(mess.getURI()))
+				uris.add(mess.getURI());
+		}
+		Map<String, Iterable<Message>> sortedMap = new Hashtable<String, Iterable<Message>>();
+		for (String uri : uris) {
+			sortedMap.put(uri, getMessages(uri, type));
+		}
+		return sortedMap;
+	}
+	
 }
Received on Monday, 19 October 2009 16:20:22 UTC

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