Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

PageElement.java

00001 package edu.virtualschool.jwaa.xml;
00002 
00003 import java.util.ArrayList;
00004 import java.util.Iterator;
00005 import java.util.List;
00006 
00007 import org.apache.log4j.Logger;
00008 import org.jdom.Attribute;
00009 import org.jdom.Element;
00010 import org.jdom.JDOMException;
00011 import org.jdom.xpath.XPath;
00012 import org.mortbay.html.Page;
00013 
00014 import edu.virtualschool.jwaa.Fault;
00015 import edu.virtualschool.jwaa.bean.DemoRole;
00016 
00028 public class PageElement extends edu.virtualschool.jwaa.MetaPage  
00029 {
00030   public final String id;
00031   public final PagesElement pages;
00032   public String text;
00033   public final Element element;
00034   public final List childPageList = new ArrayList();
00035   public FormElement form = null;
00036   boolean isCommited = false;
00037 
00038   final static Logger logger = Logger.getLogger(Page.class.getName());
00039   final static Outputter outputter = new Outputter(true);
00040   private static XPath formPath = null;
00041   private static XPath idrefPath = null;
00042   
00043   public PageElement(PagesElement pages, Element element) 
00044     throws ValidationFault, IOFault
00045   {
00046     super(
00047         PageElement.class,
00048         getURL(pages, element.getAttributeValue("id")),
00049         element.getAttributeValue("name"),
00050         element.getAttributeValue("title"),
00051         DemoRole.findRole(element.getAttributeValue("requiresRole"))
00052     );
00053     this.id = element.getAttributeValue("id");
00054     this.pages = pages;
00055     this.element = element;
00056 
00057     getApplication().dictionary.put(id, getLink().toString());
00058 
00059     List childList = element.getChildren("child", element.getNamespace());
00060     for (Iterator iterator = childList.iterator(); iterator.hasNext();)
00061     {
00062       Element child = (Element) iterator.next();
00063       String id = child.getAttributeValue("idref");
00064       if (id == null)
00065         throw new ValidationFault("child requires an idref attribute");
00066       childPageList.add(id);
00067       iterator.remove();
00068     }
00069   }
00070   final void commit() throws ValidationFault
00071   {
00072     if (isCommited)
00073       return;
00074     for (Iterator iterator = childPageList.iterator(); iterator.hasNext(); )
00075     {
00076       String id = (String)iterator.next();
00077       PageElement child = pages.getApplication().findPage(id);
00078       if (child == null)
00079         throw new ValidationFault("child "+ id + " of page "+this+" not found");
00080       addChildNode(child);
00081     }
00082     try
00083     {
00084       if (formPath == null)
00085         formPath = XPath.newInstance(".//form");
00086       List formList = formPath.selectNodes(element);
00087       if (formList.size() == 0)
00088         this.form = null;
00089       else if (formList.size() > 1)
00090         throw new ValidationFault("Multiple forms/page are not yet supported");
00091       else
00092       {
00093         Element formElement = (Element)formList.get(0);
00094         this.form = new FormElement(this, formElement);
00095       }
00096     }
00097     catch (IOFault e)
00098     {
00099       throw new ValidationFault(e.getMessage(), e);
00100     }
00101     catch (JDOMException e)
00102     {
00103       throw new ValidationFault(e.getMessage(), e);
00104     }
00105     try
00106     {
00107       if (idrefPath == null)
00108         idrefPath = XPath.newInstance(".//@idref");
00109       List idrefList = idrefPath.selectNodes(element);
00110       for (Iterator iterator = idrefList.iterator(); iterator.hasNext(); )
00111       {
00112         Attribute attr = (Attribute)iterator.next();
00113         String key = attr.getValue();
00114         String val = (String)getApplication().dictionary.get(key);
00115         if (val == null)
00116           throw new ValidationFault("idref=\""+key+" not found in page "+this);
00117         attr.setName("href");
00118         attr.setValue(val);
00119       }
00120       for (Iterator iterator = element.getAttributes().iterator(); iterator.hasNext(); )
00121       {
00122         iterator.next();
00123         iterator.remove();
00124       }
00125       element.setName("div");
00126       element.setAttribute("class", "page");
00127       String logicText = "";
00128       if (form != null)
00129         logicText = form.logicText+"\n";
00130       this.text = logicText + 
00131         "#header()\n"+
00132         outputter.output(element)+
00133         "#footer()\n";
00134 
00135 //      XPath hrefPath = XPath.newInstance(".//@href");
00136 //      List hrefList = hrefPath.selectNodes(element);
00137 //      for (Iterator iterator = hrefList.iterator(); iterator.hasNext(); )
00138 //      {
00139 //        Attribute attr = (Attribute)iterator.next();
00140 //        String value = attr.getValue();
00141 //        attr.setValue("$self.encodeURL('"+value+"')");
00142 //      }
00143     }
00144     catch (JDOMException e1)
00145     {
00146       throw new ValidationFault("JDOM fault", e1);
00147     }
00148     isCommited = true;
00149   }
00150   final static String getURL(PagesElement pages, String id)
00151     throws ValidationFault
00152   {
00153     if (id == null)
00154       throw new ValidationFault("page requires an id attribute");
00155     return pages.getApplication().getRoot().id+
00156         "/"+pages.getApplication().id + "/"+id;
00157   }
00158   public final String getId()
00159   {
00160     return id;
00161   }
00162   GenericForm getFormInstance(Controller page) throws Fault
00163   {
00164     if (form == null)
00165       return null;
00166     else
00167       return form.getFormInstance(page);
00168   }
00169   String getText()
00170   {
00171     return text;
00172   }
00173   public final String getFilePath()
00174   {
00175     return pages.fileMonitor.toString();
00176   }
00177   public PagesElement getPages()
00178   {
00179     return pages;
00180   }
00181   public final ApplicationElement getApplication()
00182   {
00183     ApplicationElement c = pages.getApplication();
00184     return c;
00185   }
00186   public final List getChildPageIDs()
00187   {
00188     return childPageList;
00189   }
00190   public String toString()
00191   {
00192     return id;
00193   }
00194 }