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

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

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

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