1 23 24 package org.apache.slide.projector.processor.tree; 25 26 import java.io.InputStream ; 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.Stack ; 32 import java.util.logging.Level ; 33 import java.util.logging.Logger ; 34 35 import org.apache.slide.projector.ConfigurableProcessor; 36 import org.apache.slide.projector.ConfigurationException; 37 import org.apache.slide.projector.Context; 38 import org.apache.slide.projector.Result; 39 import org.apache.slide.projector.descriptor.ParameterDescriptor; 40 import org.apache.slide.projector.descriptor.ResultDescriptor; 41 import org.apache.slide.projector.descriptor.ResultEntryDescriptor; 42 import org.apache.slide.projector.descriptor.StateDescriptor; 43 import org.apache.slide.projector.i18n.DefaultMessage; 44 import org.apache.slide.projector.processor.SimpleProcessor; 45 import org.apache.slide.projector.value.ArrayValue; 46 import org.apache.slide.projector.value.StreamableValue; 47 import org.apache.slide.projector.value.StringValue; 48 import org.apache.slide.projector.value.Value; 49 import org.xml.sax.InputSource ; 50 import org.xml.sax.helpers.AttributesImpl ; 51 52 import de.zeigermann.xml.simpleImporter.DefaultSimpleImportHandler; 53 import de.zeigermann.xml.simpleImporter.SimpleImporter; 54 import de.zeigermann.xml.simpleImporter.SimplePath; 55 56 60 public class Sitemap implements ConfigurableProcessor { 61 private static Logger logger = Logger.getLogger(Sitemap.class.getName()); 62 63 private final static ParameterDescriptor[] parameterDescriptors = new ParameterDescriptor[0]; 64 private static ResultDescriptor resultDescriptors; 65 66 private static Result result = new Result(StateDescriptor.OK); 67 68 public void configure(StreamableValue config) throws ConfigurationException { 69 try { 70 InputStream configuration = config.getInputStream(); 71 SimpleImporter importer = new SimpleImporter(); 72 ConfigurationHandler handler = new ConfigurationHandler(); 73 importer.addSimpleImportHandler(handler); 74 importer.parse(new InputSource (configuration)); 75 Page rootPage = handler.getRootPage(); 76 List resources = new ArrayList (); 77 buildArray(resources, rootPage); 78 Value[] array = new Value[resources.size()]; 79 ArrayValue arrayResource = new ArrayValue((Value [])resources.toArray(array)); 80 result.addResultEntry(SimpleProcessor.OUTPUT, arrayResource); 81 resultDescriptors = new ResultDescriptor( 82 new StateDescriptor[] { StateDescriptor.OK_DESCRIPTOR }, 83 new ResultEntryDescriptor[] { 84 new ResultEntryDescriptor(SimpleProcessor.OUTPUT, new DefaultMessage("sitemap/output"), ArrayValue.CONTENT_TYPE, false) 85 }); 86 } catch (Exception exception) { 87 logger.log(Level.SEVERE, "Error while parsing sitemap configuration", exception); 88 } 89 } 90 91 private void buildArray(List resources, Page page) { 92 for ( Iterator j = page.getChildren().iterator(); j.hasNext(); ) { 93 Page child = (Page)j.next(); 94 if ( child.getId() != null ) { 95 resources.add(new StringValue(child.getId())); 96 } 97 if ( child.getChildren().size() > 0 ) { 98 List childrenResources = new ArrayList (); 99 buildArray(childrenResources, child); 100 Value[] array = new Value[childrenResources.size()]; 101 ArrayValue arrayResource = new ArrayValue((Value [])childrenResources.toArray(array)); 102 resources.add(arrayResource); 103 } 104 } 105 } 106 107 public Result process(Map parameter, Context context) throws Exception { 108 return result; 109 } 110 111 public ParameterDescriptor[] getParameterDescriptors() { 112 return parameterDescriptors; 113 } 114 115 public ResultDescriptor getResultDescriptor() { 116 return resultDescriptors; 117 } 118 119 class Page { 120 private List children = new ArrayList (); 121 private String id; 122 123 public Page(String id) { 124 this.id = id; 125 } 126 127 public String getId() { 128 return id; 129 } 130 131 public void addChild(Page page) { 132 children.add(page); 133 } 134 135 public List getChildren() { 136 return children; 137 } 138 } 139 140 class ConfigurationHandler extends DefaultSimpleImportHandler { 141 private Stack pageStack = new Stack (); 142 private Page rootPage; 143 144 public Page getRootPage() { 145 return rootPage; 146 } 147 148 public void startElement(SimplePath path, String name, AttributesImpl attributes, String leadingCDdata) { 149 if ( path.matches("page") ) { 150 String id = attributes.getValue(org.apache.slide.projector.processor.tree.TreeRenderer.ID); 151 Page page = new Page(id); 152 Page parentPage = (Page)pageStack.peek(); 153 parentPage.addChild(page); 154 pageStack.push(page); 155 } else if ( path.matches("sitemap" ) ) { 156 Page page = new Page(null); 157 pageStack.push(page); 158 rootPage = page; 159 } 160 } 161 162 public void endElement(SimplePath path, String name) { 163 if ( path.matches("page") ) { 164 pageStack.pop(); 165 } 166 } 167 } 168 } 169 | Popular Tags |