1 19 package org.netbeans.modules.websvc.wsdl.validator; 20 21 import java.io.IOException ; 22 import java.util.List ; 23 import javax.swing.JEditorPane ; 24 import javax.swing.text.StyledDocument ; 25 import org.netbeans.modules.xml.wsdl.model.WSDLComponent; 26 import org.netbeans.modules.xml.xam.Component; 27 import org.netbeans.modules.xml.xam.dom.DocumentComponent; 28 import org.netbeans.modules.xml.xam.spi.Validator.ResultItem; 29 import org.netbeans.modules.xml.xam.spi.Validator.ResultType; 30 import org.openide.ErrorManager; 31 import org.openide.filesystems.FileObject; 32 import org.openide.loaders.DataObject; 33 import org.openide.loaders.DataObjectNotFoundException; 34 import org.openide.text.CloneableEditorSupport; 35 import org.openide.text.NbDocument; 36 import org.openide.util.NbBundle; 37 import org.openide.windows.IOProvider; 38 import org.openide.windows.InputOutput; 39 import org.openide.windows.OutputListener; 40 import org.openide.windows.OutputWriter; 41 42 47 public class ValidationOutputWindow { 48 49 OutputWriter normalWriter; 50 OutputWriter errorWriter; 51 52 55 public ValidationOutputWindow() { 56 initialize(); 57 } 58 59 60 64 public void displayValidationInformation(List <ResultItem> validationInformation) { 65 66 for(ResultItem resultItem: validationInformation) { 67 ResultType resultType = resultItem.getType(); 68 69 Component component = resultItem.getComponents(); 70 71 try { 72 if(resultType.equals(ResultType.ERROR)) 73 showError(resultItem); 74 else if(resultType.equals(ResultType.WARNING)) 75 showWarning(resultItem); 76 else if(resultType.equals(ResultType.ADVICE)) 77 showAdvice(resultItem); 78 } catch (IOException ex) { 79 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 80 } 81 normalWriter.println(""); 82 83 } 84 85 printCountSummary(validationInformation); 86 } 87 88 89 90 91 94 private void initialize() { 95 InputOutput io = IOProvider.getDefault().getIO(NbBundle. 96 getMessage(ValidationOutputWindow.class, "TITLE_XML_check_window"), false); 97 98 normalWriter = io.getOut(); 99 errorWriter = io.getErr(); 100 } 101 102 103 106 private void showError(ResultItem resultItem) 107 throws IOException { 108 109 OutputListener listener = new ValidationOutputListener(resultItem); 110 errorWriter.println(getFileName(resultItem) + ":" + 111 getLineNumber(resultItem) + "," + getColumnNumber(resultItem), 112 listener, true); 113 errorWriter.println(resultItem.getDescription()); 114 } 115 116 117 120 private void showWarning(ResultItem resultItem) 121 throws IOException { 122 OutputListener listener = new ValidationOutputListener(resultItem); 123 errorWriter.println(getFileName(resultItem) + ":" + 124 getLineNumber(resultItem) + "," + getColumnNumber(resultItem), 125 listener, false); 126 errorWriter.println(resultItem.getDescription()); 127 } 128 129 130 133 private void showAdvice(ResultItem resultItem) 134 throws IOException { 135 normalWriter.println(getFileName(resultItem) + " :" + 136 getLineNumber(resultItem) + "," + getColumnNumber(resultItem)); 137 normalWriter.println(resultItem.getDescription()); 138 } 139 140 141 144 private String getFileName(ResultItem resultItem) { 145 String fileName; 146 147 assert resultItem.getModel() != null: "Model associated with ResultItem is null"; fileName = ((FileObject) resultItem.getModel().getModelSource(). 149 getLookup().lookup(FileObject.class)).getPath(); 150 151 return fileName; 152 } 153 154 155 158 private StyledDocument getStyledDocument(Component component) { 159 int position = 0; 160 DataObject dobj = null; 161 162 try { 163 dobj = DataObject.find((FileObject) component.getModel(). 164 getModelSource().getLookup().lookup(FileObject.class)); 165 } catch (DataObjectNotFoundException ex) { 166 ErrorManager.getDefault().notify(ex); 167 } 168 169 CloneableEditorSupport editor = (CloneableEditorSupport)dobj. 170 getCookie(org.openide.cookies.EditorCookie.class); 171 StyledDocument doc = editor.getDocument(); 172 173 return doc; 174 } 175 176 177 180 private int getLineNumber(ResultItem resultItem) { 181 int lineNumber; 182 183 if(resultItem.getComponents() != null) { 184 StyledDocument doc = getStyledDocument(resultItem.getComponents()); 185 int position = getPosition(resultItem.getComponents()); 186 lineNumber = NbDocument.findLineNumber(doc, position) + 1; 187 } else { 188 lineNumber = resultItem.getLineNumber(); 189 } 190 return lineNumber; 191 } 192 193 194 197 private int getColumnNumber(ResultItem resultItem) { 198 int columnNumber; 199 200 if(resultItem.getComponents() != null) { 201 StyledDocument doc = getStyledDocument(resultItem.getComponents()); 202 int position = getPosition(resultItem.getComponents()); 203 columnNumber = NbDocument.findLineColumn(doc, position); 204 } else { 205 columnNumber = resultItem.getColumnNumber(); 206 } 207 return columnNumber; 208 } 209 210 211 214 private int getPosition(Component component) { 215 int position = 0; 216 217 if(component instanceof DocumentComponent) { 219 position = ((DocumentComponent)component).findPosition(); 220 } 221 return position; 222 } 223 224 225 private void printCountSummary(List <ResultItem> validationInformation) { 226 int warnings = 0; 227 int errors = 0; 228 229 for(ResultItem resultItem: validationInformation) { 230 if(resultItem.getType().equals(ResultType.ERROR)) 231 errors ++; 232 else if(resultItem.getType().equals(ResultType.WARNING)) 233 warnings++; 234 } 235 236 normalWriter.println(errors + " Error(s), " + warnings +" Warning(s)."); 237 } 238 239 240 241 244 private final class ValidationOutputListener 245 implements org.openide.windows.OutputListener { 246 247 FileObject fileObject; 248 ResultItem resultItem; 249 250 public ValidationOutputListener(ResultItem resultItem) { 251 fileObject = (FileObject) resultItem.getModel(). 252 getModelSource().getLookup().lookup(FileObject.class); 253 this.resultItem = resultItem; 254 } 255 256 public void outputLineSelected(org.openide.windows.OutputEvent ev) { 257 } 258 259 public void outputLineAction(org.openide.windows.OutputEvent ev) { 260 261 try { 262 DataObject dataObject = DataObject.find(fileObject); 263 264 if(dataObject == null) 265 return; 266 267 CloneableEditorSupport editor = (CloneableEditorSupport)dataObject. 268 getCookie(org.openide.cookies.EditorCookie.class); 269 JEditorPane [] panes = editor.getOpenedPanes(); 270 boolean focusFound = false; 271 if(panes.length > 0){ 272 for(int i = 0; i < panes.length; i++){ 273 JEditorPane pane = panes[i]; 274 if(pane.hasFocus()){ 275 setCaretPosition(pane); 276 focusFound = true; 277 break; 278 } 279 } 280 if(!focusFound){ 281 JEditorPane pane = panes[0]; 282 pane.requestFocusInWindow(); 283 setCaretPosition(pane); 284 } 285 } 286 } catch (IOException ex){ 287 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 288 } 289 290 } 291 292 private void setCaretPosition(JEditorPane pane){ 293 Component component = resultItem.getComponents(); 294 if(component instanceof WSDLComponent){ 295 int position = ((WSDLComponent)component).findPosition(); 296 pane.setCaretPosition(position); 297 }else { 298 int line = resultItem.getLineNumber(); 299 try { 300 int position = NbDocument.findLineOffset( 301 (StyledDocument )pane.getDocument(),line); 302 pane.setCaretPosition(position); 303 } catch (IndexOutOfBoundsException iob) { 304 } 306 } 307 } 308 309 public void outputLineCleared(org.openide.windows.OutputEvent ev) { 310 } 311 } 312 } | Popular Tags |