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

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

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

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