Java Code Examples for org.openide.xml.XMLUtil#createXMLReader()

The following examples show how to use org.openide.xml.XMLUtil#createXMLReader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParseUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Parsing just for detecting the version  SAX parser used
*/
public static String getVersion(InputSource is, org.xml.sax.helpers.DefaultHandler versionHandler, 
        EntityResolver ddResolver) throws IOException, SAXException {
    XMLReader reader = XMLUtil.createXMLReader(false);
    reader.setContentHandler(versionHandler);
    reader.setEntityResolver(ddResolver);
    try {
        reader.parse(is);
    } catch (SAXException ex) {
        String message = ex.getMessage();
        if (message != null && message.startsWith(EXCEPTION_PREFIX)) {
            return message.substring(EXCEPTION_PREFIX.length());
        } else {
            throw new SAXException(NbBundle.getMessage(ParseUtils.class, "MSG_cannotParse"), ex);
        }
    }
    throw new SAXException(NbBundle.getMessage(ParseUtils.class, "MSG_cannotFindRoot"));
}
 
Example 2
Source File: ParseUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Parsing just for detecting the version  SAX parser used
 */
public static String getVersion(java.io.InputStream is, org.xml.sax.helpers.DefaultHandler versionHandler,
    EntityResolver ddResolver) throws java.io.IOException, SAXException {

    XMLReader reader = XMLUtil.createXMLReader(false);
    reader.setContentHandler(versionHandler);
    reader.setEntityResolver(ddResolver);
    try {
        reader.parse(new InputSource(is));
    } catch (SAXException ex) {
        is.close();
        String message = ex.getMessage();
        if (message != null && message.startsWith(EXCEPTION_PREFIX)) {
            return message.substring(EXCEPTION_PREFIX.length());
        } else {
            throw new SAXException(NbBundle.getMessage(ParseUtils.class, "MSG_cannotParse"), ex);
        }
    }
    is.close();
    throw new SAXException(NbBundle.getMessage(ParseUtils.class, "MSG_cannotFindRoot"));
}
 
Example 3
Source File: LibrarySupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private Map<String,Map<String,List<Map<String,String>>>> getItems () {
    if (items == null)
        try {
            XMLReader reader = XMLUtil.createXMLReader ();
            Handler handler = new Handler ();
            reader.setEntityResolver (handler);
            reader.setContentHandler (handler);
            ClassLoader loader = (ClassLoader) Lookup.getDefault ().
                lookup (ClassLoader.class);
            InputStream is = loader.getResourceAsStream (resourceName);
            try {
                reader.parse (new InputSource (is));
            } finally {
                is.close ();
            }
            items = handler.result;
        } catch (Exception ex) {
            ErrorManager.getDefault ().notify (ex);
            items = Collections.<String,Map<String,List<Map<String,String>>>> emptyMap ();
        }
    return items;
}
 
Example 4
Source File: LibraryDeclarationParser.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * The recognizer entry method taking an InputSource.
 * @param input InputSource to be parsed.
 * @throws java.io.IOException on I/O error.
 * @throws SAXException propagated exception thrown by a DocumentHandler.
 * @throws javax.xml.parsers.ParserConfigurationException a parser satisfining requested configuration can not be created.
 * @throws javax.xml.parsers.FactoryConfigurationRrror if the implementation can not be instantiated.
 *
 */
public void parse(final InputSource input) throws SAXException, ParserConfigurationException, IOException {
    if (used.getAndSet(true)) {
        throw new IllegalStateException("The LibraryDeclarationParser was already used, create a new instance");  //NOI18N
    }
    try {
        final XMLReader parser = XMLUtil.createXMLReader(false, true);
        parser.setContentHandler(this);
        parser.setErrorHandler(getDefaultErrorHandler());
        parser.setEntityResolver(this);
        parser.parse(input);
    } finally {
        //Recover recognizer internal state from exceptions to be reusable
        if (!context.empty()) {
            context.clear();
        }
        if (buffer.length() > 0) {
            buffer.delete(0, buffer.length());
        }
        expectedNS = null;
    }
}
 
Example 5
Source File: ParseUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Parsing just for detecting the version  SAX parser used
*/
public static String getVersion(InputSource is, org.xml.sax.helpers.DefaultHandler versionHandler, 
        EntityResolver ddResolver) throws IOException, SAXException {
    XMLReader reader = XMLUtil.createXMLReader(false);
    reader.setContentHandler(versionHandler);
    reader.setEntityResolver(ddResolver);
    try {
        reader.parse(is);
    } catch (SAXException ex) {
        String message = ex.getMessage();
        if (message != null && message.startsWith(EXCEPTION_PREFIX)) {
            return message.substring(EXCEPTION_PREFIX.length());
        } else {
            throw new SAXException(NbBundle.getMessage(ParseUtils.class, "MSG_cannotParse"), ex);
        }
    }
    throw new SAXException(NbBundle.getMessage(ParseUtils.class, "MSG_cannotFindRoot"));
}
 
Example 6
Source File: ParseUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Parsing just for detecting the version  SAX parser used
 */
public static String getVersion(java.io.InputStream is, org.xml.sax.helpers.DefaultHandler versionHandler,
    EntityResolver ddResolver) throws java.io.IOException, SAXException {

    XMLReader reader = XMLUtil.createXMLReader(false);
    reader.setContentHandler(versionHandler);
    reader.setEntityResolver(ddResolver);
    try {
        reader.parse(new InputSource(is));
    } catch (SAXException ex) {
        is.close();
        String message = ex.getMessage();
        if (message != null && message.startsWith(EXCEPTION_PREFIX)) {
            String versionStr = message.substring(EXCEPTION_PREFIX.length());
            if ("".equals(versionStr) || "null".equals(versionStr)) { // NOI18N
                return null;
            } else {
                return versionStr;
            }
        } else {
            throw new SAXException(NbBundle.getMessage(ParseUtils.class, "MSG_cannotParse"), ex);
        }
    }
    is.close();
    throw new SAXException(NbBundle.getMessage(ParseUtils.class, "MSG_cannotFindRoot"));
}
 
Example 7
Source File: ToolbarConvertor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public Object read(Reader r) throws IOException, ClassNotFoundException {
    Lookup lkp = findContext(r);
    FileObject fo = lkp.lookup(FileObject.class);
    String displayName = fo.getFileSystem().getDecorator().annotateName(fo.getName(), Collections.singleton(fo));
    try {
        XMLReader reader = XMLUtil.createXMLReader(true);
        ToolbarParser parser = new ToolbarParser();
        reader.setContentHandler(parser);
        reader.setErrorHandler(parser);
        reader.setEntityResolver(EntityCatalog.getDefault());
        reader.parse(new InputSource(r));
        return parser.createToolbarConfiguration( fo.getName(), displayName );
    } catch( SAXException saxE ) {
        IOException ioE = new IOException();
        ioE.initCause(saxE);
        throw ioE;
    }
}
 
Example 8
Source File: PaletteEnvironmentProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private XMLReader getXMLReader() throws SAXException {
    XMLReader res = null == cachedReader ? null : cachedReader.get();
    if( null == res ) {
        res = XMLUtil.createXMLReader(true);
        res.setEntityResolver(EntityCatalog.getDefault());
        cachedReader = new WeakReference<XMLReader>(res);
    }
    return res;
}
 
Example 9
Source File: RSSFeed.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected List<FeedItem> buildItemList() throws SAXException, ParserConfigurationException, IOException {
    XMLReader reader = XMLUtil.createXMLReader( false, true );
    FeedHandler handler = new FeedHandler( getMaxItemCount() );
    reader.setContentHandler( handler );
    reader.setEntityResolver( new RSSEntityResolver() );
    reader.setErrorHandler( new ErrorCatcher() );

    InputSource is = findInputSource(new URL(url));
    reader.parse( is );

    return handler.getItemList();
}
 
Example 10
Source File: XCatalog.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Parses the specified input source. */
public CatalogParser(InputSource source) throws SAXException, IOException {
    
    XMLReader parser = XMLUtil.createXMLReader(true);
    
    // setup parser
    parser.setEntityResolver(new Resolver());  // overwrite system entity resolver
    parser.setContentHandler(this);
    parser.setErrorHandler(this);
    
    // set initial base and parse
    setBase(source.getSystemId());
    parser.parse(source);
    
}
 
Example 11
Source File: DefaultAttributes.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private XMLReader getParser(boolean validate)
throws SAXException, ParserConfigurationException, FactoryConfigurationError {
    XMLReader parser = XMLUtil.createXMLReader(validate);

    // create document handler and register it
    parser.setEntityResolver(this);
    parser.setContentHandler(this);
    parser.setErrorHandler(this);

    return parser;
}
 
Example 12
Source File: DatabaseConnectionConvertor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Object instanceCreate() throws java.io.IOException, ClassNotFoundException {
    synchronized (this) {
        Object o = refConnection.get();
        if (o != null) {
            return o;
        }

        XMLDataObject obj = getHolder();
        if (obj == null) {
            return null;
        }
        FileObject connectionFO = obj.getPrimaryFile();
        Handler handler = new Handler(connectionFO.getNameExt());
        try {
            XMLReader reader = XMLUtil.createXMLReader();
            InputSource is = new InputSource(obj.getPrimaryFile().getInputStream());
            is.setSystemId(connectionFO.toURL().toExternalForm());
            reader.setContentHandler(handler);
            reader.setErrorHandler(handler);
            reader.setEntityResolver(EntityCatalog.getDefault());

            reader.parse(is);
        } catch (SAXException ex) {
            Exception x = ex.getException();
            LOGGER.log(Level.FINE, "Cannot read " + obj + ". Cause: " + ex.getLocalizedMessage(), ex);
            if (x instanceof java.io.IOException) {
                throw (IOException)x;
            } else {
                throw new java.io.IOException(ex.getMessage());
        }
        }

        DatabaseConnection inst = createDatabaseConnection(handler);
        refConnection = new WeakReference<>(inst);
        attachListener();
        return inst;
    }
}
 
Example 13
Source File: PersistenceManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Returns a XML parser. The same parser can be returned assuming that config files
 * are parser sequentially.
 *
 * @return XML parser with set content handler, errror handler
 * and entity resolver.
 */
public XMLReader getXMLParser (DefaultHandler h) throws SAXException {
    if (parser == null) {
        // get non validating, not namespace aware parser
        parser = XMLUtil.createXMLReader();
        parser.setEntityResolver(new EntityResolver () {
            /** Implementation of entity resolver. Points to the local DTD
             * for our public ID */
            public InputSource resolveEntity (String publicId, String systemId)
            throws SAXException {
                if (ModeParser.INSTANCE_DTD_ID_1_0.equals(publicId)
                || ModeParser.INSTANCE_DTD_ID_1_1.equals(publicId)
                || ModeParser.INSTANCE_DTD_ID_1_2.equals(publicId)
                || ModeParser.INSTANCE_DTD_ID_2_0.equals(publicId)
                || ModeParser.INSTANCE_DTD_ID_2_1.equals(publicId)
                || ModeParser.INSTANCE_DTD_ID_2_2.equals(publicId)
                || ModeParser.INSTANCE_DTD_ID_2_3.equals(publicId)
                || GroupParser.INSTANCE_DTD_ID_2_0.equals(publicId)
                || TCGroupParser.INSTANCE_DTD_ID_2_0.equals(publicId)
                || TCRefParser.INSTANCE_DTD_ID_1_0.equals(publicId)
                || TCRefParser.INSTANCE_DTD_ID_2_0.equals(publicId)
                || TCRefParser.INSTANCE_DTD_ID_2_1.equals(publicId)
                || TCRefParser.INSTANCE_DTD_ID_2_2.equals(publicId)
                || WindowManagerParser.INSTANCE_DTD_ID_1_0.equals(publicId)
                || WindowManagerParser.INSTANCE_DTD_ID_1_1.equals(publicId)
                || WindowManagerParser.INSTANCE_DTD_ID_2_0.equals(publicId)
                || WindowManagerParser.INSTANCE_DTD_ID_2_1.equals(publicId)) {
                    InputStream is = new ByteArrayInputStream(new byte[0]);
                    return new InputSource(is);
                }
                return null; // i.e. follow advice of systemID
            }
        });
    }
    parser.setContentHandler(h);
    parser.setErrorHandler(h);
    return parser;
}
 
Example 14
Source File: AutomaticDependencies.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * The recognizer entry method taking an InputSource.
 * @param input InputSource to be parsed.
 * @throws IOException on I/O error.
 * @throws SAXException propagated exception thrown by a DocumentHandler.
 */
public void parse(final InputSource input) throws SAXException, IOException {
    XMLReader parser = XMLUtil.createXMLReader(false, false); // fastest mode
    parser.setContentHandler(this);
    parser.setErrorHandler(this);
    parser.setEntityResolver(this);
    parser.parse(input);
}
 
Example 15
Source File: XmlOutputParser.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Creates a new instance of XMLOutputParser */
private XmlOutputParser(TestSession session) throws SAXException {
    testSession = session;
    xmlReader = XMLUtil.createXMLReader();
    xmlReader.setContentHandler(this);
    
    regexp = JavaRegexpUtils.getInstance();
}
 
Example 16
Source File: RemotePlatformProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
@NonNull
public synchronized RemotePlatform instanceCreate() throws IOException, ClassNotFoundException {
    RemotePlatform remotePlatform = platformRef == null ? null : platformRef.get();
    if (remotePlatform == null) {
        final SAXHandler handler = new SAXHandler();
        try (InputStream in = store.getPrimaryFile().getInputStream()) {
            final XMLReader reader = XMLUtil.createXMLReader();
            InputSource is = new InputSource(in);
            is.setSystemId(store.getPrimaryFile().toURL().toExternalForm());
            reader.setContentHandler(handler);
            reader.setErrorHandler(handler);
            reader.setEntityResolver(handler);
            reader.parse(is);
        } catch (SAXException ex) {
            final Exception x = ex.getException();
            if (x instanceof java.io.IOException) {
                throw (IOException)x;
            } else {
                throw new java.io.IOException(ex);
            }
        }
        remotePlatform = RemotePlatform.create(
                handler.name,
                handler.properties,
                handler.sysProperties);
        remotePlatform.addPropertyChangeListener(this);
        platformRef = new WeakReference<>(remotePlatform);
    }
    return remotePlatform;
}
 
Example 17
Source File: Interpreter.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void parse(){
    try{
        Parser p = new XMLReaderAdapter(XMLUtil.createXMLReader());
        p.setDocumentHandler(this);
        String externalForm = url.toExternalForm();
        InputSource is = new InputSource(externalForm);
        try {
            p.parse(is);
        } catch (NullPointerException npe) {
            npe.printStackTrace();
            if (npe.getCause() != null) {
                npe.getCause().printStackTrace();
            }
        }
    }
    catch(java.io.IOException ie){
        ie.printStackTrace();
    }
    catch(org.xml.sax.SAXException se){
        se.printStackTrace();
    }
}
 
Example 18
Source File: JDBCDriverConvertor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static JDBCDriver readDriverFromFile(FileObject fo) throws IOException, MalformedURLException {
    Handler handler = new Handler();
    
    // parse the XM file
    try {
        XMLReader reader = XMLUtil.createXMLReader();
        InputSource is = new InputSource(fo.getInputStream());
        is.setSystemId(fo.toURL().toExternalForm());
        reader.setContentHandler(handler);
        reader.setErrorHandler(handler);
        reader.setEntityResolver(EntityCatalog.getDefault());

        reader.parse(is);
    } catch (SAXException ex) {
        throw new IOException(ex.getMessage());
    }
    
    // read the driver from the handler
    URL[] urls = new URL[handler.urls.size()];
    int j = 0;
    for (Iterator i = handler.urls.iterator(); i.hasNext(); j++) {
        urls[j] = new URL((String)i.next());
    }
    if (checkClassPathDrivers(handler.clazz, urls) == false) {
        return null;
    }
    
    if (handler.displayName == null) {
        handler.displayName = handler.name;
    }
    return JDBCDriver.create(handler.name, handler.displayName, handler.clazz, urls);
}
 
Example 19
Source File: PaletteItemDataObject.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void loadFile() {
    fileLoaded = true;
    PaletteItem item = paletteItem;
    if (item == null)
        item = new PaletteItem(this);

    FileObject file = getPrimaryFile();
    if (file.getSize() == 0L) { // item file is empty
        // just derive the component class name from the file name
        item.setComponentClassSource(new ClassSource(file.getName().replace('-', '.')));
        paletteItem = item;
        return;
    }
    
    // parse the XML file
    try {
        XMLReader reader = XMLUtil.createXMLReader();
        PaletteItemHandler handler = new PaletteItemHandler();
        reader.setContentHandler(handler);
        InputSource input = new InputSource(getPrimaryFile().getURL().toExternalForm());
        reader.parse(input);
        // TODO report errors, validate using DTD?
        
        item.setComponentExplicitType(handler.componentExplicitType);
        item.setComponentInitializerId(handler.componentInitializerId);
        if (handler.componentClassName != null || displayName_key != null) {
            item.setComponentClassSource(new ClassSource(handler.componentClassName, handler.entries));
            paletteItem = item;
        }
    } catch (SAXException saxex) {
        ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, saxex);
    } catch (IOException ioex) {
        ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioex);
    }
}
 
Example 20
Source File: XMLFileSystem.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private synchronized void setXmlUrls(URL[] urls, boolean validate)
throws IOException, PropertyVetoException {
    if (urls == null) {
        throw new NullPointerException("Null URL list"); // NOI18N
    }
    Collection<URL> asList = Arrays.asList(urls);
    if (asList.contains(null)) {
        throw new NullPointerException("Null URL list member: " + asList); // NOI18N
    }

    ResourceElem rootElem;
    String oldDisplayName = getDisplayName();

    if (urls.length == 0) {
        urlsToXml = urls;
        refreshChildrenInAtomicAction((AbstractFolder) getRoot(), rootElem = new ResourceElem(true, urls)); // NOI18N
        rootElem = null;

        return;
    }

    Handler handler = new Handler(DTD_MAP, rootElem = new ResourceElem(true, urls), validate); // NOI18N        

    try {
        _setSystemName("XML_" + urls[0].toExternalForm().replace('/','-')); // NOI18N
    } catch (PropertyVetoException pvx) {
        rootElem = null;
        throw pvx;
    }

    URL act = null;

    try {
        XMLReader xp = XMLUtil.createXMLReader(validate, false);
        xp.setEntityResolver(handler);
        xp.setContentHandler(handler);
        xp.setErrorHandler(handler);

        for (int index = 0; index < urls.length; index++) {
            act = urls[index];
            handler.urlContext = act;

            String systemId = act.toExternalForm();

            xp.parse(systemId);
        }
        urlsToXml = urls.clone();
        refreshChildrenInAtomicAction((AbstractFolder) getRoot(), rootElem);
    } catch (IOException iox) {
        Exceptions.attachMessage(iox, Arrays.toString(urls));
        throw iox;
    } catch (Exception e) {
        throw (IOException) new IOException(act + ": " + e.toString()).initCause(e); // NOI18N
    } finally {
        rootElem = null;
    }

    firePropertyChange(PROP_DISPLAY_NAME, oldDisplayName, getDisplayName());
}