alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

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 NetBeans. 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.xml.core.wizard;

import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;
import org.netbeans.api.xml.services.UserCatalog;
import org.openide.util.Lookup;
import org.openide.xml.XMLUtil;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

/**
 * Get (potentially partial) SchemaInfo from passed XML Schema.
 *
 * @author  Petr Kuzel
 * @see     SchemaInfo
 */
final class SchemaParser extends DefaultHandler {

    private SchemaInfo info =  new SchemaInfo();
    
    // root elemnt depth is 0, its children has 1 etc.
    private int depth = 0;
    
    /** Creates a new instance of SchemaParser */
    public SchemaParser() {
    }
    
    public SchemaInfo parse(String sid) {
        if (sid == null) {
            return null;
        } else {
            return parse( new InputSource(sid));
        }
    }
    
    public SchemaInfo parse(InputSource in) {
    
        Util.THIS.debug("SchemaParser started.");                                           // NOI18N
                
        try {
            depth = 0;
            XMLReader parser = XMLUtil.createXMLReader(false, true);
            parser.setContentHandler(this);
            parser.setErrorHandler(this);

            UserCatalog catalog = UserCatalog.getDefault();
            EntityResolver res = (catalog == null ? null : catalog.getEntityResolver());
            
            if (res != null) parser.setEntityResolver(res);
            
            parser.parse(in);
            
            return info;
            
        } catch (SAXException ex) {
            Util.THIS.debug("Ignoring ex. thrown while looking for Schema roots:", ex);     // NOI18N
            if (ex.getException() instanceof RuntimeException) {
                Util.THIS.debug("Nested exception:", ex.getException());                    // NOI18N
            }                        
            return info;  // better partial result than nothing
        } catch (IOException ex) {
            Util.THIS.debug("Ignoring ex. thrown while looking for Schema roots:", ex);     // NOI18N
            return info;  // better partial result than nothing
        } finally {
            Util.THIS.debug("SchemaParser stopped.");                                       // NOI18N
        }
        
    }
    
    public void startElement (String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
        depth++;
        if (depth > 2) return;
        
        //??? should be more accurate, check ns etc
        // may be, we should be also interested in "defaultForm" attributes
                
        if ("element".equals(localName)) {                                      // NOI18N
            String root = atts.getValue("name");
            if (root != null) {
                Util.THIS.debug("\telement decl: " + root);                     // NOI18N
                info.roots.add(root);
            }
        } else if ("schema".equals(localName)) {                                // NOI18N
            String ns = atts.getValue("targetNamespace");                       // NOI18N
            if (ns != null) {
                Util.THIS.debug("\ttarget namespace: " + ns);                   // NOI18N
                info.namespace = ns;
            }
        }
    }        
    
    public void endElement (String uri, String localName, String qName) {
        depth--;
    }
    
    /**
     * Very basic information structure about schema.
     */
    public static final class SchemaInfo {
        /**
         * Root candidates
         */
        public final Set roots = new TreeSet();
        
        /**
         * Target namespace or null
         */
        public String namespace;
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.