001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.compapp.projects.common;
042:
043: import java.io.IOException;
044: import java.io.PrintWriter;
045: import java.io.StringWriter;
046: import java.util.List;
047: import java.util.logging.Level;
048: import java.util.logging.Logger;
049: import org.netbeans.api.project.Project;
050: import org.netbeans.modules.compapp.projects.common.CatalogWSDL.Entry;
051: import org.openide.filesystems.FileObject;
052: import org.openide.util.NbBundle;
053:
054: /**
055: * This class can be used to validate the implicit catalog entries. It reads
056: * the catalog.wsdl and checks if the wsdl/xsd imports in the catalog.wsdl have a
057: * valid uri reference for their system id (location/schemaLocation). It first
058: * will locate the file object corresponding to the uri reference and then
059: * optionally load xdm model also to validate the contents.
060: * Report error, warning and info level messages to the print output (defaults
061: * to the system.out). This print output can be redirected by setting the ResultPrinter
062: * on the validator.
063: * <p><blockquote><pre>
064: * Project prj = ... // get the project
065: * ImplicitCatalogValidator validator = new ImplicitCatalogValidator(prj);
066: * StringResult out = new StringResult(); // results printed to a string.
067: * validator.setResultPrinter(out);
068: * boolean validated = validator.validate();
069: * if (validated) {
070: * // continue.
071: * } else {
072: * System.out.println("Validation Failed:" + out.toString());
073: * }
074: *
075: *
076: * </blockquote></pre><p>
077: *
078: * @see ResultPrinter
079: * @author chikkala
080: */
081: public class ImplicitCatalogValidator {
082:
083: /** logger */
084: private static final Logger sLogger = Logger
085: .getLogger(ImplicitCatalogValidator.class.getName());
086: /** Project in which the implicit catalog is validated*/
087: private Project mPrj;
088: /** catalog wsdl file model */
089: private CatalogWSDL mCatalogWSDL;
090: /** implicit catalog support reference */
091: private ImplicitCatalogSupport mCatalogSupport;
092: /** printer to print results */
093: private ResultPrinter mPrn;
094:
095: /**
096: * create constructor that validates the implicit catalog.
097: * @param catSupport
098: * @param catWSDL
099: */
100: public ImplicitCatalogValidator(ImplicitCatalogSupport catSupport,
101: CatalogWSDL catWSDL) {
102: this .mPrn = new ResultPrinter();
103: this .mCatalogSupport = catSupport;
104: this .mCatalogWSDL = catWSDL;
105: }
106:
107: /**
108: * This method creates a new ImplicitCatalogValidator using the
109: * ImplicitCatalogSupport and the Catalog wsdl file loaded from the project.
110: * @param prj project on which the validation should be performed.
111: * @return validator
112: * @throws java.io.IOException on error
113: */
114: public static ImplicitCatalogValidator newInstance(Project prj)
115: throws IOException {
116: ImplicitCatalogValidator newInstance = null;
117: ImplicitCatalogSupport catSupport = ImplicitCatalogSupport
118: .getInstance(prj);
119: CatalogWSDL catWSDL = CatalogWSDL.loadCatalogWSDL(prj);
120: newInstance = new ImplicitCatalogValidator(catSupport, catWSDL);
121: return newInstance;
122: }
123:
124: /**
125: * sets the result printer. By default a printer is initialized that prints
126: * the results to stdout.
127: * @param out
128: */
129: public void setResultPrinter(ResultPrinter out) {
130: this .mPrn = out;
131: }
132:
133: /**
134: * return printer
135: * @return
136: */
137: protected ResultPrinter getResultPrinter() {
138: return this .mPrn;
139: }
140:
141: /**
142: *
143: * @return
144: */
145: protected CatalogWSDL getCatalogWSDL() {
146: return this .mCatalogWSDL;
147: }
148:
149: /**
150: *
151: * @return
152: */
153: protected ImplicitCatalogSupport getImplicitCatalogSupport() {
154: return this .mCatalogSupport;
155: }
156:
157: /**
158: * validates the catalog.wsdl entries by checking the correpsonding entries
159: * in the catalog.xml and also existance of the actual file and optionally
160: * its valid contents (checks if the xam model is valid or not).
161: * @return true if the validation is successful or false if any of the validation
162: * is failed with errors.
163: */
164: public boolean validate() {
165: String startedMsg = NbBundle.getMessage(
166: ImplicitCatalogValidator.class,
167: "icat.validator.msg.started");
168: String finishedMsg = NbBundle.getMessage(
169: ImplicitCatalogValidator.class,
170: "icat.validator.msg.finished");
171: this .mPrn.println(startedMsg);
172: List<Entry> entries = this .mCatalogWSDL.getEntries();
173: for (Entry entry : entries) {
174: FileObject fo = null;
175: try {
176: fo = this .mCatalogSupport
177: .resolveImplicitReference(entry.getLocation());
178: printResult(entry, fo);
179: } catch (Exception ex) {
180: printResult(entry, ex);
181: sLogger.log(Level.FINE, ex.getMessage(), ex);
182: }
183: }
184: this .mPrn.println(finishedMsg);
185: return true;
186: }
187:
188: /**
189: * prints out the result of validating an entry in catalog.wsdl
190: * @param entry entry in the catalog.wsdl
191: * @param fo resolved file corresponding to that entry.
192: */
193: protected void printResult(Entry entry, FileObject fo) {
194: String systemIdMsg = NbBundle.getMessage(this .getClass(),
195: "icat.validator.msg.entry.systemid", entry
196: .getLocation());
197: String typeMsg = NbBundle.getMessage(this .getClass(),
198: "icat.validator.msg.entry.type", entry.getType());
199: if (fo == null) {
200: String errMsg = NbBundle.getMessage(this .getClass(),
201: "icat.validator.msg.error", entry.getNamesapce());
202: String notFoundMsg = NbBundle.getMessage(this .getClass(),
203: "icat.validator.msg.entry.result.file.not.found");
204: String resultMsg = NbBundle.getMessage(this .getClass(),
205: "icat.validator.msg.entry.result", notFoundMsg);
206:
207: this .mPrn.println(errMsg);
208: this .mPrn.println(systemIdMsg);
209: this .mPrn.println(typeMsg);
210: this .mPrn.println(resultMsg);
211: } else {
212: String infoMsg = NbBundle.getMessage(this .getClass(),
213: "icat.validator.msg.info", entry.getNamesapce());
214: String fileMsg = NbBundle.getMessage(this .getClass(),
215: "icat.validator.msg.entry.result.file", fo
216: .getPath());
217: String resultMsg = NbBundle.getMessage(this .getClass(),
218: "icat.validator.msg.entry.result", fileMsg);
219: this .mPrn.println(infoMsg);
220: this .mPrn.println(systemIdMsg);
221: this .mPrn.println(typeMsg);
222: this .mPrn.println(resultMsg);
223: }
224: }
225:
226: /**
227: * prints any exception results in validating the entry
228: * @param entry
229: * @param ex
230: */
231: protected void printResult(Entry entry, Exception ex) {
232: String errMsg = NbBundle.getMessage(this .getClass(),
233: "icat.validator.msg.error", entry.getNamesapce());
234: String systemIdMsg = NbBundle.getMessage(this .getClass(),
235: "icat.validator.msg.entry.systemid", entry
236: .getLocation());
237: String typeMsg = NbBundle.getMessage(this .getClass(),
238: "icat.validator.msg.entry.type", entry.getType());
239: String resultMsg = NbBundle.getMessage(this .getClass(),
240: "icat.validator.msg.entry.result", ex.getMessage());
241: this .mPrn.println(errMsg);
242: this .mPrn.println(systemIdMsg);
243: this .mPrn.println(typeMsg);
244: this .mPrn.println(resultMsg);
245:
246: // TODO: print statcktrace to log
247: }
248:
249: /**
250: * This class is a print interface for the validation results. By default it
251: * prints the result output to stdout. Extended classes
252: * from this class can override this behaviour by setting different output
253: * writers which may redirect the printing to various outputs (like special
254: * output window, or logger etc).
255: */
256: public static class ResultPrinter {
257:
258: /** standard output writer*/
259: private PrintWriter mOut;
260: /** error output writer */
261: private PrintWriter mErr;
262:
263: /** constructor */
264: public ResultPrinter() {
265: initWriters();
266: }
267:
268: /**
269: * initializes the writers. By default it initializes writes to system.out and err.
270: * Called from the constructor to initialize the writers. extended classes
271: * can overwrite this method to initilize the error and output writers to
272: * different writers.
273: */
274: protected void initWriters() {
275: PrintWriter out = new PrintWriter(System.out, true);
276: PrintWriter err = new PrintWriter(System.err, true);
277: setOutWriter(out);
278: setErrorWriter(err);
279: }
280:
281: /**
282: *
283: * @param out
284: */
285: protected final void setOutWriter(PrintWriter out) {
286: this .mOut = out;
287: }
288:
289: /**
290: *
291: * @return
292: */
293: protected final PrintWriter getOutWriter() {
294: return this .mOut;
295: }
296:
297: /**
298: *
299: * @param err
300: */
301: protected final void setErrorWriter(PrintWriter err) {
302: this .mErr = err;
303: }
304:
305: /**
306: *
307: * @return
308: */
309: protected final PrintWriter getErrorWriter() {
310: return this .mErr;
311: }
312:
313: /**
314: *
315: * @param obj
316: */
317: public void println(Object obj) {
318: this .mOut.println(obj);
319: }
320:
321: /**
322: *
323: * @param obj
324: */
325: public void error(Object obj) {
326: this .mErr.print(obj);
327: }
328:
329: /**
330: *
331: * @param obj
332: */
333: public void warning(Object obj) {
334: this .mErr.print(obj);
335: }
336: }
337:
338: /**
339: * ResultPrinter that prints results to a string.
340: */
341: public static class StringResult extends ResultPrinter {
342:
343: private StringWriter mStrWriter;
344:
345: /**
346: * initializes both error and outout writers to a string writer.
347: */
348: @Override
349: protected void initWriters() {
350: this .mStrWriter = new StringWriter();
351: PrintWriter out = new PrintWriter(mStrWriter, true);
352: setOutWriter(out);
353: setErrorWriter(out); // both err and out to same file.
354: }
355:
356: @Override
357: public String toString() {
358: return this.mStrWriter.getBuffer().toString();
359: }
360: }
361: }
|