[BACK]Return to OM2OXM.java CVS log [TXT][DIR] Up to [local] / OpenXM / src / OpenMath

Annotation of OpenXM/src/OpenMath/OM2OXM.java, Revision 1.5

1.1       tam         1: /**
1.5     ! tam         2:  * $OpenXM: OpenXM/src/OpenMath/OM2OXM.java,v 1.4 1999/11/12 08:07:56 tam Exp $
1.1       tam         3:  *
                      4:  * $B$3$N%/%i%9$G$O0J2<$N(B BNF $B$GI=$5$l$k9=J82r@O$r<BAu$7$F$$$k(B
                      5:  * expr -> stag [expr | immediate]* etag
                      6:  * immediate -> INTEGER
                      7:  *            | SYMBOL
                      8:  * stag -> '<' SYMBOL '>'
                      9:  * etag -> '<' '/' SYMBOL '>'
                     10:  * $B=*C<5-9f(B: INTEGER, SYMBOL, '<', '/', '>'
                     11:  * $BHs=*C<5-9f(B: expr, stag, etag, immediate
                     12:  * $B3+;O5-9f(B: expr
                     13:  *
                     14:  * $B0J2<$N4X?t$r;HMQ(B:
                     15:  * public void send(OutputStream os); - buffer $B$K$"$k(B OX message $B$rAw?.$9$k(B.
                     16:  * public int parse(); - $B9=J82r@O$r3+;O$9$k(B.
                     17:  * private int parse_expr();       - parser $B$N(B expr $BItJ,(B
                     18:  * private Attribute parse_stag(); - parser $B$N(B stag $BItJ,(B
                     19:  * private void parse_etag(Attribute tag,int argnum); - parser $B$N(B etag $BItJ,(B
                     20:  * private int parse_immediate();  - parser $B$N(B immediate $BItJ,(B
                     21:  * private void parse_error(String mesg); - parser $B$N(B error $B$rI=<($7$F=*N;$9$k(B
                     22:  * public static void main(String[] args); - main $B4X?t(B.
                     23:  *
                     24:  * $B0J2<$NJQ?t$r;HMQ(B:
                     25:  * Attribute Attribute; - $B;z6g2r@O4o$r8F$S=P$7(B, $B$=$NB0@-CM$rJ]B8$7$F$*$/(B
                     26:  * int token; - $B8=:_FI$_9~$s$G$$$k(B token $B$rJ]B8$7$F$*$/$N$K;HMQ(B.
                     27:  * CellStack stack; - $B%;%^%s%F%#%C%/%9$N%A%'%C%/$K;H$&FbIt%9%?%C%/(B.
                     28:  * ByteArrayOutputStream buffer; - oxasir $B$KAw?.$9$k$^$($K%G!<%?$rN/$a$F$*$/(B
                     29:  */
                     30:
                     31: import java.io.*;
                     32: import java.net.*;
                     33: import java.util.Vector;
                     34: import JP.ac.kobe_u.math.tam.OpenXM.*;
                     35:
                     36: final class OM2OXM implements Runnable{
                     37:   private PushbackInputStream is;
                     38:   private String attribute;
                     39:   private int token = TT_NULL;
                     40:   private boolean lexPushbackFlag = false;
                     41:   private OpenXM asir; // for debug
1.3       tam        42:   private boolean debug = true;
1.1       tam        43:
                     44:   // Token Type for lexical analyzer
                     45:   final static int TT_NULL      = 0;
                     46:   final static int TT_String    = -1;
                     47:   final static int TT_StartTag  = -2;
                     48:   final static int TT_EndTag    = -3;
                     49:   final static int TT_SingleTag = -4;
                     50:
                     51:   public void run(){ // for debug
                     52:     //$B%5!<%PB&$+$iAw?.$5$l$?J8;zNs$r<u?.$7$^$9!#(B
                     53:     try{
                     54:       while(true){
                     55:        CMO tmp;
                     56:
                     57:        Thread.yield();
                     58:
                     59:        switch(asir.receiveOXtag()){
                     60:        case OpenXM.OX_COMMAND:
                     61:           asir.receiveSM();
                     62:           break;
                     63:
                     64:        case OpenXM.OX_DATA:
                     65:           tmp = asir.receiveCMO();
1.2       tam        66:          System.out.println("=> "+ CMO2OM(tmp));
1.1       tam        67:           break;
                     68:        }
                     69:
                     70:       }
                     71:     }catch(IOException e){}
                     72:   }
                     73:
                     74:   public static String CMO2OM(CMO cmo){
1.2       tam        75:     return "<OMOBJ>"+ CMO2OM_sub(cmo) +"</OMOBJ>";
                     76:   }
                     77:
                     78:   private static String CMO2OM_sub(CMO cmo){
1.1       tam        79:     String ret = "";
                     80:
                     81:     switch(cmo.getDISCRIMINATOR()){
                     82:     case CMO.CMO_NULL:
                     83:       return "<OMI>0</OMI>";
                     84:
                     85:     case CMO.CMO_INT32:
                     86:       return "<OMI>"+ ((CMO_INT32)cmo).intValue() +"</OMI>";
                     87:
                     88:       // case CMO.CMO_DATUM:
                     89:
                     90:     case CMO.CMO_STRING:
                     91:       return "<OMSTR>"+ ((CMO_STRING)cmo).getString() +"</OMSTR>";
                     92:
                     93:       // case CMO.CMO_LIST:
                     94:
                     95:     case CMO.CMO_MONOMIAL32:
                     96:       ret += "<OMA><OMS name=\"Monom\" cd=\"poly\"/>";
1.2       tam        97:       ret += CMO2OM_sub(((CMO_MONOMIAL32)cmo).getCoefficient());
1.1       tam        98:       for(int i=0;i<((CMO_MONOMIAL32)cmo).getDegree().length;i++){
                     99:        ret += "<OMI>"+ ((CMO_MONOMIAL32)cmo).getDegree()[i] +"</OMI>";
                    100:       }
                    101:       ret += "</OMA>";
                    102:       return ret;
                    103:
                    104:     case CMO.CMO_ZZ:
                    105:       return "<OMI>"+ ((CMO_ZZ)cmo).BigIntValue() +"</OMI>";
                    106:
                    107:     case CMO.CMO_QQ:
                    108:       return "<OMA><OMS name=\"over\" cd=\"basic\"/>"+
1.2       tam       109:        CMO2OM_sub(((CMO_QQ)cmo).getBunshi())+
                    110:        CMO2OM_sub(((CMO_QQ)cmo).getBunbo())+
                    111:        "</OMA>";
1.1       tam       112:
                    113:     case CMO.CMO_ZERO:
                    114:       return "<OMI> 0 </OMI>";
                    115:
                    116:       // case CMO.CMO_DMS:
                    117:
                    118:     case CMO.CMO_DMS_GENERIC:
                    119:       ret += "<OMA><OMS name=\"PolyRing\" cd=\"poly\"/>";
1.4       tam       120:       ret += "<OMI>2</OMI></OMA>";
1.1       tam       121:       return ret;
                    122:
                    123:     case CMO.CMO_DISTRIBUTED_POLYNOMIAL:
                    124:       ret += "<OMA><OMS name=\"DMP\" cd=\"poly\"/>";
1.2       tam       125:       ret += CMO2OM_sub(((CMO_DISTRIBUTED_POLYNOMIAL)cmo).getRing());
1.1       tam       126:       ret += "<OMA><OMS name=\"SDMP\" cd=\"poly\"/>";
1.2       tam       127:       for(int i=0;i<((CMO_DISTRIBUTED_POLYNOMIAL)cmo).getMonomials().length;
                    128:          i++){
                    129:        ret += CMO2OM_sub(((CMO_DISTRIBUTED_POLYNOMIAL)cmo).getMonomials()[i]);
1.1       tam       130:       }
                    131:       ret += "</OMA></OMA>";
                    132:       return ret;
                    133:
                    134:     case CMO.CMO_INDETERMINATE:
                    135:       return "<OMV name=\""+ ((CMO_INDETERMINATE)cmo).getString() +"\"/>";
                    136:
                    137:       /*
                    138:        case CMO.CMO_TREE:
                    139:       return "<OMA><OMS name=\""+ "\"/>"+
                    140:        "</OMA>";
                    141:        */
                    142:
                    143:     default:
                    144:       return "<OMSTR>"+ cmo.toCMOexpression() +"</OMSTR>";
                    145:     }
                    146:   }
                    147:
                    148:   private boolean isSpace(int ch){ // use from lex
                    149:     return (ch==' ' || ch=='\t' || ch=='\n' || ch=='\r');
                    150:   }
                    151:
                    152:   private int skipSpace() throws IOException{ // use from lex
                    153:     int ch;
                    154:
                    155:     do{
                    156:       ch = is.read();
                    157:     }while(isSpace(ch));
                    158:     is.unread(ch);
                    159:
                    160:     return ch;
                    161:   }
                    162:
                    163:   private void pushbackLex(){
                    164:     if(lexPushbackFlag){
                    165:       System.err.println("lex pushback error!");
                    166:     }
                    167:     lexPushbackFlag = true;
                    168:   }
                    169:
                    170:   private int readNextToken() throws IOException{ // lexical analyzer
                    171:     String buf = "";
                    172:     int ch;
                    173:
                    174:     if(lexPushbackFlag){
                    175:       lexPushbackFlag = false;
                    176:       return token;
                    177:     }
                    178:
                    179:     token = TT_NULL;
                    180:     attribute = "";
                    181:     skipSpace();
                    182:     ch = is.read();
                    183:
                    184:     if(ch == '<'){ // for tag
                    185:       ch = skipSpace();
                    186:       if(ch == '/'){
                    187:        is.read();
                    188:        token = TT_EndTag;
                    189:        skipSpace();
                    190:       }else{
                    191:        token = TT_StartTag;
                    192:       }
                    193:       while((ch = is.read()) != '>' && ch != '/' && ch != -1){
                    194:        if(isSpace(ch)){
                    195:          if((ch = skipSpace()) != '>' && ch != '/'){
                    196:            buf += " ";
                    197:          }
                    198:          continue;
                    199:        }
                    200:
                    201:        buf += Character.toUpperCase((char)ch);
                    202:
                    203:        if(ch == '"'){
                    204:          do{
                    205:            ch = is.read();
                    206:            buf += (char)ch;
                    207:          }while(ch != '"' && ch != -1);
                    208:        }
                    209:       }
                    210:       if(ch == '>'){ // for StartTag and EndTag
                    211:        attribute = buf;
                    212:        return token;
                    213:       }else if(ch == '/'){
                    214:        skipSpace();
                    215:        if((ch = is.read()) == '>'){ // for SingleTag
                    216:          attribute = buf;
                    217:          return token = TT_SingleTag;
                    218:        }
                    219:       }
                    220:       return token = TT_NULL;
                    221:     }else if(ch != -1){
                    222:       is.unread(ch);
                    223:       while((ch = is.read()) != '<' && ch != -1){
1.3       tam       224:        debug("debug: "+ch);
1.1       tam       225:        if(isSpace(ch)){
1.5     ! tam       226:          String spaces = String.valueOf((char)ch);
1.1       tam       227:
                    228:          while(isSpace(ch = is.read())){
                    229:            spaces += (char)ch;
                    230:          }
                    231:          if(ch != '<' && ch != -1){
                    232:            buf += spaces;
                    233:          }
                    234:          is.unread(ch);
                    235:        }else{
                    236:          buf += (char)ch;
                    237:        }
                    238:       }
                    239:       is.unread(ch);
                    240:
                    241:       attribute = buf;
                    242:       return token = TT_String;
                    243:     }
                    244:
                    245:     return token = TT_NULL;
                    246:   }
                    247:
                    248:   private boolean exceptTokenTypeInParse(int type) throws IOException{
                    249:     // use from parse
                    250:     if(readNextToken() != type){
                    251:       parse_error("We expect type :'"+ type
                    252:                  +"', but we got type :'"+ token +"'("+ attribute +").");
                    253:     }
1.3       tam       254:     //debug(":"+token+":"+attribute+":"+type);
1.1       tam       255:     return true;
                    256:   }
                    257:
                    258:   public CMO parse(InputStream stream) throws IOException{
                    259:     // start -> '<OMOBJ>' object '</OMOBJ>'
                    260:     CMO ret;
                    261:
                    262:     is = new PushbackInputStream(stream);
                    263:
                    264:     exceptTokenTypeInParse(TT_StartTag);
                    265:     if(!attribute.equals("OMOBJ")){
                    266:       parse_error("We expect '<OMOBJ>'.");
                    267:     }
                    268:
                    269:     ret = parse_object();
1.3       tam       270:
1.1       tam       271:     exceptTokenTypeInParse(TT_EndTag);
                    272:     if(!attribute.equals("OMOBJ")){
                    273:       parse_error("We expect '</OMOBJ>'.");
                    274:     }
                    275:
                    276:     return ret;
                    277:   }
                    278:
                    279:   private CMO parse_object() throws IOException{
                    280:     // object -> variable
                    281:     //         | '<OMI>' S? integer S? '</OMI>'
1.3       tam       282:     //         | '<OMSTR>' S? utf7 S? '</OMSTR>'
1.1       tam       283:     //         | '<OMA>' S? symbol S? objects S? '</OMA>'
                    284:     CMO ret;
                    285:
                    286:     if(readNextToken() != TT_StartTag && token != TT_SingleTag){
                    287:       parse_error("We expect '<OMI> or '<OMA>' or '<OMV>'.");
                    288:     }
                    289:
                    290:     if(attribute.startsWith("OMV")){
                    291:       pushbackLex();
                    292:       ret = parse_variable();
                    293:     }else if(attribute.equals("OMI")){
                    294:       pushbackLex();
                    295:       ret = parse_OMI();
1.3       tam       296:     }else if(attribute.equals("OMSTR")){
                    297:       pushbackLex();
                    298:       ret = parse_OMSTR();
1.1       tam       299:     }else if(attribute.equals("OMA")){
                    300:       String name,cdname;
                    301:       int argnum = 0;
                    302:
                    303:       name = parse_symbol();
                    304:
                    305:       if(name.equals("DMP")){
                    306:        ret = parse_symb_DMP();
                    307:       }else{
                    308:        ret = new CMO_TREE(name,"Basic",parse_objects());
                    309:       }
                    310:
                    311:       exceptTokenTypeInParse(TT_EndTag);
                    312:       if(!attribute.equals("OMA")){
                    313:        parse_error("We expect '</OMA>'.");
                    314:       }
                    315:     }else{
                    316:       parse_error("???");
                    317:       ret = null;
                    318:     }
                    319:
                    320:     return ret;
                    321:   }
                    322:
                    323:   private CMO_ZZ parse_OMI() throws IOException{
                    324:     CMO_ZZ ret;
                    325:
                    326:     exceptTokenTypeInParse(TT_StartTag);
                    327:     if(!attribute.equals("OMI")){
                    328:       parse_error("We expect '<OMI>'.");
                    329:     }
                    330:
                    331:     ret = (CMO_ZZ)parse_integer();
                    332:
                    333:     exceptTokenTypeInParse(TT_EndTag);
                    334:     if(!attribute.equals("OMI")){
                    335:       parse_error("We expect '</OMI>'.");
                    336:     }
                    337:
                    338:     return ret;
                    339:   }
                    340:
1.3       tam       341:   private CMO_STRING parse_OMSTR() throws IOException{
                    342:     CMO_STRING ret;
                    343:
                    344:     exceptTokenTypeInParse(TT_StartTag);
                    345:     if(!attribute.equals("OMSTR")){
                    346:       parse_error("We expect '<OMSTR>'.");
                    347:     }
                    348:
                    349:     if(readNextToken() == TT_String){
                    350:       //ret = (CMO_STRING)parse_utf7();
                    351:       ret = new CMO_STRING(attribute);
                    352:     }else{
                    353:       ret = new CMO_STRING("");
                    354:       pushbackLex();
                    355:     }
                    356:
                    357:     exceptTokenTypeInParse(TT_EndTag);
                    358:     if(!attribute.equals("OMSTR")){
                    359:       parse_error("We expect '</OMSTR>'.");
                    360:     }
                    361:
                    362:     return ret;
                    363:   }
                    364:
1.1       tam       365:   private CMO parse_symb_DMP() throws IOException{
                    366:     parse_object();
                    367:     return new CMO_DISTRIBUTED_POLYNOMIAL(new CMO_DMS_GENERIC(),
                    368:                                          parse_symb_SDMP());
                    369:   }
                    370:
                    371:   private CMO_MONOMIAL32[] parse_symb_SDMP() throws IOException{
                    372:     Vector mono = new Vector();
                    373:     CMO_MONOMIAL32[] ret;
                    374:
                    375:     exceptTokenTypeInParse(TT_StartTag);
                    376:     if(!attribute.equals("OMA")){
                    377:       parse_error("We expect '<OMA>'.");
                    378:     }
                    379:
                    380:     if(!parse_symbol().equals("SDMP")){
                    381:       parse_error("We expect '<SDMP>'");
                    382:     }
                    383:
                    384:     while(readNextToken() != TT_EndTag){
                    385:       pushbackLex();
                    386:       mono.addElement(parse_symb_Monom());
                    387:     }
                    388:
                    389:     if(!attribute.equals("OMA")){
                    390:       parse_error("We expect '</OMA>'.");
                    391:     }
                    392:
                    393:     ret = new CMO_MONOMIAL32[mono.size()];
                    394:     mono.copyInto((Object[])ret);
                    395:
                    396:     return ret;
                    397:   }
                    398:
                    399:   private CMO_MONOMIAL32 parse_symb_Monom() throws IOException{
                    400:     Vector degree = new Vector();
                    401:     int[] array;
                    402:
                    403:     exceptTokenTypeInParse(TT_StartTag);
                    404:     if(!attribute.equals("OMA")){
                    405:       parse_error("We expect '<OMA>'.");
                    406:     }
                    407:
                    408:     if(!parse_symbol().equals("Monom")){
                    409:       parse_error("We expect '<Monom>'");
                    410:     }
                    411:
                    412:     while(readNextToken() != TT_EndTag){
                    413:       pushbackLex();
                    414:       degree.addElement(parse_OMI());
                    415:     }
                    416:
                    417:     if(!attribute.equals("OMA")){
                    418:       parse_error("We expect '<OMA>'.");
                    419:     }
                    420:
                    421:     array = new int[degree.size()-1];
                    422:     for(int i=0;i<array.length;i++){
                    423:       array[i] = ((CMO_ZZ)degree.elementAt(i+1)).intValue();
                    424:     }
                    425:
                    426:     return new CMO_MONOMIAL32(array,(CMO_ZZ)degree.elementAt(0));
                    427:   }
                    428:
                    429:   private CMO parse_objects() throws IOException{
                    430:     // $B2r@O$5$l$?(B object $B$r(B LIST $B$GJV$9(B
                    431:     Vector objects = new Vector();
                    432:     CMO[] array;
                    433:
                    434:     while(readNextToken() != TT_EndTag){
                    435:       pushbackLex();
                    436:       objects.addElement(parse_object());
                    437:     }
                    438:     pushbackLex();
                    439:
                    440:     objects.trimToSize();
                    441:     array = new CMO[objects.size()];
                    442:     objects.copyInto((Object[])array);
                    443:
                    444:     System.out.println("debug :"+ new CMO_LIST(array));
                    445:     return new CMO_LIST(array);
                    446:   }
                    447:
                    448:   private String parse_symbol() throws IOException{
                    449:     // symbname $B$rJV$9(B
                    450:     String ret = "";
                    451:     StreamTokenizer para;
                    452:
                    453:     exceptTokenTypeInParse(TT_SingleTag);
                    454:
                    455:     para = new StreamTokenizer(new StringReader(attribute));
                    456:     para.resetSyntax();
                    457:     para.wordChars('A','Z');
                    458:     para.wordChars('a','z');
                    459:     para.wordChars('0','9');
                    460:     para.wordChars('\u00A0','\u00FF');
                    461:     para.whitespaceChars('\u0000','\u0020');
                    462:     para.quoteChar('"');
                    463:     para.eolIsSignificant(false);
                    464:     para.lowerCaseMode(true);
                    465:     para.slashStarComments(false);
                    466:     para.slashSlashComments(false);
                    467:     para.ordinaryChar('=');
                    468:
                    469:     if(para.nextToken() != StreamTokenizer.TT_WORD
                    470:        || !para.sval.equalsIgnoreCase("OMS")){
                    471:       parse_error("We expect '<OMS>', but '"+ para.sval +"'.");
                    472:     }
                    473:
                    474:     if(para.nextToken() != StreamTokenizer.TT_WORD){
                    475:       parse_error("We expect 'name' or 'cd'.");
                    476:     }
                    477:     if(para.sval.equals("name")){
                    478:       if(para.nextToken() != '='){
                    479:        parse_error("We expect '='.");
                    480:       }
                    481:       if(para.nextToken() != '"'){
                    482:        parse_error("We expect '\"'.");
                    483:       }
                    484:       ret = parse_symbname(para.sval);
                    485:
                    486:       if(para.nextToken() != StreamTokenizer.TT_WORD
                    487:         || !para.sval.equals("cd")){
                    488:        parse_error("We expect 'cd'.");
                    489:       }
                    490:       if(para.nextToken() != '='){
                    491:        parse_error("We expect '='.");
                    492:       }
                    493:       if(para.nextToken() != '"'){
                    494:        parse_error("We expect '\"'.");
                    495:       }
                    496:       parse_cdname(para.sval);
                    497:
                    498:     }else if(para.sval.equals("cd")){
                    499:       if(para.nextToken() != '='){
                    500:        parse_error("We expect '='.");
                    501:       }
                    502:       if(para.nextToken() != '"'){
                    503:        parse_error("We expect '\"'.");
                    504:       }
                    505:       parse_cdname(para.sval);
                    506:
                    507:       if(para.nextToken() != StreamTokenizer.TT_WORD
                    508:         || !para.sval.equals("name")){
                    509:        parse_error("We expect 'cd'.");
                    510:       }
                    511:       if(para.nextToken() != '='){
                    512:        parse_error("We expect '='.");
                    513:       }
                    514:       if(para.nextToken() != '"'){
                    515:        parse_error("We expect '\"'.");
                    516:       }
                    517:       ret = parse_symbname(para.sval);
                    518:
                    519:     }else{
                    520:       parse_error("We expect 'name' or 'cd'.");
                    521:     }
                    522:
                    523:     if(para.nextToken() != StreamTokenizer.TT_EOF){
                    524:       parse_error("We expect '/>'.");
                    525:     }
                    526:
                    527:     return ret;
                    528:   }
                    529:
                    530:   private CMO parse_integer() throws IOException{
                    531:     //Attribute integer = new Attribute(value);
                    532:     String str;
                    533:     int i=0;
                    534:
                    535:     exceptTokenTypeInParse(TT_String);
                    536:     str = attribute;
                    537:
                    538:     if(str.charAt(i)=='-'){
                    539:       i++;
                    540:     }
                    541:     if(str.length()>=i+2 && str.charAt(i)=='x'){ // for HEX
                    542:       String strhex = str.substring(0,i) + str.substring(i+1);
                    543:       for(i++;i<str.length();i++){
                    544:        if("0123456789ABCDEF".indexOf((int)str.charAt(i)) == -1){
                    545:          parse_error("We expect integer.");
                    546:        }
                    547:       }
                    548:       // 10 $B?J?t$KJQ49(B
                    549:       str = (new java.math.BigInteger(strhex,16)).toString();
                    550:     }else if(str.length()>=i+1){ // for DEC
                    551:       for(;i<str.length();i++){
                    552:        if("0123456789".indexOf((int)str.charAt(i)) == -1){
                    553:          parse_error("We expect integer.");
                    554:        }
                    555:       }
                    556:     }else{
                    557:       parse_error("We expect integer.");
                    558:     }
                    559:
                    560:     return new CMO_ZZ(str);
                    561:   }
                    562:
                    563:   private CMO parse_variable() throws IOException{
                    564:     StreamTokenizer para;
                    565:     CMO ret;
                    566:
                    567:     exceptTokenTypeInParse(TT_SingleTag);
                    568:
                    569:     para = new StreamTokenizer(new StringReader(attribute));
                    570:     para.resetSyntax();
                    571:     para.wordChars('A','Z');
                    572:     para.wordChars('a','z');
                    573:     para.wordChars('0','9');
                    574:     para.wordChars('\u00A0','\u00FF');
                    575:     para.whitespaceChars('\u0000','\u0020');
                    576:     para.quoteChar('"');
                    577:     para.eolIsSignificant(false);
                    578:     para.lowerCaseMode(true);
                    579:     para.slashStarComments(false);
                    580:     para.slashSlashComments(false);
                    581:     para.ordinaryChar('=');
                    582:
                    583:     if(para.nextToken() != StreamTokenizer.TT_WORD
                    584:        || !para.sval.equalsIgnoreCase("OMV")){
                    585:       parse_error("We expect '<OMV>'.");
                    586:     }
                    587:
                    588:     if(para.nextToken() != StreamTokenizer.TT_WORD
                    589:        || !para.sval.equals("name")){
                    590:       parse_error("We expect 'name', but obtain '"+ para.sval +"'.");
                    591:     }
                    592:     if(para.nextToken() != '='){
                    593:       parse_error("We expect '='.");
                    594:     }
                    595:     if(para.nextToken() != '"'){
                    596:       parse_error("We expect '\"'.");
                    597:     }
                    598:     ret = new CMO_INDETERMINATE(parse_varname(para.sval));
                    599:
                    600:     if(para.nextToken() != StreamTokenizer.TT_EOF){
                    601:       parse_error("We expect '/>'.");
                    602:     }
                    603:
                    604:     return ret;
                    605:   }
                    606:
                    607:   private String parse_varname(String str){
                    608:     String words = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
                    609:       + "0123456789+='(),-./:?!#$%*;@[]^_`{|}";
                    610:
                    611:     for(int i=0;i<str.length();i++){
                    612:       if(words.indexOf((int)str.charAt(i))==-1){
                    613:        parse_error("string \""+ str +"\" is not varname.");
                    614:       }
                    615:     }
                    616:
                    617:     return str;
                    618:   }
                    619:
                    620:   private String parse_cdname(String str){
                    621:     String alphabet = "abcdefghijklmnopqrstuvwxyz";
                    622:     int i=0;
                    623:
                    624:     if(alphabet.indexOf((int)str.charAt(i))==-1){
                    625:       parse_error("cdname \""+ str +"\" must begin small alphabet.");
                    626:     }
                    627:     for(i++;i<str.length();i++){
                    628:       if((alphabet + "0123456789_").indexOf((int)str.charAt(i))==-1){
                    629:        parse_error("string \""+ str +"\" is not cdname.");
                    630:       }
                    631:     }
                    632:
                    633:     return str;
                    634:   }
                    635:
                    636:   private String parse_symbname(String str) throws IOException{
                    637:     String Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
                    638:     int i=0;
                    639:
                    640:     if(Alphabet.indexOf((int)str.charAt(i))==-1){
                    641:       parse_error("symbname \""+ str +"\" must begin alphabet.");
                    642:     }
                    643:     for(i++;i<str.length();i++){
                    644:       if((Alphabet + "0123456789_").indexOf((int)str.charAt(i))==-1){
                    645:        parse_error("string \""+ str +"\" is not symbname.");
                    646:       }
                    647:     }
                    648:
                    649:     return str;
                    650:   }
                    651:
                    652:   private void parse_error(String mesg){
                    653:     System.err.println(mesg);
                    654:     System.err.print("error occuered near :");
                    655:     try{
                    656:       for(int i=0;i<10;i++){
                    657:        System.err.print((char)is.read());
                    658:       }
                    659:       System.err.println((char)is.read());
                    660:     }catch(IOException e){}
                    661:     System.exit(1);
1.3       tam       662:   }
                    663:
                    664:   private void debug(String str){
                    665:     if(debug){
                    666:       System.err.println(str);
                    667:     }
1.1       tam       668:   }
                    669:
                    670:   public static void main(String[] argv) throws IOException{
                    671:     OM2OXM P = new OM2OXM();
                    672:     OpenXM asir;
                    673:     String host = "localhost";
                    674:     int CtrlPort = 1200,StreamPort = 1300;
                    675:
                    676:     for(int i=0;i<argv.length;i++){
                    677:       if(argv[i].equals("-h")){
                    678:        System.out.println("");
                    679:        System.exit(0);
                    680:       }else if(argv[i].equals("-host")){
                    681:        host = argv[++i];
                    682:       }else if(argv[i].equals("-data")){
                    683:        StreamPort = Integer.valueOf(argv[++i]).intValue();
                    684:       }else if(argv[i].equals("-control")){
                    685:        CtrlPort = Integer.valueOf(argv[++i]).intValue();
                    686:       }else{
                    687:        System.err.println("unknown option :"+ argv[i]);
                    688:        System.exit(1);
                    689:       }
                    690:     }
                    691:
                    692:     try{
                    693:       asir = new OpenXM(host,CtrlPort,StreamPort);
                    694:       asir.sendSM(new SM(SM.SM_mathcap));
                    695:     }catch(UnknownHostException e){
                    696:       System.err.println("host unknown.");
                    697:       System.err.println(e.getMessage());
                    698:       return;
                    699:     }catch(IOException e){
                    700:       System.err.println("connection failed.");
                    701:       System.err.println("IOException occuer !!");
                    702:       System.err.println(e.getMessage());
                    703:       return;
                    704:     }
                    705:
                    706:     P.asir = asir;
                    707:     new Thread(P).start();
                    708:     System.out.println("start");
                    709:
                    710:     try{
                    711:       //P.value = new Attribute(System.in);
                    712:
                    713:       //$B%5!<%PB&$+$iAw?.$5$l$?J8;zNs$r<u?.$7$^$9!#(B
                    714:       while(true){
                    715:        CMO obj = P.parse(System.in);
                    716:        asir.send(obj);
                    717:        asir.sendSM(new SM(SM.SM_popCMO));
                    718:       }
                    719:
                    720:       //System.out.println("breaking...");
                    721:
                    722:     }catch(IOException e){
                    723:       e.printStackTrace();
                    724:     }
                    725:   }
                    726: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>