mkristofic@1583
|
1 |
/*
|
mkristofic@1583
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
mkristofic@1583
|
3 |
*
|
mkristofic@1583
|
4 |
* Copyright 2009-2017 Oracle and/or its affiliates. All rights reserved.
|
mkristofic@1583
|
5 |
*
|
mkristofic@1583
|
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
|
mkristofic@1583
|
7 |
* Other names may be trademarks of their respective owners.
|
mkristofic@1583
|
8 |
*
|
mkristofic@1583
|
9 |
* The contents of this file are subject to the terms of either the GNU
|
mkristofic@1583
|
10 |
* General Public License Version 2 only ("GPL") or the Common
|
mkristofic@1583
|
11 |
* Development and Distribution License("CDDL") (collectively, the
|
mkristofic@1583
|
12 |
* "License"). You may not use this file except in compliance with the
|
mkristofic@1583
|
13 |
* License. You can obtain a copy of the License at
|
mkristofic@1583
|
14 |
* http://www.netbeans.org/cddl-gplv2.html
|
mkristofic@1583
|
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
mkristofic@1583
|
16 |
* specific language governing permissions and limitations under the
|
mkristofic@1583
|
17 |
* License. When distributing the software, include this License Header
|
mkristofic@1583
|
18 |
* Notice in each file and include the License file at
|
mkristofic@1583
|
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
|
mkristofic@1583
|
20 |
* particular file as subject to the "Classpath" exception as provided
|
mkristofic@1583
|
21 |
* by Oracle in the GPL Version 2 section of the License file that
|
mkristofic@1583
|
22 |
* accompanied this code. If applicable, add the following below the
|
mkristofic@1583
|
23 |
* License Header, with the fields enclosed by brackets [] replaced by
|
mkristofic@1583
|
24 |
* your own identifying information:
|
mkristofic@1583
|
25 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
mkristofic@1583
|
26 |
*
|
mkristofic@1583
|
27 |
* Contributor(s):
|
mkristofic@1583
|
28 |
*
|
mkristofic@1583
|
29 |
* The Original Software is NetBeans. The Initial Developer of the Original
|
mkristofic@1583
|
30 |
* Software is Sun Microsystems, Inc. Portions Copyright 2009-2010 Sun
|
mkristofic@1583
|
31 |
* Microsystems, Inc. All Rights Reserved.
|
mkristofic@1583
|
32 |
*
|
mkristofic@1583
|
33 |
* If you wish your version of this file to be governed by only the CDDL
|
mkristofic@1583
|
34 |
* or only the GPL Version 2, indicate your decision by adding
|
mkristofic@1583
|
35 |
* "[Contributor] elects to include this software in this distribution
|
mkristofic@1583
|
36 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
mkristofic@1583
|
37 |
* single choice of license, a recipient has the option to distribute
|
mkristofic@1583
|
38 |
* your version of this file under either the CDDL, the GPL Version 2 or
|
mkristofic@1583
|
39 |
* to extend the choice of license to its licensees as provided above.
|
mkristofic@1583
|
40 |
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
mkristofic@1583
|
41 |
* Version 2 license, then the option applies only if the new code is
|
mkristofic@1583
|
42 |
* made subject to such option by the copyright holder.
|
mkristofic@1583
|
43 |
*/
|
sonali@667
|
44 |
/*
|
sonali@667
|
45 |
* To change this template, choose Tools | Templates
|
sonali@667
|
46 |
* and open the template in the editor.
|
sonali@667
|
47 |
*/
|
sonali@667
|
48 |
package org.netbeans.modules.xml.schema.wizard;
|
sonali@667
|
49 |
|
sonali@667
|
50 |
import java.awt.Component;
|
sonali@667
|
51 |
import java.awt.Dialog;
|
sonali@667
|
52 |
import java.io.BufferedOutputStream;
|
sonali@667
|
53 |
import java.io.File;
|
sonali@667
|
54 |
import java.io.IOException;
|
sonali@667
|
55 |
import java.io.OutputStream;
|
sonali@667
|
56 |
import java.io.OutputStreamWriter;
|
sonali@667
|
57 |
import java.io.Writer;
|
sonali@667
|
58 |
import java.net.URI;
|
sonali@667
|
59 |
import java.net.URISyntaxException;
|
sonali@667
|
60 |
import java.net.URL;
|
sonali@667
|
61 |
import java.text.DateFormat;
|
sonali@667
|
62 |
import java.text.MessageFormat;
|
sonali@667
|
63 |
import java.util.Date;
|
sonali@667
|
64 |
import java.util.HashSet;
|
sonali@971
|
65 |
import java.util.Map;
|
sonali@667
|
66 |
import java.util.NoSuchElementException;
|
sonali@667
|
67 |
import java.util.Set;
|
sonali@667
|
68 |
import javax.swing.JComponent;
|
sonali@667
|
69 |
import javax.swing.event.ChangeListener;
|
sonali@971
|
70 |
import javax.xml.parsers.DocumentBuilder;
|
sonali@971
|
71 |
import javax.xml.parsers.DocumentBuilderFactory;
|
sonali@971
|
72 |
import javax.xml.transform.OutputKeys;
|
sonali@971
|
73 |
import javax.xml.transform.Result;
|
sonali@971
|
74 |
import javax.xml.transform.Transformer;
|
sonali@971
|
75 |
import javax.xml.transform.TransformerFactory;
|
sonali@971
|
76 |
import javax.xml.transform.dom.DOMSource;
|
sonali@971
|
77 |
import javax.xml.transform.stream.StreamResult;
|
sonali@667
|
78 |
import org.netbeans.api.project.FileOwnerQuery;
|
sonali@667
|
79 |
import org.netbeans.api.project.Project;
|
sonali@1021
|
80 |
import org.netbeans.editor.BaseDocument;
|
sonali@667
|
81 |
import org.netbeans.modules.xml.api.EncodingUtil;
|
samaresh@1138
|
82 |
import org.netbeans.modules.xml.lib.GuiUtil;
|
sonali@667
|
83 |
import org.netbeans.modules.xml.schema.SchemaDataObject;
|
sonali@1021
|
84 |
import org.netbeans.modules.xml.text.TextEditorSupport;
|
sonali@667
|
85 |
import org.netbeans.modules.xml.wizard.AbstractPanel;
|
sonali@667
|
86 |
import org.netbeans.modules.xml.wizard.DocumentModel;
|
samaresh@1138
|
87 |
import org.netbeans.modules.xml.wizard.SchemaParser;
|
sonali@667
|
88 |
import org.netbeans.modules.xml.wizard.XMLContentPanel;
|
sonali@667
|
89 |
import org.netbeans.modules.xml.wizard.XMLGeneratorVisitor;
|
sonali@667
|
90 |
import org.openide.DialogDisplayer;
|
sonali@667
|
91 |
import org.openide.WizardDescriptor;
|
sonali@1021
|
92 |
import org.openide.cookies.EditCookie;
|
sonali@1021
|
93 |
import org.openide.cookies.EditorCookie;
|
sonali@667
|
94 |
import org.openide.filesystems.FileObject;
|
sonali@667
|
95 |
import org.openide.filesystems.FileLock;
|
sonali@667
|
96 |
import org.openide.filesystems.FileSystem;
|
sonali@667
|
97 |
import org.openide.filesystems.FileUtil;
|
sonali@667
|
98 |
import org.openide.loaders.DataObject;
|
sonali@667
|
99 |
import org.openide.util.NbBundle;
|
sonali@971
|
100 |
import org.w3c.dom.Attr;
|
sonali@971
|
101 |
import org.w3c.dom.Document;
|
sonali@971
|
102 |
import org.w3c.dom.NamedNodeMap;
|
sonali@667
|
103 |
|
sonali@667
|
104 |
public final class SampleXMLGeneratorWizardIterator implements WizardDescriptor.InstantiatingIterator {
|
sonali@667
|
105 |
|
sonali@667
|
106 |
private int index;
|
sonali@667
|
107 |
private WizardDescriptor.Panel[] panels;
|
sonali@667
|
108 |
private Project srcProject;
|
sonali@667
|
109 |
private FileObject schemaFileObject;
|
sonali@667
|
110 |
private DocumentModel model;
|
sonali@667
|
111 |
private XMLContentPanel xmlPanel;
|
sonali@667
|
112 |
private static String PREFIX = "ns0";
|
sonali@667
|
113 |
private static final String XML_EXT = "xml";
|
sonali@667
|
114 |
|
sonali@667
|
115 |
public SampleXMLGeneratorWizardIterator(SchemaDataObject dobj) {
|
sonali@667
|
116 |
schemaFileObject = dobj.getPrimaryFile();
|
sonali@667
|
117 |
this.srcProject = FileOwnerQuery.getOwner(schemaFileObject);
|
sonali@667
|
118 |
}
|
sonali@667
|
119 |
|
sonali@667
|
120 |
public void show() {
|
sonali@667
|
121 |
WizardDescriptor wizardDescriptor = new WizardDescriptor(getPanels());
|
sonali@667
|
122 |
initialize(wizardDescriptor);
|
sonali@667
|
123 |
wizardDescriptor.setTitleFormat(new MessageFormat("{0}"));
|
samaresh@858
|
124 |
wizardDescriptor.setTitle(NbBundle.getMessage(SampleXMLGeneratorWizardIterator.class,"TITLE_XML_WIZARD"));
|
sonali@667
|
125 |
Dialog dialog = DialogDisplayer.getDefault().createDialog(wizardDescriptor);
|
samaresh@858
|
126 |
dialog.getAccessibleContext().setAccessibleName(
|
samaresh@858
|
127 |
NbBundle.getMessage(SampleXMLGeneratorWizardIterator.class,"TITLE_XML_WIZARD"));
|
samaresh@858
|
128 |
dialog.getAccessibleContext().setAccessibleDescription(
|
samaresh@858
|
129 |
NbBundle.getMessage(SampleXMLGeneratorWizardIterator.class,"TITLE_XML_WIZARD"));
|
sonali@667
|
130 |
dialog.setVisible(true);
|
sonali@667
|
131 |
dialog.toFront();
|
sonali@667
|
132 |
boolean cancelled = wizardDescriptor.getValue() != WizardDescriptor.FINISH_OPTION;
|
sonali@667
|
133 |
|
sonali@667
|
134 |
if (!cancelled) {
|
sonali@667
|
135 |
try {
|
sonali@667
|
136 |
instantiate();
|
sonali@667
|
137 |
}catch (Exception e){
|
sonali@667
|
138 |
e.printStackTrace();
|
sonali@667
|
139 |
}
|
sonali@667
|
140 |
}
|
sonali@667
|
141 |
|
sonali@667
|
142 |
}
|
sonali@667
|
143 |
|
sonali@667
|
144 |
/**
|
sonali@667
|
145 |
* Initialize panels representing individual wizard's steps and sets
|
sonali@667
|
146 |
* various properties for them influencing wizard appearance.
|
sonali@667
|
147 |
*/
|
sonali@667
|
148 |
private WizardDescriptor.Panel[] getPanels() {
|
sonali@735
|
149 |
|
sonali@667
|
150 |
xmlPanel = new XMLContentPanel(true);
|
sonali@667
|
151 |
|
sonali@667
|
152 |
if (panels == null) {
|
sonali@667
|
153 |
panels = new WizardDescriptor.Panel[]{
|
sonali@667
|
154 |
//Templates.createSimpleTargetChooser(srcProject, folders)
|
sonali@667
|
155 |
new AbstractPanel.WizardStep(xmlPanel)
|
sonali@667
|
156 |
};
|
sonali@667
|
157 |
String[] steps = new String[panels.length];
|
sonali@667
|
158 |
for (int i = 0; i < panels.length; i++) {
|
sonali@667
|
159 |
Component c = panels[i].getComponent();
|
sonali@667
|
160 |
if (steps[i] == null) {
|
sonali@667
|
161 |
// Default step name to component name of panel. Mainly
|
sonali@667
|
162 |
// useful for getting the name of the target chooser to
|
sonali@667
|
163 |
// appear in the list of steps.
|
sonali@667
|
164 |
steps[i] = c.getName();
|
sonali@667
|
165 |
}
|
sonali@667
|
166 |
if (c instanceof JComponent) { // assume Swing components
|
sonali@667
|
167 |
JComponent jc = (JComponent) c;
|
sonali@667
|
168 |
// Sets step number of a component
|
mkubec@1002
|
169 |
jc.putClientProperty(WizardDescriptor.PROP_CONTENT_SELECTED_INDEX, new Integer(i));
|
sonali@667
|
170 |
// Sets steps names for a panel
|
mkubec@1002
|
171 |
jc.putClientProperty(WizardDescriptor.PROP_CONTENT_DATA, steps);
|
sonali@667
|
172 |
// Turn on subtitle creation on each step
|
mkubec@1002
|
173 |
jc.putClientProperty(WizardDescriptor.PROP_AUTO_WIZARD_STYLE, Boolean.TRUE);
|
sonali@667
|
174 |
// Show steps on the left side with the image on the background
|
mkubec@1002
|
175 |
jc.putClientProperty(WizardDescriptor.PROP_CONTENT_DISPLAYED, Boolean.TRUE);
|
sonali@667
|
176 |
// Turn on numbering of all steps
|
mkubec@1002
|
177 |
jc.putClientProperty(WizardDescriptor.PROP_CONTENT_NUMBERED, Boolean.TRUE);
|
sonali@667
|
178 |
}
|
sonali@667
|
179 |
}
|
sonali@667
|
180 |
}
|
sonali@667
|
181 |
return panels;
|
sonali@667
|
182 |
}
|
sonali@667
|
183 |
|
sonali@667
|
184 |
public Set instantiate() throws IOException {
|
sonali@667
|
185 |
final String extension = XML_EXT;
|
sonali@667
|
186 |
|
sonali@667
|
187 |
String targetName = schemaFileObject.getName();
|
sonali@667
|
188 |
if (targetName == null || "null".equals(targetName)) { // NOI18N
|
sonali@667
|
189 |
targetName = "XMLDocument"; // NOI18N
|
sonali@667
|
190 |
}
|
sonali@667
|
191 |
|
sonali@667
|
192 |
final FileObject targetFolder = schemaFileObject.getParent();
|
sonali@667
|
193 |
String uniqueTargetName = targetName;
|
sonali@979
|
194 |
int i = 1;
|
sonali@667
|
195 |
while (targetFolder.getFileObject(uniqueTargetName, extension) != null) {
|
sonali@667
|
196 |
uniqueTargetName = targetName + i;
|
sonali@667
|
197 |
i++;
|
sonali@667
|
198 |
}
|
sonali@667
|
199 |
|
sonali@667
|
200 |
final String name = uniqueTargetName;
|
sonali@667
|
201 |
|
sonali@667
|
202 |
// in atomic action create data object and return it
|
sonali@667
|
203 |
|
sonali@667
|
204 |
FileSystem filesystem = targetFolder.getFileSystem();
|
sonali@667
|
205 |
final FileObject[] fileObject = new FileObject[1];
|
sonali@1021
|
206 |
String encoding = EncodingUtil.getProjectEncoding(targetFolder);
|
sonali@1021
|
207 |
if (!EncodingUtil.isValidEncoding(encoding)) {
|
sonali@1021
|
208 |
encoding = "UTF-8"; //NOI18N
|
sonali@1021
|
209 |
}
|
sonali@1021
|
210 |
String nameExt = name + "." + extension;
|
sonali@667
|
211 |
FileSystem.AtomicAction fsAction = new FileSystem.AtomicAction() {
|
sonali@667
|
212 |
public void run() throws IOException {
|
sonali@1021
|
213 |
|
sonali@667
|
214 |
FileObject fo = targetFolder.createData(name, extension);
|
sonali@1021
|
215 |
fileObject[0] = fo;
|
sonali@667
|
216 |
}
|
sonali@667
|
217 |
};
|
sonali@1021
|
218 |
|
sonali@1021
|
219 |
filesystem.runAtomicAction(fsAction);
|
sonali@1021
|
220 |
StringBuffer sb = new StringBuffer();
|
sonali@1021
|
221 |
//write the comment
|
sonali@1021
|
222 |
writeXMLComment(sb, nameExt, encoding);
|
sonali@1021
|
223 |
//write the body
|
sonali@1021
|
224 |
writeXMLFile(sb);
|
sonali@667
|
225 |
|
sonali@1021
|
226 |
FileLock lock = null;
|
sonali@1021
|
227 |
try {
|
sonali@1021
|
228 |
lock = fileObject[0].lock();
|
sonali@1021
|
229 |
OutputStream out = fileObject[0].getOutputStream(lock);
|
sonali@1021
|
230 |
out = new BufferedOutputStream(out, 999);
|
sonali@1021
|
231 |
Writer writer = new OutputStreamWriter(out, encoding);
|
sonali@1021
|
232 |
writer.write(sb.toString());
|
sonali@1021
|
233 |
writer.flush();
|
sonali@1021
|
234 |
writer.close();
|
sonali@1021
|
235 |
lock.releaseLock();
|
sonali@1021
|
236 |
lock = null;
|
sonali@667
|
237 |
|
sonali@1021
|
238 |
} finally {
|
sonali@1021
|
239 |
if (lock != null) {
|
sonali@1021
|
240 |
lock.releaseLock();
|
sonali@1021
|
241 |
}
|
sonali@1021
|
242 |
}
|
sonali@667
|
243 |
// perform default action and return
|
sonali@1021
|
244 |
|
sonali@667
|
245 |
Set set = new HashSet(1);
|
sonali@667
|
246 |
DataObject createdObject = DataObject.find(fileObject[0]);
|
samaresh@1138
|
247 |
GuiUtil.performDefaultAction(createdObject);
|
sonali@667
|
248 |
set.add(createdObject);
|
sonali@667
|
249 |
|
sonali@1042
|
250 |
formatXML(fileObject[0]);
|
sonali@667
|
251 |
return set;
|
sonali@667
|
252 |
}
|
sonali@667
|
253 |
|
sonali@667
|
254 |
public void initialize(WizardDescriptor wiz) {
|
sonali@667
|
255 |
index=0;
|
sonali@667
|
256 |
URL targetFolderURL = null;
|
sonali@667
|
257 |
try {
|
sonali@667
|
258 |
FileObject folder = schemaFileObject.getParent();
|
sonali@667
|
259 |
targetFolderURL = folder.getURL();
|
sonali@667
|
260 |
//#25604 workaround
|
sonali@667
|
261 |
if (targetFolderURL.toExternalForm().endsWith("/") == false) {
|
sonali@667
|
262 |
targetFolderURL = new URL(targetFolderURL.toExternalForm() + "/");
|
sonali@667
|
263 |
}
|
sonali@667
|
264 |
} catch (IOException ignore) {
|
sonali@667
|
265 |
}
|
sonali@667
|
266 |
|
sonali@667
|
267 |
model = new DocumentModel(targetFolderURL);
|
sonali@667
|
268 |
File file = FileUtil.toFile(schemaFileObject);
|
sonali@667
|
269 |
String uri = file.getPath();
|
sonali@667
|
270 |
if (uri != null) {
|
sonali@667
|
271 |
try {
|
sonali@667
|
272 |
// escape the non-ASCII characters
|
sonali@667
|
273 |
uri = new URI(uri).toASCIIString();
|
sonali@667
|
274 |
} catch (URISyntaxException e) {
|
sonali@667
|
275 |
// the specified uri is not valid, it is too late to fix it now
|
sonali@667
|
276 |
}
|
sonali@667
|
277 |
}
|
sonali@667
|
278 |
model.setPrimarySchema(uri);
|
sonali@667
|
279 |
|
sonali@667
|
280 |
xmlPanel.setObject(model);
|
sonali@667
|
281 |
model.setPrefix(PREFIX);
|
samaresh@1138
|
282 |
String ns = SchemaParser.getNamespace(schemaFileObject);
|
sonali@667
|
283 |
model.setNamespace(ns);
|
sonali@667
|
284 |
}
|
sonali@667
|
285 |
|
sonali@667
|
286 |
public void uninitialize(WizardDescriptor wizard) {
|
sonali@667
|
287 |
panels = null;
|
sonali@667
|
288 |
}
|
sonali@667
|
289 |
|
sonali@667
|
290 |
public WizardDescriptor.Panel current() {
|
samaresh@858
|
291 |
return panels[index];
|
sonali@667
|
292 |
}
|
sonali@667
|
293 |
|
sonali@667
|
294 |
public String name() {
|
samaresh@858
|
295 |
return NbBundle.getMessage(SchemaWizardIterator.class, "TITLE_x_of_y",
|
samaresh@858
|
296 |
Integer.valueOf(index + 1), Integer.valueOf(panels.length));
|
sonali@667
|
297 |
}
|
sonali@667
|
298 |
|
sonali@667
|
299 |
public boolean hasNext() {
|
sonali@667
|
300 |
return index < getPanels().length - 1;
|
sonali@667
|
301 |
}
|
sonali@667
|
302 |
|
sonali@667
|
303 |
public boolean hasPrevious() {
|
sonali@667
|
304 |
return index > 0;
|
sonali@667
|
305 |
}
|
sonali@667
|
306 |
|
sonali@667
|
307 |
public void nextPanel() {
|
sonali@667
|
308 |
if (!hasNext()) {
|
sonali@667
|
309 |
throw new NoSuchElementException();
|
sonali@667
|
310 |
}
|
sonali@667
|
311 |
index++;
|
sonali@667
|
312 |
}
|
sonali@667
|
313 |
|
sonali@667
|
314 |
public void previousPanel() {
|
sonali@667
|
315 |
if (!hasPrevious()) {
|
sonali@667
|
316 |
throw new NoSuchElementException();
|
sonali@667
|
317 |
}
|
sonali@667
|
318 |
index--;
|
sonali@667
|
319 |
}
|
sonali@667
|
320 |
|
sonali@667
|
321 |
// If nothing unusual changes in the middle of the wizard, simply:
|
sonali@667
|
322 |
public void addChangeListener(ChangeListener l) {
|
sonali@667
|
323 |
}
|
sonali@667
|
324 |
|
sonali@667
|
325 |
public void removeChangeListener(ChangeListener l) {
|
sonali@667
|
326 |
}
|
sonali@667
|
327 |
|
sonali@667
|
328 |
|
sonali@1021
|
329 |
private void generateXMLBody(DocumentModel model, String root, StringBuffer writer){
|
sonali@667
|
330 |
XMLGeneratorVisitor visitor = new XMLGeneratorVisitor(model.getPrimarySchema(), model.getXMLContentAttributes(), writer);
|
sonali@667
|
331 |
visitor.generateXML(root);
|
sonali@667
|
332 |
|
sonali@667
|
333 |
}
|
sonali@971
|
334 |
|
sonali@971
|
335 |
private void modifyRootElementAttrs(FileObject fobj) {
|
sonali@971
|
336 |
try {
|
sonali@971
|
337 |
File file = new File(fobj.getPath());
|
sonali@971
|
338 |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
sonali@971
|
339 |
DocumentBuilder db = dbf.newDocumentBuilder();
|
sonali@971
|
340 |
Document doc = db.parse(file);
|
sonali@971
|
341 |
doc.getDocumentElement().normalize();
|
sonali@971
|
342 |
|
sonali@971
|
343 |
NamedNodeMap rootAttributes = doc.getDocumentElement().getAttributes();
|
sonali@971
|
344 |
Map<String, String> nsAttrs = model.getXMLContentAttributes().getNamespaceToPrefixMap();
|
sonali@971
|
345 |
|
sonali@971
|
346 |
if(nsAttrs == null || nsAttrs.size() == 0)
|
sonali@971
|
347 |
return;
|
sonali@971
|
348 |
for(String ns:nsAttrs.keySet()) {
|
sonali@971
|
349 |
Attr galaxy = doc.createAttribute("xmlns:" + nsAttrs.get(ns));
|
sonali@971
|
350 |
galaxy.setValue(ns);
|
sonali@971
|
351 |
rootAttributes.setNamedItem(galaxy);
|
sonali@971
|
352 |
}
|
sonali@971
|
353 |
|
sonali@971
|
354 |
//write to oputput file
|
sonali@971
|
355 |
Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
sonali@971
|
356 |
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
sonali@971
|
357 |
|
sonali@971
|
358 |
DOMSource source = new DOMSource(doc);
|
sonali@971
|
359 |
Result result = new StreamResult(file);
|
sonali@971
|
360 |
transformer.transform(source, result);
|
sonali@971
|
361 |
|
sonali@971
|
362 |
} catch(Exception e) {
|
sonali@971
|
363 |
|
sonali@971
|
364 |
}
|
sonali@971
|
365 |
}
|
sonali@1021
|
366 |
|
sonali@1021
|
367 |
private void writeXMLComment(StringBuffer writer, String filename, String encoding) throws IOException {
|
sonali@1021
|
368 |
writer.append("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n"); // NOI18N
|
sonali@1021
|
369 |
writer.append("\n"); // NOI18N
|
sonali@1021
|
370 |
// comment
|
sonali@1021
|
371 |
Date now = new Date();
|
sonali@1021
|
372 |
String currentDate = DateFormat.getDateInstance(DateFormat.LONG).format(now);
|
sonali@1021
|
373 |
String currentTime = DateFormat.getTimeInstance(DateFormat.SHORT).format(now);
|
sonali@1021
|
374 |
String userName = System.getProperty("user.name");
|
sonali@1021
|
375 |
writer.append("<!--\n"); // NOI18N
|
sonali@1021
|
376 |
writer.append(" Document : " + filename + "\n"); // NOI18N
|
sonali@1021
|
377 |
writer.append(" Created on : " + currentDate + ", " + currentTime + "\n"); // NOI18N
|
sonali@1021
|
378 |
writer.append(" Author : " + userName + "\n"); // NOI18N
|
sonali@1021
|
379 |
writer.append(" Description:\n"); // NOI18N
|
sonali@1021
|
380 |
writer.append(" Purpose of the document follows.\n"); // NOI18N
|
sonali@1021
|
381 |
writer.append("-->\n"); // NOI18N
|
sonali@1021
|
382 |
writer.append("\n");
|
sonali@1021
|
383 |
}
|
sonali@1021
|
384 |
|
sonali@1021
|
385 |
|
sonali@1021
|
386 |
|
sonali@1021
|
387 |
private void formatXML(FileObject fobj){
|
sonali@1021
|
388 |
try {
|
sonali@1021
|
389 |
DataObject dobj = DataObject.find(fobj);
|
sonali@1021
|
390 |
EditorCookie ec = dobj.getCookie(EditorCookie.class);
|
sonali@1021
|
391 |
if (ec == null) {
|
sonali@1021
|
392 |
return;
|
sonali@1021
|
393 |
}
|
sonali@1021
|
394 |
BaseDocument doc = (BaseDocument) ec.getDocument();
|
sonali@1042
|
395 |
org.netbeans.modules.xml.text.api.XMLFormatUtil.reformat(doc, 0, doc.getLength());
|
sonali@1021
|
396 |
EditCookie cookie = dobj.getCookie(EditCookie.class);
|
sonali@1021
|
397 |
if (cookie instanceof TextEditorSupport) {
|
sonali@1021
|
398 |
if (cookie != null) {
|
sonali@1021
|
399 |
((TextEditorSupport) cookie).saveDocument();
|
sonali@1021
|
400 |
}
|
sonali@1021
|
401 |
}
|
sonali@1021
|
402 |
|
sonali@1021
|
403 |
} catch (Exception e) {
|
sonali@1021
|
404 |
//if exception , then the file will be informatted
|
sonali@1021
|
405 |
}
|
sonali@1021
|
406 |
|
sonali@1021
|
407 |
|
sonali@1021
|
408 |
}
|
sonali@1021
|
409 |
|
sonali@1021
|
410 |
private void modifyRootElementAttrs(StringBuffer xmlBuffer) {
|
sonali@1021
|
411 |
Map<String, String> nsAttrs = model.getXMLContentAttributes().getNamespaceToPrefixMap();
|
sonali@1021
|
412 |
|
sonali@1021
|
413 |
if (nsAttrs == null || nsAttrs.size() == 0) {
|
sonali@1021
|
414 |
return;
|
sonali@1021
|
415 |
}
|
sonali@1021
|
416 |
int firstOccur = xmlBuffer.indexOf("xmlns");
|
sonali@1021
|
417 |
int insertLoc = xmlBuffer.indexOf("xmlns", firstOccur + 1);
|
sonali@1021
|
418 |
|
sonali@1021
|
419 |
StringBuffer sb = new StringBuffer();
|
sonali@1021
|
420 |
for (String ns : nsAttrs.keySet()) {
|
sonali@1021
|
421 |
String xmlnsString = "xmlns:" + nsAttrs.get(ns) + "='" + ns + "'";
|
sonali@1021
|
422 |
if (xmlBuffer.indexOf(xmlnsString) == -1) {
|
sonali@1021
|
423 |
xmlBuffer.insert(insertLoc, xmlnsString + "\n ");
|
sonali@1021
|
424 |
}
|
sonali@1021
|
425 |
}
|
sonali@1021
|
426 |
xmlBuffer.insert(insertLoc, sb.toString());
|
sonali@1021
|
427 |
|
sonali@1021
|
428 |
}
|
sonali@1021
|
429 |
|
sonali@1021
|
430 |
|
sonali@1021
|
431 |
private void writeXMLFile(StringBuffer writer) throws IOException {
|
sonali@1021
|
432 |
String root = model.getRoot();
|
sonali@1021
|
433 |
if (root == null) {
|
sonali@1021
|
434 |
root = "root";
|
sonali@1021
|
435 |
}
|
sonali@1021
|
436 |
String prefix = model.getPrefix();
|
sonali@1021
|
437 |
String namespace = model.getNamespace();
|
sonali@1021
|
438 |
if (prefix == null || "".equals(prefix)) {
|
sonali@1021
|
439 |
writer.append("<" + root + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'\n");
|
sonali@1021
|
440 |
} else {
|
sonali@1021
|
441 |
writer.append("<" + prefix + ":" + root + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'\n");
|
sonali@1021
|
442 |
}
|
sonali@1021
|
443 |
|
sonali@1021
|
444 |
if (prefix == null || "".equals(prefix)) {
|
sonali@1021
|
445 |
writer.append(" xmlns='" + namespace + "'\n");
|
sonali@1021
|
446 |
} else {
|
sonali@1021
|
447 |
writer.append(" xmlns:" + prefix + "='" + namespace + "'\n");
|
sonali@1021
|
448 |
}
|
sonali@1021
|
449 |
writer.append(" xsi:schemaLocation='" + namespace + " " + schemaFileObject.getNameExt() + "'>\n");
|
sonali@1021
|
450 |
generateXMLBody(model, root, writer);
|
sonali@1021
|
451 |
modifyRootElementAttrs(writer);
|
sonali@1021
|
452 |
if (prefix == null || "".equals(prefix)) {
|
sonali@1021
|
453 |
writer.append("\n"); // NOI18N
|
sonali@1021
|
454 |
writer.append("</" + root + ">\n"); // NOI18N
|
sonali@1021
|
455 |
} else {
|
sonali@1021
|
456 |
writer.append("\n"); // NOI18N
|
sonali@1021
|
457 |
writer.append("</" + prefix + ":" + root + ">\n");
|
sonali@1021
|
458 |
}
|
sonali@1021
|
459 |
|
sonali@1021
|
460 |
// writer.flush();
|
sonali@1021
|
461 |
// writer.close();
|
sonali@1021
|
462 |
|
sonali@1021
|
463 |
}
|
sonali@1021
|
464 |
|
sonali@667
|
465 |
|
sonali@667
|
466 |
// If something changes dynamically (besides moving between panels), e.g.
|
sonali@667
|
467 |
// the number of panels changes in response to user input, then uncomment
|
sonali@667
|
468 |
// the following and call when needed: fireChangeEvent();
|
sonali@667
|
469 |
/*
|
sonali@667
|
470 |
private Set<ChangeListener> listeners = new HashSet<ChangeListener>(1); // or can use ChangeSupport in NB 6.0
|
sonali@667
|
471 |
public final void addChangeListener(ChangeListener l) {
|
sonali@667
|
472 |
synchronized (listeners) {
|
sonali@667
|
473 |
listeners.add(l);
|
sonali@667
|
474 |
}
|
sonali@667
|
475 |
}
|
sonali@667
|
476 |
public final void removeChangeListener(ChangeListener l) {
|
sonali@667
|
477 |
synchronized (listeners) {
|
sonali@667
|
478 |
listeners.remove(l);
|
sonali@667
|
479 |
}
|
sonali@667
|
480 |
}
|
sonali@667
|
481 |
protected final void fireChangeEvent() {
|
sonali@667
|
482 |
Iterator<ChangeListener> it;
|
sonali@667
|
483 |
synchronized (listeners) {
|
sonali@667
|
484 |
it = new HashSet<ChangeListener>(listeners).iterator();
|
sonali@667
|
485 |
}
|
sonali@667
|
486 |
ChangeEvent ev = new ChangeEvent(this);
|
sonali@667
|
487 |
while (it.hasNext()) {
|
sonali@667
|
488 |
it.next().stateChanged(ev);
|
sonali@667
|
489 |
}
|
sonali@667
|
490 |
}
|
sonali@667
|
491 |
*/
|
sonali@667
|
492 |
|
sonali@667
|
493 |
}
|