001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.xslt.tmap.model.xsltmap;
020:
021: import java.io.File;
022: import java.io.FileNotFoundException;
023: import java.io.IOException;
024: import java.io.OutputStream;
025: import java.nio.charset.Charset;
026: import javax.xml.parsers.DocumentBuilder;
027: import javax.xml.parsers.DocumentBuilderFactory;
028: import javax.xml.parsers.ParserConfigurationException;
029: import org.netbeans.api.project.Project;
030: import org.netbeans.api.queries.FileEncodingQuery;
031: import org.netbeans.modules.xslt.tmap.util.Util;
032: import org.openide.filesystems.FileLock;
033: import org.openide.filesystems.FileObject;
034: import org.openide.filesystems.FileUtil;
035: import org.openide.xml.XMLUtil;
036: import org.w3c.dom.Attr;
037: import org.w3c.dom.Document;
038: import org.w3c.dom.NamedNodeMap;
039: import org.w3c.dom.Node;
040: import org.w3c.dom.NodeList;
041: import org.xml.sax.SAXException;
042:
043: /**
044: *
045: * @author Vitaly Bychkov
046: * @version 1.0
047: */
048: public class XmlUtil {
049: public static String LINE_SEPARATOR = System
050: .getProperty("line.separator");
051: public static final String UTF8 = "UTF-8"; // NOI18N
052: private static final String XSLT_MAP_TEMPLATE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
053: + LINE_SEPARATOR
054: + "<xsltmap>"
055: + LINE_SEPARATOR
056: + "</xsltmap>" + LINE_SEPARATOR;
057:
058: private XmlUtil() {
059: }
060:
061: public static FileObject createTemplateXsltMapFo(Project project)
062: throws IOException {
063: if (project == null) {
064: throw new IllegalArgumentException(
065: "project shouldn't be null");
066: }
067: FileObject xsltMapFo = Util.getXsltMapFo(project);
068: if (xsltMapFo != null) {
069: return xsltMapFo;
070: }
071:
072: FileObject projectSource = Util.getProjectSource(project);
073: assert projectSource != null;
074:
075: File xsltMapFile = new File(FileUtil.toFile(projectSource)
076: .getPath(), XsltMapConst.XSLTMAP + "."
077: + XsltMapConst.XML);
078: if (xsltMapFile != null) {
079: xsltMapFile.createNewFile();
080: }
081: xsltMapFo = FileUtil.toFileObject(xsltMapFile);
082:
083: Document document = XMLUtil.createDocument("xsltmap", null,
084: null, null);
085: document.setXmlStandalone(true);
086:
087: FileLock fileLock = null;
088: OutputStream outputStream = null;
089: try {
090: fileLock = xsltMapFo.lock();
091: outputStream = xsltMapFo.getOutputStream(fileLock);
092: XMLUtil.write(document, outputStream, XmlUtil.UTF8);
093: } finally {
094: if (outputStream != null) {
095: outputStream.flush();
096: outputStream.close();
097: }
098: if (fileLock != null) {
099: fileLock.releaseLock();
100: }
101: }
102:
103: // xsltMapFo = projectSource.getFileObject(XsltMapConst.XSLTMAP+"."+XsltMapConst.XML);
104: return xsltMapFo;
105: }
106:
107: public static FileObject createNewXmlFo(String path,
108: String fileNameNoExt, String namespaceURI)
109: throws IOException {
110: if (path == null || fileNameNoExt == null) {
111: throw new IllegalArgumentException(
112: "path and fileName shouldn't be null");
113: }
114:
115: File xmlFile = new File(path, fileNameNoExt + ".xml");
116: if (xmlFile != null) {
117: xmlFile.createNewFile();
118: }
119: FileObject xmlFileFo = FileUtil.toFileObject(xmlFile);
120:
121: Document document = XMLUtil.createDocument("transformmap",
122: namespaceURI, null, null);
123: document.setXmlStandalone(true);
124:
125: FileLock fileLock = null;
126: OutputStream outputStream = null;
127: try {
128: fileLock = xmlFileFo.lock();
129: outputStream = xmlFileFo.getOutputStream(fileLock);
130: XMLUtil.write(document, outputStream, FileEncodingQuery
131: .getDefaultEncoding().name());
132: } finally {
133: if (outputStream != null) {
134: outputStream.flush();
135: outputStream.close();
136: }
137: if (fileLock != null) {
138: fileLock.releaseLock();
139: }
140: }
141:
142: // xsltMapFo = projectSource.getFileObject(XsltMapConst.XSLTMAP+"."+XsltMapConst.XML);
143: return xmlFileFo;
144: }
145:
146: /**
147: * This method is used from anttask so it shouldn't use FileObject at all
148: */
149: public static Document getDocument(File file) {
150: return getDocument(file, false);
151: }
152:
153: /**
154: * This method is used from anttask so it shouldn't use FileObject at all
155: */
156: public static Document getDocument(File file,
157: boolean supportNamespace) {
158: // return getDocument(FileUtil.toFileObject(file));
159:
160: DocumentBuilderFactory docFactory = DocumentBuilderFactory
161: .newInstance();
162: docFactory.setValidating(false);
163: docFactory.setNamespaceAware(supportNamespace);
164: DocumentBuilder docBuilder;
165: try {
166: docBuilder = docFactory.newDocumentBuilder();
167: } catch (ParserConfigurationException ex) {
168: ex.printStackTrace();
169: return null;
170: }
171: Document document = null;
172:
173: FileLock fLock = null;
174: try {
175: // TODO a
176: // fLock = fo.lock();
177:
178: document = docBuilder.parse(file);
179: document.setXmlStandalone(true);
180:
181: } catch (FileNotFoundException ex) {
182: ex.printStackTrace();
183: document = null;
184: } catch (SAXException ex) {
185: ex.printStackTrace();
186: document = null;
187: } catch (IOException ex) {
188: ex.printStackTrace();
189: document = null;
190: }/* finally {
191: if (fLock != null) {
192: fLock.releaseLock();
193: }
194: }*/
195:
196: return document;
197: }
198:
199: public static Document getDocument(FileObject fo) {
200: if (fo == null || !fo.canRead()) {
201: return null;
202: }
203:
204: DocumentBuilderFactory docFactory = DocumentBuilderFactory
205: .newInstance();
206: docFactory.setValidating(false);
207: DocumentBuilder docBuilder;
208: try {
209: docBuilder = docFactory.newDocumentBuilder();
210: } catch (ParserConfigurationException ex) {
211: ex.printStackTrace();
212: return null;
213: }
214: Document document = null;
215:
216: FileLock fLock = null;
217: try {
218: // TODO a
219: // fLock = fo.lock();
220: document = docBuilder.parse(fo.getInputStream());
221: document.setXmlStandalone(true);
222:
223: } catch (FileNotFoundException ex) {
224: ex.printStackTrace();
225: document = null;
226: } catch (SAXException ex) {
227: ex.printStackTrace();
228: document = null;
229: } catch (IOException ex) {
230: ex.printStackTrace();
231: document = null;
232: }/* finally {
233: if (fLock != null) {
234: fLock.releaseLock();
235: }
236: }*/
237:
238: return document;
239: }
240:
241: public static Node getElementByTagName(NodeList children,
242: String tagName) {
243: if (children == null || tagName == null) {
244: return null;
245: }
246: Node requredNode = null;
247: int length = children.getLength();
248: for (int i = 0; i < children.getLength(); i++) {
249: Node tmpNode = children.item(i);
250: if (tagName.equals(tmpNode.getNodeName())) {
251: requredNode = tmpNode;
252: break;
253: }
254: }
255: return requredNode;
256: }
257:
258: public static Node getElementByTagName(Document document,
259: String tagName) {
260: if (document == null || tagName == null) {
261: return null;
262: }
263:
264: NodeList nodes = document.getElementsByTagName(tagName);
265: if (nodes == null || nodes.getLength() < 1) {
266: return null;
267: }
268: return nodes.item(0);
269: }
270:
271: public static String getAttrValue(NamedNodeMap attrs,
272: String attrName) {
273: if (attrs == null || attrName == null) {
274: return null;
275: }
276: String attrValue = null;
277:
278: Node attrNode = attrs.getNamedItem(attrName);
279: if (attrNode == null
280: || Node.ATTRIBUTE_NODE != attrNode.getNodeType()) {
281: return null;
282: }
283:
284: attrValue = attrNode.getNodeValue();
285: return attrValue;
286: }
287:
288: public static Attr createAttr(String attrName, String attrValue,
289: Document document) {
290: if (attrName == null || attrName.length() == 0
291: || attrValue == null || document == null) {
292: return null;
293: }
294:
295: Attr attribute = document.createAttribute(attrName);
296: attribute.setValue(attrValue);
297: return attribute;
298: }
299:
300: }
|