Back to index

im-sdk  12.3.91
ManageRule.java
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 
00043 
00044 package sun.awt.im.iiimp;
00045 
00046 import java.io.InputStream;
00047 import java.io.InputStreamReader;
00048 import java.util.StringTokenizer;
00049 import java.util.Vector;
00050 
00051 import com.sun.iiim.*;
00052 
00053 class ManageRule {
00054 
00055     private final static String PROTOCOL_DRIVER_NAME =
00056        "sun.awt.im.iiimp.ProtocolDriver";
00057 
00058     private final static String DEFAULT_PCE_RULE =
00059        "sun/awt/im/iiimp/sample.lwesyntax";
00060 
00061     private String ruleString = null;
00062 
00063     ManageRule(String rule) {
00064        ruleString = rule;
00065        parseRule(ruleString);
00066     }
00067 
00068     ManageRule(InputStreamReader reader) {
00069        StringBuffer sb = new StringBuffer();
00070        int ch;
00071        try {
00072            for (; (ch = reader.read()) != -1;) {
00073               sb.append((char)ch);
00074            }
00075        } catch(Exception e) {}
00076        ruleString = sb.toString();
00077        parseRule(ruleString);
00078     }
00079 
00080     public String toString() {
00081        return ruleString;
00082     }
00083 
00084     int size() {
00085        return imVector.size();
00086     }
00087 
00088     boolean contains(Object o) {
00089        Class c1 = o.getClass();
00090        for (int i = 0; i < imVector.size(); i++) {
00091            Class c2 = imVector.get(i).getClass();
00092            if (c1 == c2) {
00093               return true;
00094            }
00095        }
00096        return false;
00097     }
00098 
00099     IIIMListener get(int i) {
00100        return (IIIMListener)imVector.get(i);
00101     }
00102 
00103     private Vector imVector;
00104 
00105     private PCE pce;
00106     private String ccdef;
00107 
00108     void setCCDEF(String ccdef) {
00109        this.ccdef = ccdef;
00110        if (pce != null) {
00111            try {
00112               pce.pushSyntax(new LWESyntax(ccdef));
00113            } catch(InvalidSyntaxException e) {
00114               debug(" err = " + e);
00115            }
00116        }
00117     }
00118 
00119     private void parseRule(String ruleString) {
00120        imVector = new Vector();
00121 
00122        StringTokenizer st = new StringTokenizer(ruleString);
00123        imVector = new Vector();
00124 
00125        while(st.hasMoreTokens()) {
00126            String className = st.nextToken();
00127            Object obj;
00128 
00129            try {
00130               if (className.equals(PROTOCOL_DRIVER_NAME)) {
00131                   obj = ProtocolDriver.getInstance();
00132               } else {
00133                   obj = Class.forName(className).newInstance();
00134               }
00135            } catch(Exception e) {
00136               // ignore invalid entry in the rule
00137               continue;
00138            }
00139            if (obj == null) {
00140               continue;
00141            }
00142            if (obj instanceof IIIMListener) {
00143               if (obj instanceof PCE) {
00144                   // PCE setup
00145                   pce = (PCE)obj;
00146                   LWESyntax syntax = null;
00147                   if (ccdef == null) {
00148                      ClassLoader loader = ClassLoader.getSystemClassLoader();
00149                      InputStream is = loader.getResourceAsStream
00150                          (DEFAULT_PCE_RULE);
00151                      InputStreamReader reader = new InputStreamReader(is);
00152                      try {
00153                          syntax = new LWESyntax(reader);
00154                      } catch(InvalidSyntaxException e) {
00155                          debug(" err : " + e);
00156                          continue;
00157                      }
00158                   } else {
00159                      try {
00160                          syntax = new LWESyntax(ccdef);
00161                      } catch(InvalidSyntaxException e) {
00162                          debug(" err : " + e);
00163                          continue;
00164                      }
00165                   }
00166                   ((PCE)obj).pushSyntax(syntax);
00167               } else if (obj instanceof ProtocolDriver) {
00168                   Manager.setPD((ProtocolDriver)obj);
00169               }
00170               imVector.add(obj);
00171            }
00172        }
00173     }
00174 
00175     private void debug(String str) {
00176        if (Manager.DEBUG) {
00177            System.err.println(str);
00178        }
00179     }
00180 }