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.xml.retriever.catalog.impl;
043:
044: import java.io.BufferedInputStream;
045: import java.io.ByteArrayInputStream;
046: import java.io.File;
047: import java.io.IOException;
048: import java.io.InputStream;
049: import java.net.URI;
050: import java.net.URISyntaxException;
051: import javax.swing.text.BadLocationException;
052: import javax.swing.text.StyledDocument;
053: import javax.xml.parsers.DocumentBuilderFactory;
054: import javax.xml.parsers.ParserConfigurationException;
055: import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModelFactory;
056: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
057: import org.netbeans.modules.xml.xam.locator.CatalogModel;
058: import org.netbeans.modules.xml.xam.ModelSource;
059: import org.openide.cookies.EditorCookie;
060: import org.openide.filesystems.FileObject;
061: import org.openide.filesystems.FileUtil;
062: import org.openide.loaders.DataObject;
063: import org.openide.loaders.DataObjectNotFoundException;
064: import org.w3c.dom.DOMImplementation;
065: import org.w3c.dom.ls.DOMImplementationLS;
066: import org.w3c.dom.ls.LSInput;
067: import org.w3c.dom.ls.LSResourceResolver;
068:
069: /**
070: *
071: * @author girix
072: */
073: public class LSResourceResolverImpl implements LSResourceResolver {
074:
075: /** Creates a new instance of LSResourceResolverImpl */
076: public LSResourceResolverImpl() {
077: }
078:
079: public LSInput resolveResource(String type, String namespaceURI,
080: String publicId, String systemId, String baseURIStr) {
081: //check for sanity of the systemID
082: if ((systemId == null) || (systemId.trim().length() <= 0))
083: return null;
084: URI systemIdURI = null;
085: try {
086: systemIdURI = new URI(systemId);
087: } catch (URISyntaxException ex) {
088: return null;
089: }
090:
091: FileObject baseFO = null;
092: //get the resolver object
093: CatalogModel depRez = null;
094: try {
095: baseFO = getFileObject(baseURIStr);
096: depRez = getResolver(baseFO);
097: } catch (CatalogModelException ex) {
098: return null;
099: } catch (IOException ex) {
100: return null;
101: }
102: if (depRez == null)
103: return null;
104: ModelSource baseMS = null;
105: try {
106: baseMS = org.netbeans.modules.xml.retriever.catalog.Utilities
107: .createModelSource(baseFO, false);
108: } catch (CatalogModelException ex) {
109: }
110: //get the model source from it
111: ModelSource resultMS = null;
112: try {
113: resultMS = depRez.getModelSource(systemIdURI, baseMS);
114: } catch (CatalogModelException ex) {
115: return null;
116: }
117: if (resultMS == null)
118: return null;
119:
120: //get file object
121: FileObject resultFob = (FileObject) resultMS.getLookup()
122: .lookup(FileObject.class);
123: if (resultFob == null)
124: return null;
125:
126: //get file
127: File resultFile = FileUtil.toFile(resultFob);
128: if (resultFile == null)
129: return null;
130:
131: //get URI out of file
132: URI resultURI = resultFile.toURI();
133:
134: //create LSInput object
135: DOMImplementation domImpl = null;
136: try {
137:
138: domImpl = DocumentBuilderFactory.newInstance()
139: .newDocumentBuilder().getDOMImplementation();
140: } catch (ParserConfigurationException ex) {
141: return null;
142: }
143: DOMImplementationLS dols = (DOMImplementationLS) domImpl
144: .getFeature("LS", "3.0");
145: LSInput lsi = dols.createLSInput();
146: InputStream is = getFileStreamFromDocument(resultFile);
147: if (is != null)
148: lsi.setByteStream(is);
149: lsi.setSystemId(resultURI.toString());
150: return lsi;
151: }
152:
153: private FileObject getFileObject(String baseURIStr)
154: throws IOException {
155: if (baseURIStr == null)
156: return null;
157: URI baseURI = null;
158: try {
159: baseURI = new URI(baseURIStr);
160: } catch (URISyntaxException ex) {
161: IOException ioe = new IOException();
162: ioe.initCause(ex);
163: throw ioe;
164: }
165: if (baseURI.isAbsolute()) {
166: if (baseURI.getScheme().equalsIgnoreCase("file")) { //NOI18N
167: File baseFile = null;
168: try {
169: baseFile = new File(baseURI);
170: } catch (Exception e) {
171: IOException ioe = new IOException();
172: ioe.initCause(e);
173: throw ioe;
174: }
175: baseFile = FileUtil.normalizeFile(baseFile);
176: FileObject baseFileObject = null;
177: try {
178: baseFileObject = FileUtil.toFileObject(baseFile);
179: } catch (Exception e) {
180: IOException ioe = new IOException();
181: ioe.initCause(e);
182: throw ioe;
183: }
184: return baseFileObject;
185: }
186: }
187: return null;
188: }
189:
190: private CatalogModel getResolver(FileObject baseFileObject)
191: throws CatalogModelException {
192: if (baseFileObject != null)
193: return CatalogWriteModelFactory.getInstance()
194: .getCatalogWriteModelForProject(baseFileObject);
195: return null;
196: }
197:
198: private InputStream getFileStreamFromDocument(File resultFile) {
199: FileObject fo = FileUtil.toFileObject(FileUtil
200: .normalizeFile(resultFile));
201: if (fo != null) {
202: DataObject dobj = null;
203: try {
204: dobj = DataObject.find(fo);
205: } catch (DataObjectNotFoundException ex) {
206: return null;
207: }
208: if (dobj.isModified()) {
209: EditorCookie this DocumentEditorCookie = (EditorCookie) dobj
210: .getCookie(EditorCookie.class);
211: if (this DocumentEditorCookie == null)
212: return null;
213: StyledDocument sd = null;
214: try {
215: sd = this DocumentEditorCookie.openDocument();
216: } catch (IOException ex) {
217: return null;
218: }
219: if (sd == null)
220: return null;
221: String docContent = null;
222: try {
223: docContent = sd.getText(0, sd.getLength());
224: } catch (BadLocationException ex) {
225: return null;
226: }
227: if (docContent == null)
228: return null;
229: BufferedInputStream bis = new BufferedInputStream(
230: new ByteArrayInputStream(docContent.getBytes()));
231: return bis;
232: } else {
233: //return null so that the validator will use normal file path to access doc
234: return null;
235: }
236: }
237: return null;
238: }
239:
240: }
|