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:
042: package org.netbeans.modules.web.struts;
043:
044: //import org.netbeans.modules.xml.catalog.settings.CatalogSettings;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.util.logging.Level;
048: import java.util.logging.Logger;
049: import org.netbeans.modules.web.struts.config.model.StrutsConfig;
050: import org.openide.filesystems.FileObject;
051: import org.openide.loaders.DataObjectExistsException;
052: import org.openide.loaders.MultiDataObject;
053: import org.openide.nodes.CookieSet;
054: import org.openide.nodes.Node;
055: import org.openide.util.Lookup;
056: import org.xml.sax.InputSource;
057:
058: import org.netbeans.api.xml.cookies.ValidateXMLCookie;
059: import org.netbeans.api.xml.cookies.CheckXMLCookie;
060: import org.netbeans.modules.xml.api.XmlFileEncodingQueryImpl;
061: import org.netbeans.spi.queries.FileEncodingQueryImplementation;
062: import org.netbeans.spi.xml.cookies.*;
063: import org.w3c.dom.Document;
064: import org.xml.sax.ErrorHandler;
065: import org.xml.sax.SAXException;
066: import org.xml.sax.SAXParseException;
067:
068: /**
069: *
070: * @author Petr Pisl
071: */
072: public class StrutsConfigDataObject extends MultiDataObject implements
073: org.openide.nodes.CookieSet.Factory {
074:
075: private static StrutsCatalog strutsCatalog = new StrutsCatalog();
076: private boolean documentDirty = true;
077: private boolean documentValid = true;
078: protected boolean nodeDirty = false;
079: private InputStream inputStream;
080: private SAXParseError error;
081: private StrutsConfig lastGoodConfig = null;
082:
083: /** Editor support for text data object. */
084: private transient StrutsConfigEditorSupport editorSupport;
085:
086: /** Property name for property documentValid */
087: public static final String PROP_DOC_VALID = "documentValid"; // NOI18N
088:
089: /** Creates a new instance of StrutsConfigDataObject */
090: public StrutsConfigDataObject(FileObject pf,
091: StrutsConfigLoader loader) throws DataObjectExistsException {
092: super (pf, loader);
093: init();
094:
095: }
096:
097: private void init() {
098: CookieSet cookies = getCookieSet();
099:
100: getCookieSet().add(StrutsConfigEditorSupport.class, this );
101: getCookieSet().assign(FileEncodingQueryImplementation.class,
102: XmlFileEncodingQueryImpl.singleton());
103: // Creates Check XML and Validate XML context actions
104: InputSource in = DataObjectAdapters.inputSource(this );
105: CheckXMLCookie checkCookie = new CheckXMLSupport(in);
106: getCookieSet().add(checkCookie);
107: ValidateXMLCookie validateCookie = new ValidateXMLSupport(in);
108: getCookieSet().add(validateCookie);
109: }
110:
111: /**
112: * Provides node that should represent this data object. When a node for
113: * representation in a parent is requested by a call to getNode (parent)
114: * it is the exact copy of this node
115: * with only parent changed. This implementation creates instance
116: * <CODE>DataNode</CODE>.
117: * <P>
118: * This method is called only once.
119: *
120: * @return the node representation for this data object
121: * @see DataNode
122: */
123: protected synchronized Node createNodeDelegate() {
124: return new StrutsConfigNode(this );
125: }
126:
127: /** Implements <code>CookieSet.Factory</code> interface. */
128: public Node.Cookie createCookie(Class clazz) {
129: if (clazz.isAssignableFrom(StrutsConfigEditorSupport.class))
130: return getEditorSupport();
131: else
132: return null;
133: }
134:
135: @Override
136: public Lookup getLookup() {
137: return getCookieSet().getLookup();
138: }
139:
140: /** Gets editor support for this data object. */
141: public StrutsConfigEditorSupport getEditorSupport() {
142: if (editorSupport == null) {
143: synchronized (this ) {
144: if (editorSupport == null)
145: editorSupport = new StrutsConfigEditorSupport(this );
146: }
147: }
148:
149: return editorSupport;
150: }
151:
152: public StrutsConfig getStrutsConfig() throws java.io.IOException {
153: if (lastGoodConfig == null)
154: parsingDocument();
155: return lastGoodConfig;
156: }
157:
158: public StrutsConfig getStrutsConfig(boolean parsenow)
159: throws java.io.IOException {
160: if (parsenow) {
161: StrutsConfig previous = lastGoodConfig;
162: parsingDocument();
163: if (lastGoodConfig == null)
164: lastGoodConfig = previous;
165: }
166: return getStrutsConfig();
167: }
168:
169: /** This method is used for obtaining the current source of xml document.
170: * First try if document is in the memory. If not, provide the input from
171: * underlayed file object.
172: * @return The input source from memory or from file
173: * @exception IOException if some problem occurs
174: */
175: protected InputStream prepareInputSource()
176: throws java.io.IOException {
177: if ((getEditorSupport() != null)
178: && (getEditorSupport().isDocumentLoaded())) {
179: // loading from the memory (Document)
180: return getEditorSupport().getInputStream();
181: } else {
182: return getPrimaryFile().getInputStream();
183: }
184: }
185:
186: /** This method has to be called everytime after prepareInputSource calling.
187: * It is used for closing the stream, because it is not possible to access the
188: * underlayed stream hidden in InputSource.
189: * It is save to call this method without opening.
190: */
191: protected void closeInputSource() {
192: InputStream is = inputStream;
193: if (is != null) {
194: try {
195: is.close();
196: } catch (IOException e) {
197: // nothing to do, if exception occurs during saving.
198: }
199: if (is == inputStream) {
200: inputStream = null;
201: }
202: }
203: }
204:
205: public void write(StrutsConfig config) throws java.io.IOException {
206: java.io.File file = org.openide.filesystems.FileUtil
207: .toFile(getPrimaryFile());
208: org.openide.filesystems.FileObject configFO = getPrimaryFile();
209: try {
210: org.openide.filesystems.FileLock lock = configFO.lock();
211: try {
212: java.io.OutputStream os = configFO
213: .getOutputStream(lock);
214: try {
215: config.write(os);
216: } finally {
217: os.close();
218: }
219: } finally {
220: lock.releaseLock();
221: }
222: } catch (org.openide.filesystems.FileAlreadyLockedException ex) {
223: // PENDING should write a message
224: }
225: }
226:
227: /** This method parses XML document and calls updateNode method which
228: * updates corresponding Node.
229: */
230: public void parsingDocument() {
231: error = null;
232: try {
233: error = updateNode(prepareInputSource());
234: } catch (Exception e) {
235: Logger.getLogger("global").log(Level.INFO, null, e);
236: setDocumentValid(false);
237: return;
238: } finally {
239: closeInputSource();
240: documentDirty = false;
241: }
242: if (error == null) {
243: setDocumentValid(true);
244: } else {
245: setDocumentValid(false);
246: }
247: setNodeDirty(false);
248: }
249:
250: public void setDocumentValid(boolean valid) {
251: if (documentValid != valid) {
252: if (valid)
253: repairNode();
254: documentValid = valid;
255: firePropertyChange(PROP_DOC_VALID,
256: !documentValid ? Boolean.TRUE : Boolean.FALSE,
257: documentValid ? Boolean.TRUE : Boolean.FALSE);
258: }
259: }
260:
261: /** This method repairs Node Delegate (usually after changing document by property editor)
262: */
263: protected void repairNode() {
264: // PENDING: set the icon in Node
265: // ((DataNode)getNodeDelegate()).setIconBase (getIconBaseForValidDocument());
266: org.openide.awt.StatusDisplayer.getDefault().setStatusText(""); // NOI18N
267: /* if (inOut!=null) {
268: inOut.closeInputOutput();
269: errorAnnotation.detach();
270: }*/
271: }
272:
273: private org.w3c.dom.Document getDomDocument(InputStream inputSource)
274: throws SAXParseException {
275: try {
276: // creating w3c document
277: org.w3c.dom.Document doc = org.netbeans.modules.schema2beans.GraphManager
278: .createXmlDocument(new org.xml.sax.InputSource(
279: inputSource), false, strutsCatalog,
280: new J2eeErrorHandler(this ));
281: return doc;
282: } catch (Exception e) {
283: // XXX Change that
284: throw new SAXParseException(e.getMessage(),
285: new org.xml.sax.helpers.LocatorImpl());
286: }
287: }
288:
289: /** Update the node from document. This method is called after document is changed.
290: * @param is Input source for the document
291: * @return number of the line with error (document is invalid), 0 (xml document is valid)
292: */
293: // TODO is prepared for handling arrors, but not time to finish it.
294: protected SAXParseError updateNode(InputStream is)
295: throws java.io.IOException {
296: try {
297: Document doc = getDomDocument(is);
298: lastGoodConfig = StrutsConfig.createGraph(doc);
299: } catch (SAXParseException ex) {
300: return new SAXParseError(ex);
301: } catch (SAXException ex) {
302: throw new IOException();
303: }
304: return null;
305: }
306:
307: public boolean isDocumentValid() {
308: return documentValid;
309: }
310:
311: /** setter for property documentDirty. Method updateDocument() usually setsDocumentDirty to false
312: */
313: public void setDocumentDirty(boolean dirty) {
314: documentDirty = dirty;
315: }
316:
317: /** Getter for property documentDirty.
318: * @return Value of property documentDirty.
319: */
320: public boolean isDocumentDirty() {
321: return documentDirty;
322: }
323:
324: /** Getter for property nodeDirty.
325: * @return Value of property nodeDirty.
326: */
327: public boolean isNodeDirty() {
328: return nodeDirty;
329: }
330:
331: /** Setter for property nodeDirty.
332: * @param dirty New value of property nodeDirty.
333: */
334: public void setNodeDirty(boolean dirty) {
335: nodeDirty = dirty;
336: }
337:
338: org.openide.nodes.CookieSet getCookieSet0() {
339: return getCookieSet();
340: }
341:
342: public static class J2eeErrorHandler implements ErrorHandler {
343:
344: private StrutsConfigDataObject dataObject;
345:
346: public J2eeErrorHandler(StrutsConfigDataObject obj) {
347: dataObject = obj;
348: }
349:
350: public void error(SAXParseException exception)
351: throws SAXException {
352: dataObject.createSAXParseError(exception);
353: throw exception;
354: }
355:
356: public void fatalError(SAXParseException exception)
357: throws SAXException {
358: dataObject.createSAXParseError(exception);
359: throw exception;
360: }
361:
362: public void warning(SAXParseException exception)
363: throws SAXException {
364: dataObject.createSAXParseError(exception);
365: throw exception;
366: }
367: }
368:
369: private void createSAXParseError(SAXParseException error) {
370: this .error = new SAXParseError(error);
371: }
372: }
|