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

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

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

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