1 4 package com.openedit.modules.xml; 5 6 import java.util.Iterator ; 7 import java.util.List ; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.dom4j.Element; 12 13 import com.openedit.OpenEditException; 14 import com.openedit.WebPageRequest; 15 import com.openedit.modules.BaseModule; 16 import com.openedit.util.PathUtilities; 17 18 24 public class XmlModule extends BaseModule 25 { 26 private static final Log log = LogFactory.getLog(XmlModule.class); 27 protected XmlArchive fieldXmlArchive; 28 29 30 public XmlFile loadXmlFile( WebPageRequest inReq) throws Exception 31 { 32 String id = inReq.getRequestParameter("fileid"); 34 if( id == null) 35 { 36 id = inReq.getCurrentAction().getChildValue("fileid"); 37 } 38 XmlFile element = getXmlArchive().loadXmlFile(id); 39 40 if( element == null) 41 { 42 String path = inReq.getCurrentAction().getChildValue("path"); 43 if( element != null && path == null ) 44 { 45 path = element.getPath(); 46 } 47 String fullPath = PathUtilities.buildRelative(path, inReq.getPath()); 48 String name = inReq.getCurrentAction().getChildValue("element-name"); 49 List attribs = inReq.getCurrentAction().getConfig().getChildren("attribute"); 50 element = getXmlArchive().createXmlFile(id, fullPath, name, attribs); 51 } 52 inReq.putPageValue("xmlfile", element); 53 54 return element; 55 } 56 public void selectElement(WebPageRequest inReq) throws Exception 57 { 58 String eid = inReq.getRequestParameter("elementid"); 59 if( eid != null) 60 { 61 XmlFile file = loadXmlFile(inReq); 62 Element element = file.getElementById(eid); 63 inReq.putSessionValue("selectedelement", element); 64 } 65 } 66 public void deleteElement(WebPageRequest inReq) throws Exception 67 { 68 XmlFile file = loadXmlFile(inReq); 69 Element element = (Element)inReq.getSessionValue("selectedelement"); 70 file.deleteElement(element); 71 getXmlArchive().saveXml(file, inReq.getUser()); 72 inReq.putSessionValue("selectedelement", null); 73 } 74 public void saveElement(WebPageRequest inReq) throws Exception 75 { 76 XmlFile file = loadXmlFile(inReq); 77 Element element = (Element)inReq.getSessionValue("selectedelement"); 78 String id = element.attributeValue("id"); 79 element = file.getElementById(id); 80 if( element == null) 81 { 82 throw new OpenEditException("No such row " + id); 83 } 84 for (Iterator iter = file.getAttributes().iterator(); iter.hasNext();) 85 { 86 AttributeDesc attrib = (AttributeDesc) iter.next(); 87 String value = inReq.getRequestParameter(attrib.getId()); 88 element.addAttribute(attrib.getId(), value); 89 } 90 getXmlArchive().saveXml(file, inReq.getUser()); 91 inReq.putPageValue("message", "save complete"); 92 inReq.putSessionValue("selectedelement", element); 93 } 94 95 96 public void addElement(WebPageRequest inReq) throws Exception 97 { 98 XmlFile file = loadXmlFile(inReq); 99 Element element = file.addNewElement(); 100 inReq.putSessionValue("selectedelement", element); 101 } 102 103 public XmlArchive getXmlArchive() 104 { 105 return fieldXmlArchive; 106 } 107 108 public void setXmlArchive(XmlArchive inXmlArchive) 109 { 110 fieldXmlArchive = inXmlArchive; 111 } 112 } 113 | Popular Tags |