View Javadoc

1   package fr.ove.openmath.mfd2;
2   
3   import java.awt.*;
4   import java.io.*;
5   import java.net.SocketException;
6   import fr.ove.openmath.jome.Jome;
7   import fr.ove.openmath.exceptions.*;
8   import fr.ove.errordialog.*;
9   import fr.inria.openmath.omapi.*;
10  import fr.inria.openmath.omapi.implementation.*;
11  import fr.ove.openmath.mfd2.PanelMfd2;
12  import fr.ove.openmath.mfd2.omparser.Mfd2ParserHandler;
13  
14  public class Selector extends Thread {
15      private PanelMfd2 panelMfd2;
16      private OpenMathResultSetable jomeOwner;
17      
18      private InputStream inputStream;
19      private XMLParser parser;
20      private Mfd2ParserHandler mfd2ParserHandler = new Mfd2ParserHandler();
21      
22      /***
23      * The constructor.
24      * @param panelMfd2 where the instance takes the object to select from.
25      */
26      public Selector(PanelMfd2 panelMfd2, OpenMathResultSetable jomeOwner) {
27          this.panelMfd2 = panelMfd2;
28          // A l'instanciation, c'est panelMfd2
29          this.jomeOwner = jomeOwner;
30          inputStream = panelMfd2.getInputStream();
31          parser = new XMLParser(mfd2ParserHandler);
32      }
33      
34      public void setOwner(OpenMathResultSetable jomeOwner) {
35          this.jomeOwner = jomeOwner;
36      }
37      
38      public void run() {
39          String symbol;
40          while (true) {
41              try {
42                  parser.initParse(inputStream);
43                  
44                  int dispo = 0;
45                  while ((dispo = inputStream.available()) == 0) {
46                      try {
47                          Thread.sleep(100);
48                      }
49                      catch (Exception e) {
50                      }
51                  }
52                  
53                  parser.parseObject(inputStream);
54                  
55                  jomeOwner.setOpenMathResult(mfd2ParserHandler.getOmObjectParsed());
56                  
57                  if (panelMfd2.getCurrentRequest() != jomeOwner)
58                      jomeOwner = panelMfd2.getCurrentRequest();
59                  
60              }
61              catch (OMException e) {
62          	    switch (e.getErrorCode()) {
63          	        case OMException.OME_WRAPPING :
64          	            try {
65          	                if (e.getException() != null) 
66              	                throw(e.getException());
67              	            else
68                  	            popError("Error During Parsing", e.getMessage(), mfd2ParserHandler.getOmObjectParsed());
69                              
70                              try {
71                                  parser.skipObject(inputStream);
72                              }
73                              catch (Exception ioe) {
74                                  ioe.printStackTrace();
75                              }
76                              
77              	                
78          	            }
79          	            catch (ControlException controlException) {
80                      	    symbol = controlException.getSymbol();
81                      	    Symbol theSymbol;
82                      	    String omObject;
83                      	    
84                      	    if (symbol.equals("terminated") || symbol.equals("commandOk"))
85                      	        panelMfd2.setStatusMessage("< " + controlException.getErrorMessage() + "\n");
86                      	    else if (symbol.equals("unhandledSymbol") || symbol.equals("unknownSymbol")) {
87                      	        theSymbol = (Symbol) controlException.getDetails();
88                      	        omObject = "<OMOBJ>\n  <OMA>\n    <OMS cd=\"control\" name=\"unhandledSymbol\"/>\n    <OMS cd=\"" + theSymbol.getCD() + "\" name=\"" + theSymbol.getName() + "\" />\n  </OMA>\n</OMOBJ>";
89                      	        panelMfd2.setObject(omObject.getBytes());
90                      	        panelMfd2.setStatusMessage("< " + controlException.getErrorMessage() + " : <OMS cd=\"" + theSymbol.getCD() + "\" name=\"" + theSymbol.getName() + "\" />\n");
91                      	        panelMfd2.setStatusMessage("< error message sent\n");
92                              }
93                              else if (symbol.equals("unknownCD")) {
94                      	        theSymbol = (Symbol) controlException.getDetails();
95                      	        omObject = "<OMOBJ>\n  <OMA>\n    <OMS cd=\"control\" name=\"unknownCD\"/>\n    <OMSTR>" + theSymbol.getCD() + "</OMSTR>\n  </OMA>\n</OMOBJ>";
96                      	        panelMfd2.setObject(omObject.getBytes());
97                      	        panelMfd2.setStatusMessage("< " + controlException.getErrorMessage() + " : " + theSymbol.getCD() + "\n");
98                      	        panelMfd2.setStatusMessage("< error message sent\n");
99                              }
100                             else 
101                                 popError(controlException);
102                         }
103                     	catch (Mfd2Exception mfd2Exception) {
104                 	        panelMfd2.setStatusMessage("< " + mfd2Exception.getErrorMessage() + "\n");
105                             popError(mfd2Exception);
106                         }
107                         catch (Exception excep) {
108                             excep.printStackTrace();
109                         }
110                         break;
111                         
112         	        case OMException.OME_NODE_NOT_FOUND:
113         	            popError("OpenMath Node Not Found", e.getMessage(), mfd2ParserHandler.getOmObjectParsed());
114                 		try {
115                 		    parser.skipObject(inputStream);
116                     	}
117                     	catch (Exception ioe) {
118                     	    ioe.printStackTrace();
119                     	}
120         	            break;
121         	            
122         	        case OMException.OME_SYNTAX:
123         	            popError("Syntax Error", e.getMessage(), mfd2ParserHandler.getOmObjectParsed());
124                 		try {
125                 		    parser.skipObject(inputStream);
126                     	}
127                     	catch (Exception ioe) {
128                     	    ioe.printStackTrace();
129                     	}
130         	            break;
131         	            
132         	        default :
133         	            popError("Error During Parsing", e.getMessage(), mfd2ParserHandler.getOmObjectParsed());
134                 		try {
135                 		    parser.skipObject(inputStream);
136                     	}
137                     	catch (Exception ioe) {
138                     	    ioe.printStackTrace();
139                     	}
140                 }
141         	}
142             catch (SocketException se) {
143                 return; // On a fermÈ la socket, donc on sort.
144             }
145             catch (Exception ex) {
146                 ex.printStackTrace();
147             }
148         }
149     }
150     
151     /***
152     * Pop up an error dialog window.
153     * @param errorMessageException the error message an possible details.
154     */
155     private void popError(ErrorMessageException errorMessageException) {
156         popError("Mfd2 Error", errorMessageException.getErrorMessage(), errorMessageException.getDetails());
157     }        
158 
159     /***
160     * Pop up an error dialog window.
161     */
162     private void popError(String nameError, String errorMessage, Object details) {
163         Container theFrame = panelMfd2;
164         while (!(theFrame instanceof Frame))
165             theFrame = theFrame.getParent();
166         
167 		ErrorDialog dial = new ErrorDialog((Frame) theFrame, true);
168 	    Rectangle bounds = theFrame.getBounds();
169 	    dial.setBounds(bounds.x + bounds.width/2, bounds.y + bounds.height/2, 390,195);
170 	    
171 	    dial.setResizable(false);
172 	    MainPanelError mpe = new MainPanelError(dial, nameError);
173 	    mpe.setMesg2(errorMessage);
174 	    if (details != null)
175     	    mpe.setDetails(details.toString());
176     	    
177 		dial.addMesg(mpe);
178 		dial.setVisible(true);
179     }        
180 }
181 
182 
183 
184 
185 
186 
187 /*
188 try {
189     parser.skipObject(inputStream);
190     
191     ?? pourquoi parser.skipObject(inputStream) ok et pas ce qui suit ????
192     
193 	int available = inputStream.available();
194 	while (available > 0) {
195 	    
196 	    System.out.println("On skip " + available + " bytes");
197 	    
198 	    inputStream.skip(available);
199 	    available = inputStream.available();
200 	}
201 	
202 }
203 catch (/*IO*Exception ioe) {
204     ioe.printStackTrace();
205 }
206 */