001: /*
002: * (C) Copyright 2006 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021: package com.nabhinc.condition;
022:
023: import java.io.ByteArrayOutputStream;
024: import java.io.StringReader;
025:
026: import javax.xml.bind.JAXBContext;
027: import javax.xml.bind.Marshaller;
028: import javax.xml.bind.Unmarshaller;
029:
030: /**
031: *
032: *
033: * @author Padmanabh Dabke
034: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
035: */
036: public class ConditionTest {
037: public static void main(String[] args) {
038:
039: String[] ips = new String[] { "127.0.0.1", "92.34.56.7" };
040: String[] users = new String[] { "wchokry", "pdabke" };
041: String[] roles = new String[] { "admin", "author" };
042:
043: AllowRoles aroles = new AllowRoles(roles);
044: DenyRoles droles = new DenyRoles(roles);
045: AllowUsers ausers = new AllowUsers(users);
046: DenyUsers dusers = new DenyUsers(users);
047: AllowIP aip = new AllowIP(ips);
048: DenyIP dip = new DenyIP(ips);
049: LoginRequired lreq = new LoginRequired();
050: And andcond = new And();
051: Not notcond = new Not();
052: Or orcond = new Or();
053:
054: ConditionWrapper cWrapper = new ConditionWrapper();
055: cWrapper.componentClass = "com.nabhinc.condition.AlwaysTrue";
056:
057: Condition[] andkids = new Condition[3];
058: andkids[0] = notcond;
059: andkids[1] = orcond;
060: andkids[2] = dusers;
061: andcond.children = andkids;
062:
063: Condition[] orkids = new Condition[6];
064: orkids[0] = aroles;
065: orkids[1] = droles;
066: orkids[2] = ausers;
067: orkids[3] = dip;
068: orkids[4] = lreq;
069: orkids[5] = cWrapper;
070: orcond.children = orkids;
071:
072: notcond.children = new Condition[] { aip };
073:
074: try {
075: /*
076: Class[] cls = XstreamUtil.generateClassArray("com.nabhinc.condition", "jaxb.index");
077:
078: XStream xstream = new XStream(new DomDriver());
079: xstream.setMode(XStream.NO_REFERENCES);
080: Annotations.configureAliases(xstream, cls);
081:
082: String xml = xstream.toXML(andcond);
083: System.out.println(xml);
084: Condition cond = (Condition) xstream.fromXML(xml);
085:
086: System.out.println(" ***************************");
087:
088: xml = xstream.toXML(cond);
089: System.out.println(xml);
090: */
091:
092: JAXBContext jc = JAXBContext
093: .newInstance("com.nabhinc.condition");
094:
095: Marshaller m = jc.createMarshaller();
096: m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
097: ByteArrayOutputStream sos = new ByteArrayOutputStream();
098: m.marshal(andcond, sos);
099: String xml = sos.toString();
100: System.out.println(xml);
101: Unmarshaller um = jc.createUnmarshaller();
102: Condition c = (Condition) um
103: .unmarshal(new StringReader(xml));
104: ByteArrayOutputStream sos2 = new ByteArrayOutputStream();
105: m.marshal(c, sos2);
106: System.out.println(sos2.toString());
107: } catch (Exception e) {
108: e.printStackTrace();
109: }
110:
111: }
112:
113: }
|