|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is Forte for Java, Community Edition. The Initial * Developer of the Original Code is Sun Microsystems, Inc. Portions * Copyright 1997-2002 Sun Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.mdrxml; import org.xml.sax.*; import java.util.*; import java.io.*; import org.openide.*; import org.openide.filesystems.*; import org.openide.loaders.*; import org.openide.xml.*; import org.openide.util.*; //import org.netbeans.modules.xml.core.tree.ModuleEntityResolver; import org.netbeans.api.mdr.*; import xmlmodel.*; import javax.jmi.reflect.RefPackage; import javax.jmi.model.*; /** * Simple MDR filler. Waring: It does not work for mixed content XML files. * * @author Petr Kuzel */ public class FillRepositoryAction extends org.netbeans.modules.mdrxml.util.XMLToolsAction { private static final boolean DEBUG = false; // shortcuts xmlmodel.Contains contains; xmlmodel.TextNodeClass textNodeClass; xmlmodel.RootNodeClass rootNodeClass; xmlmodel.AttributeNodeClass attributeClass; xmlmodel.ElementNodeClass nodeClass; /** Creates a new instance of FillRepositoryAction */ public FillRepositoryAction() { } public org.openide.util.HelpCtx getHelpCtx() { return null; } public String getName() { return "Fill MDRepository"; } protected void performXMLAction(FileObject fo) { // EntityResolver resolver = (ModuleEntityResolver) Lookup.getDefault().lookup(ModuleEntityResolver.class); // create model // find repository implementation using MDRManager MDRepository repository = MDRManager.getDefault().getDefaultRepository(); TopManager tm = TopManager.getDefault(); // get package, fill classes XmlmodelPackage extent = (XmlmodelPackage) repository.getExtent(MDRXMLModule.XML_MODEL); contains = extent.getContains(); textNodeClass = extent.getTextNode(); rootNodeClass = extent.getRootNode(); attributeClass = extent.getAttributeNode(); nodeClass = extent.getElementNode(); // put it into repository, how to specify where? an instance? try { repository.beginTrans(true); tm.setStatusText ("XML parsing started."); String id = fo.getURL().toExternalForm(); if (DEBUG) System.err.println("URL:" + id); ParserImpl impl = new ParserImpl(id); XMLReader reader = XMLUtil.createXMLReader(); // reader.setEntityResolver(resolver); reader.setErrorHandler( new ErrorHandler() { public void error(SAXParseException ex) throws SAXException { throw ex; } public void warning(SAXParseException ex) throws SAXException { throw ex; } public void fatalError(SAXParseException ex) throws SAXException { throw ex; } }); InputSource in = new InputSource(id); in.setByteStream(fo.getInputStream()); reader.setContentHandler(impl); reader.parse(in); } catch (IOException ex) { //!!! handle somehow tm.notifyException(ex); } catch (SAXException ex) { //!!! handle somehow // it is user document error! tm.notifyException(ex); } finally { repository.endTrans(); tm.setStatusText ("XML parsing finished."); } } private class ParserImpl implements ContentHandler { private final StringBuffer content = new StringBuffer(); private boolean root = true; private final Stack parents = new Stack(); private final String name; public ParserImpl(String documentID) { name = documentID; } public void characters(char[] values, int offset, int len) throws org.xml.sax.SAXException { if (DEBUG) System.err.println("characters()"); content.append(values, offset, len); } public void endDocument() throws org.xml.sax.SAXException { } public void startDocument() throws org.xml.sax.SAXException { parents.push(null); } public void startElement(String uri, String local, String qname, org.xml.sax.Attributes attrs) throws org.xml.sax.SAXException { if (DEBUG) System.err.println("startElement"); String data = content.toString(); content.setLength(0); ElementNode me = (ElementNode) parents.peek(); if (data.trim().length() > 0) { Node text = textNodeClass.createTextNode(data); // create Contains: text, me contains.add(text, me); } // handle root node ElementNode node; if (root) { node = rootNodeClass.createRootNode(qname, name); root = false; } else { node = nodeClass.createElementNode(qname); } // fill attributes for (int i = 0; i |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.