01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.lib.contract.lang;
28:
29: import java.io.*;
30: import java.util.*;
31:
32: import org.cougaar.lib.contract.Operator;
33: import org.cougaar.lib.contract.OperatorFactory;
34:
35: import org.cougaar.lib.contract.lang.cache.ClassCache;
36: import org.cougaar.lib.contract.lang.parser.*;
37:
38: /**
39: * Implementation of <code>org.cougaar.lib.contract.OperatorFactory</code> -- used to hide
40: * package dependency and some internals.
41: * <p>
42: * @see org.cougaar.lib.contract.OperatorFactory
43: */
44: public class OperatorFactoryImpl extends OperatorFactory {
45:
46: public OperatorFactoryImpl() {
47: // specify standard COUGAAR packages.
48: //
49: // This should probably be moved from a static configuration to a
50: // field of this factory!
51: String[] packages = new String[7];
52: packages[0] = "java.lang.";
53: packages[1] = "java.util.";
54: packages[2] = "org.cougaar.planning.ldm.plan.";
55: packages[3] = "org.cougaar.planning.ldm.asset.";
56: packages[4] = "org.cougaar.planning.ldm.measure.";
57: packages[5] = "org.cougaar.glm.";
58: packages[6] = "org.cougaar.glm.ldm.asset.";
59: ClassCache.setPackages(packages);
60: }
61:
62: public Operator create(final int style, final Object inObj)
63: throws Exception {
64: try {
65: // build tree tokenizer
66: BufferedVisitor bufVis = new BufferedVisitor();
67: if ((style & Op.PAREN_FLAG) != 0) {
68: ParenParser.parse(bufVis, inObj);
69: } else if ((style & Op.XML_FLAG) != 0) {
70: XMLParser.parse(bufVis, inObj);
71: } else {
72: throw new IllegalArgumentException(
73: "Unknown OpParser.parse style: " + style);
74: }
75: OpParserImpl opi = new OpParserImpl();
76: opi.parse(bufVis.getVisitTokenizer());
77: return opi.nextOp();
78: } catch (ParseException pe) {
79: // clear Java-stack and re-throw
80: throw (ParseException) pe.fillInStackTrace();
81: } catch (Exception e) {
82: // create trace
83: ParseException npe = new ParseException(e.getMessage());
84: npe.addTrace("OperatorFactory.create()");
85: throw npe;
86: }
87: }
88: }
|