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

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

1.1       tam         1: /**
1.19    ! tam         2:  * $OpenXM: OpenXM/src/OpenMath/OM2OXM.java,v 1.18 2000/02/28 14:10:28 takayama 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.14      tam        42:   protected boolean debug = false; // for debug message
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:        switch(asir.receiveOXtag()){
                     58:        case OpenXM.OX_COMMAND:
                     59:           asir.receiveSM();
                     60:           break;
                     61:
                     62:        case OpenXM.OX_DATA:
                     63:           tmp = asir.receiveCMO();
1.2       tam        64:          System.out.println("=> "+ CMO2OM(tmp));
1.1       tam        65:           break;
                     66:        }
                     67:
                     68:       }
                     69:     }catch(IOException e){}
                     70:   }
                     71:
                     72:   public static String CMO2OM(CMO cmo){
1.2       tam        73:     return "<OMOBJ>"+ CMO2OM_sub(cmo) +"</OMOBJ>";
                     74:   }
                     75:
1.9       tam        76:   private static String CMO2OM_sub(CMO cmo) throws NumberFormatException{
1.1       tam        77:     String ret = "";
                     78:
                     79:     switch(cmo.getDISCRIMINATOR()){
1.19    ! tam        80:     case CMO.NULL:
1.1       tam        81:       return "<OMI>0</OMI>";
                     82:
1.19    ! tam        83:     case CMO.INT32:
1.1       tam        84:       return "<OMI>"+ ((CMO_INT32)cmo).intValue() +"</OMI>";
                     85:
                     86:       // case CMO.CMO_DATUM:
                     87:
                     88:     case CMO.CMO_STRING:
                     89:       return "<OMSTR>"+ ((CMO_STRING)cmo).getString() +"</OMSTR>";
                     90:
1.15      tam        91:     case CMO.CMO_LIST:
                     92:       ret += "<OMA><OMS name=\"list\" cd=\"basic\"/>";
                     93:       {
                     94:        CMO ob[] = ((CMO_LIST)cmo).getElements();
                     95:
                     96:        for(int i=0;i<ob.length;i++){
                     97:          ret += CMO2OM_sub(ob[i]);
                     98:        }
                     99:       }
                    100:       ret += "</OMA>";
                    101:       return ret;
1.1       tam       102:
                    103:     case CMO.CMO_MONOMIAL32:
                    104:       ret += "<OMA><OMS name=\"Monom\" cd=\"poly\"/>";
1.2       tam       105:       ret += CMO2OM_sub(((CMO_MONOMIAL32)cmo).getCoefficient());
1.1       tam       106:       for(int i=0;i<((CMO_MONOMIAL32)cmo).getDegree().length;i++){
                    107:        ret += "<OMI>"+ ((CMO_MONOMIAL32)cmo).getDegree()[i] +"</OMI>";
                    108:       }
                    109:       ret += "</OMA>";
                    110:       return ret;
                    111:
                    112:     case CMO.CMO_ZZ:
                    113:       return "<OMI>"+ ((CMO_ZZ)cmo).BigIntValue() +"</OMI>";
                    114:
                    115:     case CMO.CMO_QQ:
                    116:       return "<OMA><OMS name=\"over\" cd=\"basic\"/>"+
1.2       tam       117:        CMO2OM_sub(((CMO_QQ)cmo).getBunshi())+
                    118:        CMO2OM_sub(((CMO_QQ)cmo).getBunbo())+
                    119:        "</OMA>";
1.1       tam       120:
                    121:     case CMO.CMO_ZERO:
                    122:       return "<OMI> 0 </OMI>";
                    123:
                    124:       // case CMO.CMO_DMS:
                    125:
                    126:     case CMO.CMO_DMS_GENERIC:
                    127:       ret += "<OMA><OMS name=\"PolyRing\" cd=\"poly\"/>";
1.4       tam       128:       ret += "<OMI>2</OMI></OMA>";
1.1       tam       129:       return ret;
                    130:
1.12      tam       131:     case CMO.CMO_RECURSIVE_POLYNOMIAL:
                    132:       return CMO2OM_CoefficientOfRecursivePOLYNOMIAL(((CMO_RECURSIVE_POLYNOMIAL)cmo).getPolynomial(),((CMO_RECURSIVE_POLYNOMIAL)cmo).getVariables());
                    133:
1.1       tam       134:     case CMO.CMO_DISTRIBUTED_POLYNOMIAL:
                    135:       ret += "<OMA><OMS name=\"DMP\" cd=\"poly\"/>";
1.2       tam       136:       ret += CMO2OM_sub(((CMO_DISTRIBUTED_POLYNOMIAL)cmo).getRing());
1.1       tam       137:       ret += "<OMA><OMS name=\"SDMP\" cd=\"poly\"/>";
1.2       tam       138:       for(int i=0;i<((CMO_DISTRIBUTED_POLYNOMIAL)cmo).getMonomials().length;
                    139:          i++){
                    140:        ret += CMO2OM_sub(((CMO_DISTRIBUTED_POLYNOMIAL)cmo).getMonomials()[i]);
1.1       tam       141:       }
                    142:       ret += "</OMA></OMA>";
                    143:       return ret;
                    144:
1.19    ! tam       145:       //case CMO.POLYNOMIAL_IN_ONE_VARIABLE:
1.12      tam       146:
1.7       tam       147:     case CMO.CMO_BIGFLOAT:
                    148:       ret += "<OMA><OMS name=\"times\" cd=\"basic\"/>";
                    149:       ret += CMO2OM_sub(((CMO_BIGFLOAT)cmo).getSyosubu());
                    150:       ret += "<OMA><OMS name=\"power\" cd=\"basic\"/>";
                    151:       ret += "<OMI>2</OMI>";
                    152:       ret += CMO2OM_sub(((CMO_BIGFLOAT)cmo).getShisubu());
                    153:       ret += "</OMA></OMA>";
                    154:       return ret;
                    155:
1.1       tam       156:     case CMO.CMO_INDETERMINATE:
                    157:       return "<OMV name=\""+ ((CMO_INDETERMINATE)cmo).getString() +"\"/>";
                    158:
1.10      tam       159:     case CMO.CMO_TREE:
1.11      tam       160:       ret += "<OMA><OMS name=\""+ ((CMO_TREE)cmo).getName() +"\" cd=\""+
1.10      tam       161:        ((CMO_TREE)cmo).getCDName() +"\"/>";
1.15      tam       162:       for(int i=0;i<((CMO_TREE)cmo).getLeaves().getElements().length;i++){
                    163:        ret += CMO2OM_sub(((CMO_TREE)cmo).getLeaves().getElements()[i]);
1.10      tam       164:       }
                    165:       ret += "</OMA>";
                    166:       return ret;
1.1       tam       167:
                    168:     default:
1.9       tam       169:       //return "<OMSTR>"+ cmo.toCMOexpression() +"</OMSTR>";
1.1       tam       170:     }
1.9       tam       171:
1.10      tam       172:     throw new NumberFormatException("unknown convert way:"+
                    173:                                    cmo.toCMOexpression());
1.12      tam       174:   }
                    175:
                    176:   private static String CMO2OM_CoefficientOfRecursivePOLYNOMIAL(CMO cmo,CMO_LIST variables){
                    177:     CMO_POLYNOMIAL_IN_ONE_VARIABLE poly;
                    178:     String ret = "",variable;
                    179:
                    180:     if(!(cmo instanceof CMO_POLYNOMIAL_IN_ONE_VARIABLE)){
                    181:       return CMO2OM_sub(cmo);
                    182:     }
                    183:
                    184:     poly = (CMO_POLYNOMIAL_IN_ONE_VARIABLE)cmo;
1.15      tam       185:     variable = CMO2OM_sub(variables.getElements()[poly.getVariable()]);
1.12      tam       186:
                    187:    for(int i=0;i<poly.getDegrees().length;i++){
                    188:       String mono;
                    189:
                    190:       mono = "<OMA><OMS name=\"times\" cd=\"basic\"/>"+
                    191:        "<OMA><OMS name=\"power\" cd=\"basic\"/>"+
                    192:        variable +"<OMI>"+ poly.getDegrees()[i] +"</OMI></OMA>"+
                    193:        CMO2OM_CoefficientOfRecursivePOLYNOMIAL(poly.getCoefficients()[i],
                    194:                                                variables)
                    195:        +"</OMA>";
                    196:       if(i==0){
                    197:        ret = mono;
                    198:       }else{
                    199:        ret = "<OMA><OMS name=\"plus\" cd=\"basic\"/>"+ ret + mono +"</OMA>";
                    200:       }
                    201:     }
                    202:
                    203:    return ret;
1.1       tam       204:   }
                    205:
                    206:   private boolean isSpace(int ch){ // use from lex
                    207:     return (ch==' ' || ch=='\t' || ch=='\n' || ch=='\r');
                    208:   }
                    209:
                    210:   private int skipSpace() throws IOException{ // use from lex
                    211:     int ch;
                    212:
                    213:     do{
                    214:       ch = is.read();
                    215:     }while(isSpace(ch));
                    216:     is.unread(ch);
                    217:
                    218:     return ch;
                    219:   }
                    220:
                    221:   private void pushbackLex(){
                    222:     if(lexPushbackFlag){
                    223:       System.err.println("lex pushback error!");
                    224:     }
                    225:     lexPushbackFlag = true;
                    226:   }
                    227:
                    228:   private int readNextToken() throws IOException{ // lexical analyzer
                    229:     String buf = "";
                    230:     int ch;
                    231:
                    232:     if(lexPushbackFlag){
                    233:       lexPushbackFlag = false;
                    234:       return token;
                    235:     }
                    236:
                    237:     token = TT_NULL;
                    238:     attribute = "";
                    239:     skipSpace();
                    240:     ch = is.read();
                    241:
                    242:     if(ch == '<'){ // for tag
                    243:       ch = skipSpace();
                    244:       if(ch == '/'){
                    245:        is.read();
                    246:        token = TT_EndTag;
                    247:        skipSpace();
                    248:       }else{
                    249:        token = TT_StartTag;
                    250:       }
                    251:       while((ch = is.read()) != '>' && ch != '/' && ch != -1){
                    252:        if(isSpace(ch)){
                    253:          if((ch = skipSpace()) != '>' && ch != '/'){
                    254:            buf += " ";
                    255:          }
                    256:          continue;
                    257:        }
                    258:
                    259:        buf += Character.toUpperCase((char)ch);
                    260:
                    261:        if(ch == '"'){
                    262:          do{
                    263:            ch = is.read();
                    264:            buf += (char)ch;
                    265:          }while(ch != '"' && ch != -1);
                    266:        }
                    267:       }
                    268:       if(ch == '>'){ // for StartTag and EndTag
                    269:        attribute = buf;
                    270:        return token;
                    271:       }else if(ch == '/'){
                    272:        skipSpace();
                    273:        if((ch = is.read()) == '>'){ // for SingleTag
                    274:          attribute = buf;
                    275:          return token = TT_SingleTag;
                    276:        }
                    277:       }
                    278:       return token = TT_NULL;
                    279:     }else if(ch != -1){
                    280:       is.unread(ch);
                    281:       while((ch = is.read()) != '<' && ch != -1){
1.3       tam       282:        debug("debug: "+ch);
1.1       tam       283:        if(isSpace(ch)){
1.5       tam       284:          String spaces = String.valueOf((char)ch);
1.1       tam       285:
                    286:          while(isSpace(ch = is.read())){
                    287:            spaces += (char)ch;
                    288:          }
                    289:          if(ch != '<' && ch != -1){
                    290:            buf += spaces;
                    291:          }
                    292:          is.unread(ch);
                    293:        }else{
                    294:          buf += (char)ch;
                    295:        }
                    296:       }
                    297:       is.unread(ch);
                    298:
                    299:       attribute = buf;
                    300:       return token = TT_String;
                    301:     }
                    302:
                    303:     return token = TT_NULL;
                    304:   }
                    305:
                    306:   private boolean exceptTokenTypeInParse(int type) throws IOException{
                    307:     // use from parse
                    308:     if(readNextToken() != type){
                    309:       parse_error("We expect type :'"+ type
                    310:                  +"', but we got type :'"+ token +"'("+ attribute +").");
                    311:     }
1.3       tam       312:     //debug(":"+token+":"+attribute+":"+type);
1.1       tam       313:     return true;
                    314:   }
                    315:
                    316:   public CMO parse(InputStream stream) throws IOException{
                    317:     // start -> '<OMOBJ>' object '</OMOBJ>'
                    318:     CMO ret;
                    319:
                    320:     is = new PushbackInputStream(stream);
                    321:
                    322:     exceptTokenTypeInParse(TT_StartTag);
                    323:     if(!attribute.equals("OMOBJ")){
                    324:       parse_error("We expect '<OMOBJ>'.");
                    325:     }
                    326:
                    327:     ret = parse_object();
1.3       tam       328:
1.1       tam       329:     exceptTokenTypeInParse(TT_EndTag);
                    330:     if(!attribute.equals("OMOBJ")){
                    331:       parse_error("We expect '</OMOBJ>'.");
                    332:     }
                    333:
                    334:     return ret;
                    335:   }
                    336:
                    337:   private CMO parse_object() throws IOException{
                    338:     // object -> variable
                    339:     //         | '<OMI>' S? integer S? '</OMI>'
1.3       tam       340:     //         | '<OMSTR>' S? utf7 S? '</OMSTR>'
1.1       tam       341:     //         | '<OMA>' S? symbol S? objects S? '</OMA>'
                    342:     CMO ret;
                    343:
                    344:     if(readNextToken() != TT_StartTag && token != TT_SingleTag){
                    345:       parse_error("We expect '<OMI> or '<OMA>' or '<OMV>'.");
                    346:     }
                    347:
                    348:     if(attribute.startsWith("OMV")){
                    349:       pushbackLex();
                    350:       ret = parse_variable();
                    351:     }else if(attribute.equals("OMI")){
                    352:       pushbackLex();
                    353:       ret = parse_OMI();
1.3       tam       354:     }else if(attribute.equals("OMSTR")){
                    355:       pushbackLex();
                    356:       ret = parse_OMSTR();
1.1       tam       357:     }else if(attribute.equals("OMA")){
                    358:       String name,cdname;
                    359:       int argnum = 0;
                    360:
                    361:       name = parse_symbol();
                    362:
                    363:       if(name.equals("DMP")){
                    364:        ret = parse_symb_DMP();
1.6       tam       365:        debug("poly: "+ret);
1.1       tam       366:       }else{
1.17      tam       367:        CMO[] leaves = parse_objects();
                    368:
                    369:        if(name.equals("list")){
                    370:          ret = new CMO_LIST(leaves);
                    371:        }else if(name.equals("over") && leaves.length==2
                    372:                 && leaves[0] instanceof CMO_ZZ
                    373:                 && leaves[1] instanceof CMO_ZZ){
                    374:          ret = new CMO_QQ((CMO_ZZ)leaves[0],(CMO_ZZ)leaves[1]);
                    375:        }else{
1.18      takayama  376:          ret = new CMO_TREE(name,"basic",new CMO_LIST(leaves));
1.17      tam       377:        }
1.1       tam       378:       }
                    379:
                    380:       exceptTokenTypeInParse(TT_EndTag);
                    381:       if(!attribute.equals("OMA")){
                    382:        parse_error("We expect '</OMA>'.");
                    383:       }
                    384:     }else{
1.17      tam       385:       parse_error("We expect '<OMI> or '<OMA>' or '<OMV>'.");
1.1       tam       386:       ret = null;
                    387:     }
                    388:
                    389:     return ret;
                    390:   }
                    391:
                    392:   private CMO_ZZ parse_OMI() throws IOException{
                    393:     CMO_ZZ ret;
                    394:
                    395:     exceptTokenTypeInParse(TT_StartTag);
                    396:     if(!attribute.equals("OMI")){
                    397:       parse_error("We expect '<OMI>'.");
                    398:     }
                    399:
                    400:     ret = (CMO_ZZ)parse_integer();
                    401:
                    402:     exceptTokenTypeInParse(TT_EndTag);
                    403:     if(!attribute.equals("OMI")){
                    404:       parse_error("We expect '</OMI>'.");
                    405:     }
                    406:
                    407:     return ret;
                    408:   }
                    409:
1.3       tam       410:   private CMO_STRING parse_OMSTR() throws IOException{
                    411:     CMO_STRING ret;
                    412:
                    413:     exceptTokenTypeInParse(TT_StartTag);
                    414:     if(!attribute.equals("OMSTR")){
                    415:       parse_error("We expect '<OMSTR>'.");
                    416:     }
                    417:
                    418:     if(readNextToken() == TT_String){
                    419:       //ret = (CMO_STRING)parse_utf7();
                    420:       ret = new CMO_STRING(attribute);
                    421:     }else{
                    422:       ret = new CMO_STRING("");
                    423:       pushbackLex();
                    424:     }
                    425:
                    426:     exceptTokenTypeInParse(TT_EndTag);
                    427:     if(!attribute.equals("OMSTR")){
                    428:       parse_error("We expect '</OMSTR>'.");
                    429:     }
                    430:
                    431:     return ret;
                    432:   }
                    433:
1.1       tam       434:   private CMO parse_symb_DMP() throws IOException{
                    435:     parse_object();
                    436:     return new CMO_DISTRIBUTED_POLYNOMIAL(new CMO_DMS_GENERIC(),
                    437:                                          parse_symb_SDMP());
                    438:   }
                    439:
                    440:   private CMO_MONOMIAL32[] parse_symb_SDMP() throws IOException{
                    441:     Vector mono = new Vector();
                    442:     CMO_MONOMIAL32[] ret;
                    443:
                    444:     exceptTokenTypeInParse(TT_StartTag);
                    445:     if(!attribute.equals("OMA")){
                    446:       parse_error("We expect '<OMA>'.");
                    447:     }
                    448:
                    449:     if(!parse_symbol().equals("SDMP")){
                    450:       parse_error("We expect '<SDMP>'");
                    451:     }
                    452:
                    453:     while(readNextToken() != TT_EndTag){
                    454:       pushbackLex();
                    455:       mono.addElement(parse_symb_Monom());
                    456:     }
                    457:
                    458:     if(!attribute.equals("OMA")){
                    459:       parse_error("We expect '</OMA>'.");
                    460:     }
                    461:
                    462:     ret = new CMO_MONOMIAL32[mono.size()];
                    463:     mono.copyInto((Object[])ret);
                    464:
                    465:     return ret;
                    466:   }
                    467:
                    468:   private CMO_MONOMIAL32 parse_symb_Monom() throws IOException{
                    469:     Vector degree = new Vector();
1.17      tam       470:     CMO coefficient;
1.1       tam       471:     int[] array;
                    472:
                    473:     exceptTokenTypeInParse(TT_StartTag);
                    474:     if(!attribute.equals("OMA")){
                    475:       parse_error("We expect '<OMA>'.");
                    476:     }
                    477:
                    478:     if(!parse_symbol().equals("Monom")){
                    479:       parse_error("We expect '<Monom>'");
                    480:     }
                    481:
1.17      tam       482:     coefficient = parse_object();
                    483:     if(!(coefficient instanceof CMO_ZZ
                    484:         || coefficient instanceof CMO_QQ
                    485:         || coefficient instanceof CMO_INT32
                    486:         || coefficient instanceof CMO_ZERO)){
                    487:       parse_error("the coefficient of Monomials must be integer or rational.");
                    488:     }
                    489:
1.1       tam       490:     while(readNextToken() != TT_EndTag){
                    491:       pushbackLex();
                    492:       degree.addElement(parse_OMI());
                    493:     }
                    494:
                    495:     if(!attribute.equals("OMA")){
                    496:       parse_error("We expect '<OMA>'.");
                    497:     }
                    498:
1.17      tam       499:     array = new int[degree.size()];
1.1       tam       500:     for(int i=0;i<array.length;i++){
1.17      tam       501:       array[i] = ((CMO_ZZ)degree.elementAt(i)).intValue();
1.1       tam       502:     }
                    503:
1.17      tam       504:     //debug("monom: "+ new CMO_MONOMIAL32(array,coefficient));
                    505:     return new CMO_MONOMIAL32(array,coefficient);
1.1       tam       506:   }
                    507:
1.17      tam       508:   private CMO[] parse_objects() throws IOException{
1.1       tam       509:     Vector objects = new Vector();
                    510:     CMO[] array;
                    511:
                    512:     while(readNextToken() != TT_EndTag){
                    513:       pushbackLex();
                    514:       objects.addElement(parse_object());
                    515:     }
                    516:     pushbackLex();
                    517:
                    518:     objects.trimToSize();
                    519:     array = new CMO[objects.size()];
                    520:     objects.copyInto((Object[])array);
                    521:
1.7       tam       522:     debug("debug :"+ new CMO_LIST(array));
1.17      tam       523:     return array;
1.1       tam       524:   }
                    525:
                    526:   private String parse_symbol() throws IOException{
                    527:     // symbname $B$rJV$9(B
                    528:     String ret = "";
                    529:     StreamTokenizer para;
                    530:
                    531:     exceptTokenTypeInParse(TT_SingleTag);
                    532:
                    533:     para = new StreamTokenizer(new StringReader(attribute));
                    534:     para.resetSyntax();
                    535:     para.wordChars('A','Z');
                    536:     para.wordChars('a','z');
                    537:     para.wordChars('0','9');
                    538:     para.wordChars('\u00A0','\u00FF');
                    539:     para.whitespaceChars('\u0000','\u0020');
                    540:     para.quoteChar('"');
                    541:     para.eolIsSignificant(false);
                    542:     para.lowerCaseMode(true);
                    543:     para.slashStarComments(false);
                    544:     para.slashSlashComments(false);
                    545:     para.ordinaryChar('=');
                    546:
                    547:     if(para.nextToken() != StreamTokenizer.TT_WORD
                    548:        || !para.sval.equalsIgnoreCase("OMS")){
                    549:       parse_error("We expect '<OMS>', but '"+ para.sval +"'.");
                    550:     }
                    551:
                    552:     if(para.nextToken() != StreamTokenizer.TT_WORD){
                    553:       parse_error("We expect 'name' or 'cd'.");
                    554:     }
                    555:     if(para.sval.equals("name")){
                    556:       if(para.nextToken() != '='){
                    557:        parse_error("We expect '='.");
                    558:       }
                    559:       if(para.nextToken() != '"'){
                    560:        parse_error("We expect '\"'.");
                    561:       }
                    562:       ret = parse_symbname(para.sval);
                    563:
                    564:       if(para.nextToken() != StreamTokenizer.TT_WORD
                    565:         || !para.sval.equals("cd")){
                    566:        parse_error("We expect 'cd'.");
                    567:       }
                    568:       if(para.nextToken() != '='){
                    569:        parse_error("We expect '='.");
                    570:       }
                    571:       if(para.nextToken() != '"'){
                    572:        parse_error("We expect '\"'.");
                    573:       }
                    574:       parse_cdname(para.sval);
                    575:
                    576:     }else if(para.sval.equals("cd")){
                    577:       if(para.nextToken() != '='){
                    578:        parse_error("We expect '='.");
                    579:       }
                    580:       if(para.nextToken() != '"'){
                    581:        parse_error("We expect '\"'.");
                    582:       }
                    583:       parse_cdname(para.sval);
                    584:
                    585:       if(para.nextToken() != StreamTokenizer.TT_WORD
                    586:         || !para.sval.equals("name")){
                    587:        parse_error("We expect 'cd'.");
                    588:       }
                    589:       if(para.nextToken() != '='){
                    590:        parse_error("We expect '='.");
                    591:       }
                    592:       if(para.nextToken() != '"'){
                    593:        parse_error("We expect '\"'.");
                    594:       }
                    595:       ret = parse_symbname(para.sval);
                    596:
                    597:     }else{
                    598:       parse_error("We expect 'name' or 'cd'.");
                    599:     }
                    600:
                    601:     if(para.nextToken() != StreamTokenizer.TT_EOF){
                    602:       parse_error("We expect '/>'.");
                    603:     }
                    604:
                    605:     return ret;
                    606:   }
                    607:
                    608:   private CMO parse_integer() throws IOException{
                    609:     //Attribute integer = new Attribute(value);
                    610:     String str;
                    611:     int i=0;
                    612:
                    613:     exceptTokenTypeInParse(TT_String);
                    614:     str = attribute;
                    615:
                    616:     if(str.charAt(i)=='-'){
                    617:       i++;
                    618:     }
                    619:     if(str.length()>=i+2 && str.charAt(i)=='x'){ // for HEX
                    620:       String strhex = str.substring(0,i) + str.substring(i+1);
                    621:       for(i++;i<str.length();i++){
                    622:        if("0123456789ABCDEF".indexOf((int)str.charAt(i)) == -1){
                    623:          parse_error("We expect integer.");
                    624:        }
                    625:       }
                    626:       // 10 $B?J?t$KJQ49(B
                    627:       str = (new java.math.BigInteger(strhex,16)).toString();
                    628:     }else if(str.length()>=i+1){ // for DEC
                    629:       for(;i<str.length();i++){
                    630:        if("0123456789".indexOf((int)str.charAt(i)) == -1){
                    631:          parse_error("We expect integer.");
                    632:        }
                    633:       }
                    634:     }else{
                    635:       parse_error("We expect integer.");
                    636:     }
                    637:
1.13      tam       638:     debug("ZZ: "+ str);
1.1       tam       639:     return new CMO_ZZ(str);
                    640:   }
                    641:
                    642:   private CMO parse_variable() throws IOException{
                    643:     StreamTokenizer para;
                    644:     CMO ret;
                    645:
                    646:     exceptTokenTypeInParse(TT_SingleTag);
                    647:
                    648:     para = new StreamTokenizer(new StringReader(attribute));
                    649:     para.resetSyntax();
                    650:     para.wordChars('A','Z');
                    651:     para.wordChars('a','z');
                    652:     para.wordChars('0','9');
                    653:     para.wordChars('\u00A0','\u00FF');
                    654:     para.whitespaceChars('\u0000','\u0020');
                    655:     para.quoteChar('"');
                    656:     para.eolIsSignificant(false);
                    657:     para.lowerCaseMode(true);
                    658:     para.slashStarComments(false);
                    659:     para.slashSlashComments(false);
                    660:     para.ordinaryChar('=');
                    661:
                    662:     if(para.nextToken() != StreamTokenizer.TT_WORD
                    663:        || !para.sval.equalsIgnoreCase("OMV")){
                    664:       parse_error("We expect '<OMV>'.");
                    665:     }
                    666:
                    667:     if(para.nextToken() != StreamTokenizer.TT_WORD
                    668:        || !para.sval.equals("name")){
                    669:       parse_error("We expect 'name', but obtain '"+ para.sval +"'.");
                    670:     }
                    671:     if(para.nextToken() != '='){
                    672:       parse_error("We expect '='.");
                    673:     }
                    674:     if(para.nextToken() != '"'){
                    675:       parse_error("We expect '\"'.");
                    676:     }
                    677:     ret = new CMO_INDETERMINATE(parse_varname(para.sval));
                    678:
                    679:     if(para.nextToken() != StreamTokenizer.TT_EOF){
                    680:       parse_error("We expect '/>'.");
                    681:     }
                    682:
                    683:     return ret;
                    684:   }
                    685:
                    686:   private String parse_varname(String str){
                    687:     String words = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
                    688:       + "0123456789+='(),-./:?!#$%*;@[]^_`{|}";
                    689:
                    690:     for(int i=0;i<str.length();i++){
                    691:       if(words.indexOf((int)str.charAt(i))==-1){
                    692:        parse_error("string \""+ str +"\" is not varname.");
                    693:       }
                    694:     }
                    695:
                    696:     return str;
                    697:   }
                    698:
                    699:   private String parse_cdname(String str){
                    700:     String alphabet = "abcdefghijklmnopqrstuvwxyz";
                    701:     int i=0;
                    702:
                    703:     if(alphabet.indexOf((int)str.charAt(i))==-1){
                    704:       parse_error("cdname \""+ str +"\" must begin small alphabet.");
                    705:     }
                    706:     for(i++;i<str.length();i++){
                    707:       if((alphabet + "0123456789_").indexOf((int)str.charAt(i))==-1){
                    708:        parse_error("string \""+ str +"\" is not cdname.");
                    709:       }
                    710:     }
                    711:
                    712:     return str;
                    713:   }
                    714:
                    715:   private String parse_symbname(String str) throws IOException{
                    716:     String Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
                    717:     int i=0;
                    718:
                    719:     if(Alphabet.indexOf((int)str.charAt(i))==-1){
                    720:       parse_error("symbname \""+ str +"\" must begin alphabet.");
                    721:     }
                    722:     for(i++;i<str.length();i++){
                    723:       if((Alphabet + "0123456789_").indexOf((int)str.charAt(i))==-1){
                    724:        parse_error("string \""+ str +"\" is not symbname.");
                    725:       }
                    726:     }
                    727:
                    728:     return str;
                    729:   }
                    730:
1.9       tam       731:   private void parse_error(String mesg) throws NumberFormatException{
                    732:     String ret;
                    733:
                    734:     ret = mesg +"\n";
1.17      tam       735:     ret += "error occuered before that :";
1.1       tam       736:     try{
1.17      tam       737:       for(int i=0;i<40;i++){
1.9       tam       738:        ret += (char)is.read();
1.1       tam       739:       }
                    740:     }catch(IOException e){}
1.9       tam       741:     ret += "\n";
                    742:
                    743:     throw new NumberFormatException(ret);
1.3       tam       744:   }
                    745:
                    746:   private void debug(String str){
                    747:     if(debug){
                    748:       System.err.println(str);
                    749:     }
1.1       tam       750:   }
                    751:
                    752:   public static void main(String[] argv) throws IOException{
                    753:     OM2OXM P = new OM2OXM();
                    754:     OpenXM asir;
                    755:     String host = "localhost";
                    756:     int CtrlPort = 1200,StreamPort = 1300;
                    757:
                    758:     for(int i=0;i<argv.length;i++){
                    759:       if(argv[i].equals("-h")){
                    760:        System.out.println("");
                    761:        System.exit(0);
                    762:       }else if(argv[i].equals("-host")){
                    763:        host = argv[++i];
                    764:       }else if(argv[i].equals("-data")){
                    765:        StreamPort = Integer.valueOf(argv[++i]).intValue();
                    766:       }else if(argv[i].equals("-control")){
                    767:        CtrlPort = Integer.valueOf(argv[++i]).intValue();
                    768:       }else{
                    769:        System.err.println("unknown option :"+ argv[i]);
                    770:        System.exit(1);
                    771:       }
                    772:     }
                    773:
                    774:     try{
                    775:       asir = new OpenXM(host,CtrlPort,StreamPort);
                    776:       asir.sendSM(new SM(SM.SM_mathcap));
                    777:     }catch(UnknownHostException e){
                    778:       System.err.println("host unknown.");
                    779:       System.err.println(e.getMessage());
                    780:       return;
                    781:     }catch(IOException e){
                    782:       System.err.println("connection failed.");
                    783:       System.err.println("IOException occuer !!");
                    784:       System.err.println(e.getMessage());
                    785:       return;
                    786:     }
                    787:
                    788:     P.asir = asir;
                    789:     new Thread(P).start();
                    790:     System.out.println("start");
                    791:
                    792:     try{
                    793:       //P.value = new Attribute(System.in);
                    794:
                    795:       //$B%5!<%PB&$+$iAw?.$5$l$?J8;zNs$r<u?.$7$^$9!#(B
                    796:       while(true){
1.9       tam       797:        try{
                    798:          CMO obj = P.parse(System.in);
                    799:          asir.send(obj);
                    800:          asir.sendSM(new SM(SM.SM_popCMO));
                    801:        }catch(NumberFormatException e){
1.16      tam       802:          System.err.println(e.getMessage());
                    803:        }catch(MathcapViolation e){
1.9       tam       804:          System.err.println(e.getMessage());
                    805:        }
1.1       tam       806:       }
                    807:
                    808:     }catch(IOException e){
                    809:       e.printStackTrace();
1.9       tam       810:     }finally{
                    811:       System.out.println("breaking...");
1.1       tam       812:     }
                    813:   }
                    814: }

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