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

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

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

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