--- /dev/null
+// ResourceLister.java\r
+// $Id: ResourceLister.java,v 1.1 2010/06/15 12:21:50 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 2002.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.tools.offline.browse ;\r
+\r
+import org.w3c.tools.resources.serialization.Serializer;\r
+import org.w3c.tools.resources.Resource ;\r
+import org.w3c.tools.resources.Attribute ;\r
+import org.w3c.tools.resources.serialization.ResourceDescription ;\r
+import org.w3c.tools.resources.serialization.AttributeDescription ;\r
+import org.w3c.tools.resources.AttributeHolder ;\r
+\r
+\r
+import java.io.BufferedReader;\r
+import java.io.File;\r
+import java.io.FileReader;\r
+import java.io.FilenameFilter;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.ObjectInputStream;\r
+import java.io.PrintStream;\r
+import java.io.Reader;\r
+import java.io.Serializable;\r
+import java.io.Writer;\r
+\r
+import java.lang.String;\r
+\r
+import org.apache.oro.text.regex.Perl5Matcher;\r
+import org.apache.oro.text.regex.Perl5Compiler;\r
+import org.apache.oro.text.regex.Pattern;\r
+import org.apache.oro.text.regex.MalformedPatternException;\r
+import org.apache.oro.text.perl.Perl5Util;\r
+\r
+/** \r
+ * The resource lister class.\r
+ * It acts on resources independently, and perform action on \r
+ * resources and frames as isolated entities.\r
+ */\r
+public class ResourceLister {\r
+\r
+ protected static Perl5Matcher pmatcher = new Perl5Matcher() ;\r
+ protected static Perl5Compiler pcompiler = new Perl5Compiler() ;\r
+ protected static Perl5Util putil = new Perl5Util();\r
+\r
+ /**\r
+ * the search and replace pattern.\r
+ */\r
+ protected static Pattern srPattern ;\r
+\r
+ /**\r
+ * Print all attributes of an AttributeHolder.\r
+ * @param ah an AttributeHolder.\r
+ */\r
+ public void printAttributeHolder(AttributeHolder ah){\r
+ Attribute[] att = ah.getAttributes();\r
+ for (int j = 0 ; j < att.length ; j++) {\r
+ Object value = ah.getValue (j,null); \r
+ this.printAttribute(att[j], value);\r
+ }\r
+ }\r
+\r
+ \r
+ /**\r
+ * Print an attribute and its value.\r
+ * @param ad an Attribute\r
+ * @param value its value.\r
+ */\r
+ private void printAttribute(Attribute ad, Object value ){\r
+ System.out.println(attributeToString(ad,value));\r
+ }\r
+\r
+\r
+ /**\r
+ * Format into an attribute=value string.\r
+ * @param ad an Attribute\r
+ * @param value its value.\r
+ * @return the attribute=value string.\r
+ */\r
+ public String attributeToString(Attribute ad, Object value ){\r
+ String concat = ad.getName() +"="+ value;\r
+ return concat;\r
+ }\r
+ \r
+ \r
+ \r
+ /**\r
+ * Print the frames associated to a resource.\r
+ * @param ah an AttributeHolder\r
+ */\r
+ public void printFrames(AttributeHolder ah){\r
+ AttributeHolder[] frames = this.getFramesFromHolder(ah);\r
+ for (int j = 0 ; j < frames.length ; j++) {\r
+ System.out.println("resource frame: ");\r
+ this.printAttributeHolder(frames[j]);\r
+ }\r
+ }\r
+\r
+\r
+ /** \r
+ * Search for a given attribute value in an attribute holder.\r
+ * @param ah the AttributeHolder\r
+ * @param attribname the attribute name\r
+ * @return the attribute value or null if not found.\r
+ */\r
+ private Object searchValue(AttributeHolder ah, String attribname){\r
+ Attribute[] att = ah.getAttributes();\r
+ Object value = null ;\r
+ for (int j = 0 ; j < att.length ; j++) {\r
+ if (att[j].getName().compareTo(attribname)==0){\r
+ value = ah.getValue(j,null);\r
+ }\r
+ }\r
+ return value ;\r
+ }\r
+\r
+ /** \r
+ * Search for key value in an attribute holder.\r
+ * @param ah the AttributeHolder\r
+ * @return the key value or null if not found.\r
+ */\r
+ public Integer getKeyFromHolder(AttributeHolder ah){\r
+ return (Integer)searchValue(ah,"key") ;\r
+ }\r
+\r
+ /** \r
+ * Search for identifier in an attribute holder.\r
+ * @param ah the AttributeHolder\r
+ * @return the identifier.\r
+ */\r
+ public String getIdentFromHolder(AttributeHolder ah){\r
+ return (String)searchValue(ah,"identifier") ;\r
+ }\r
+\r
+ /** \r
+ * Search for repository in an attribute holder.\r
+ * @param ah the AttributeHolder\r
+ * @return the repository name.\r
+ */\r
+ public String getRepositoryFromHolder(AttributeHolder ah){\r
+ return (String)searchValue(ah,"repository") ;\r
+ }\r
+\r
+ /** \r
+ * Get frames of an attribute holder.\r
+ * @param ah the AttributeHolder\r
+ * @return an array with frames.\r
+ */\r
+ public AttributeHolder[] getFramesFromHolder(AttributeHolder ah){\r
+ return (AttributeHolder[])searchValue(ah,"frames") ;\r
+ } \r
+\r
+\r
+ /** \r
+ * Perform an action on frames of an attribute holder.\r
+ * @param action the action to perform\r
+ * @param ah the AttributeHolder\r
+ * @param int the resource depth wrt the entire action target.\r
+ */\r
+ public void performActionOnFrames(String action,AttributeHolder ah,int deep)\r
+ {\r
+ AttributeHolder[] frames = this.getFramesFromHolder(ah);\r
+ for (int j = 0 ; j < frames.length ; j++) {\r
+ this.performAction(action,frames[j],deep+1);\r
+ }\r
+ }\r
+\r
+ /** \r
+ * Perform an action on an attribute holder.\r
+ * @param action the action to perform\r
+ * @param ah the AttributeHolder\r
+ * @param int the resource depth wrt the entire action target.\r
+ */\r
+ public void performAction(String action, AttributeHolder ah,int deep){\r
+\r
+ /* LIST ACTION */\r
+ if (action.compareTo("list") == 0 ){ \r
+ String depthtab = ""; \r
+ for ( int j = 0 ; j < deep ; j++ ){ \r
+ depthtab = depthtab+"\t" ; \r
+ }\r
+ System.out.println(depthtab+"|-"+this.getIdentFromHolder(ah));\r
+ /* LIST -a ACTION */\r
+ } else if (action.compareTo("listatt") == 0){\r
+ this.printAttributeHolder(ah); \r
+ /* change class action -- not implemented */\r
+ // } else if (action.compareTo("chclass") == 0){\r
+\r
+ /* REPLACE ACTION */\r
+ } else if (pmatcher.matches(action,srPattern)){ \r
+ System.out.println("hop: "+ pmatcher.getMatch().group(1));\r
+ /* browse attributes, change only those that match \r
+ pmatcher.getMatch().group(1) by applying \r
+ putil.substitute(action, attributestring); */\r
+ Attribute[] att = ah.getAttributes();\r
+ try { \r
+ /* cut the s/attribute=value/newvalue/ regexp */\r
+ Pattern pt = pcompiler.compile(pmatcher.getMatch().group(1),\r
+ Perl5Compiler.DEFAULT_MASK); \r
+ String newvalue = pmatcher.getMatch().group(2);\r
+ \r
+ for (int j = 0 ; j < att.length ; j++) {\r
+ Object value = ah.getValue (j,null);\r
+ String atts = attributeToString(att[j], value);\r
+ if (pmatcher.matches(atts,pt)){\r
+ String repl = putil.substitute(action, atts); \r
+ System.out.println("replaced: "+atts+" by: "+repl);\r
+ // System.out.println("new value "+ newvalue);\r
+ ah.setValue(j,newvalue);\r
+ }\r
+ } \r
+ }\r
+ catch (org.apache.oro.text.regex.MalformedPatternException ex){ \r
+ ex.printStackTrace(); \r
+ }\r
+ }\r
+ }\r
+\r
+\r
+ /** \r
+ * The resource lister initializer.\r
+ */\r
+ public ResourceLister(){\r
+ try { \r
+ srPattern = pcompiler.compile(\r
+ // "^s/([\\w|=|-|\\\\/]+?)/[\\w|=|-|\\\\/]+$", \r
+ "^s/(.*[^\\\\])/(.*[^\\\\])/$",\r
+ Perl5Compiler.DEFAULT_MASK); \r
+ } \r
+ catch (org.apache.oro.text.regex.MalformedPatternException ex){ \r
+ ex.printStackTrace(); \r
+ }\r
+ }\r
+\r
+}\r
+\r
+\r
+\r