001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package web.layout;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import javax.servlet.ServletContext;
025:
026: import org.apache.commons.digester.Digester;
027: import org.apache.commons.digester.ExtendedBaseRules;
028: import org.xml.sax.SAXException;
029:
030: /**
031: * 该类用于读取layout.xml中的配置
032: * @author Winter Lau
033: */
034: public class LayoutConfig {
035:
036: private List pages;
037: private boolean freezed = false;
038: private static LayoutConfig config;
039: private static File configFile;
040: private static long lastModified;
041:
042: private LayoutConfig() {
043: pages = new ArrayList();
044: }
045:
046: /**
047: * 加载配置layout.xml
048: * @return
049: * @throws SAXException
050: * @throws IOException
051: */
052: public static LayoutConfig getConfig(ServletContext context)
053: throws IOException, SAXException {
054: if (config != null && configFile.lastModified() == lastModified) {
055: return config;
056: }
057: config = new LayoutConfig();
058: Digester dig = new Digester();
059: dig.push(config);
060: dig.setValidating(false);
061: dig.setRules(new ExtendedBaseRules());
062: dig.addSetProperties("layout");
063:
064: dig.addObjectCreate("layout/pages/page", Page.class);
065: dig.addSetProperties("layout/pages/page");
066: dig.addBeanPropertySetter("layout/pages/page" + "/?");
067: dig.addSetNext("layout/pages/page", "addPage");
068:
069: InputStream in = null;
070: if (context != null) {
071: in = context.getResourceAsStream("/WEB-INF/layout.xml");
072: configFile = new File(context
073: .getRealPath("/WEB-INF/layout.xml"));
074: }
075: if (in == null) {
076: in = LayoutConfig.class.getResourceAsStream("layout.xml");
077: configFile = new File(LayoutConfig.class.getResource(
078: "layout.xml").getPath());
079: }
080: try {
081: lastModified = configFile.lastModified();
082: dig.parse(in);
083: config.freezed = true;
084: } finally {
085: if (in != null)
086: in.close();
087: }
088: return config;
089: }
090:
091: public Page getPage(int index) {
092: return (Page) pages.get(index);
093: }
094:
095: public int pageSize() {
096: return pages.size();
097: }
098:
099: public void addPage(Page page) throws IllegalAccessException {
100: if (!freezed)
101: pages.add(page);
102: else
103: throw new IllegalAccessException("LayoutConfig is fozened.");
104: }
105:
106: public static void main(String[] args) throws Exception {
107: LayoutConfig config = LayoutConfig.getConfig(null);
108: int pc = config.pageSize();
109: for (int i = 0; i < pc; i++) {
110: Page p = config.getPage(i);
111: System.out.println("Name:" + p.getName() + ",uri:"
112: + p.getUri() + ",param:" + p.getParam());
113: }
114: config = LayoutConfig.getConfig(null);
115: pc = config.pageSize();
116: for (int i = 0; i < pc; i++) {
117: Page p = config.getPage(i);
118: System.out.println("Name:" + p.getName() + ",uri:"
119: + p.getUri() + ",param:" + p.getParam());
120: }
121: }
122:
123: }
|