Search in sources :

Example 66 with EntityResolver

use of org.xml.sax.EntityResolver in project jbosstools-hibernate by jbosstools.

the class OpenMappingUtils method searchInEjb3MappingFiles.

/**
 * Trying to find hibernate console config ejb3 mapping file,
 * which is corresponding to provided element.
 *
 * @param consoleConfig
 * @param element
 * @return
 */
@SuppressWarnings("unchecked")
public static IFile searchInEjb3MappingFiles(ConsoleConfiguration consoleConfig, Object element) {
    IFile file = null;
    if (consoleConfig == null) {
        return file;
    }
    final ConsoleConfiguration cc2 = consoleConfig;
    List<String> documentPaths = (List<String>) consoleConfig.execute(new ExecutionContext.Command() {

        public Object execute() {
            String persistenceUnitName = cc2.getPreferences().getPersistenceUnitName();
            EntityResolver entityResolver = cc2.getConfiguration().getEntityResolver();
            IService service = cc2.getHibernateExtension().getHibernateService();
            return service.getJPAMappingFilePaths(persistenceUnitName, entityResolver);
        }
    });
    if (documentPaths == null) {
        return file;
    }
    IJavaProject[] projs = ProjectUtils.findJavaProjects(consoleConfig);
    ArrayList<IPath> pathsSrc = new ArrayList<IPath>();
    ArrayList<IPath> pathsOut = new ArrayList<IPath>();
    ArrayList<IPath> pathsFull = new ArrayList<IPath>();
    for (int i = 0; i < projs.length; i++) {
        IJavaProject proj = projs[i];
        IPath projPathFull = proj.getResource().getLocation();
        IPath projPath = proj.getPath();
        IPath projPathOut = null;
        try {
            projPathOut = proj.getOutputLocation();
            projPathOut = projPathOut.makeRelativeTo(projPath);
        } catch (JavaModelException e) {
        // just ignore
        }
        IPackageFragmentRoot[] pfrs = new IPackageFragmentRoot[0];
        try {
            pfrs = proj.getAllPackageFragmentRoots();
        } catch (JavaModelException e) {
        // just ignore
        }
        for (int j = 0; j < pfrs.length; j++) {
            // TODO: think about possibility to open resources from jar files
            if (pfrs[j].isArchive() || pfrs[j].isExternal()) {
                continue;
            }
            final IPath pathSrc = pfrs[j].getPath();
            final IPath pathOut = projPathOut;
            final IPath pathFull = projPathFull;
            pathsSrc.add(pathSrc);
            pathsOut.add(pathOut);
            pathsFull.add(pathFull);
        }
    }
    int scanSize = Math.min(pathsSrc.size(), pathsOut.size());
    scanSize = Math.min(pathsFull.size(), scanSize);
    for (int i = 0; i < scanSize && file == null; i++) {
        final IPath pathSrc = pathsSrc.get(i);
        final IPath pathOut = pathsOut.get(i);
        final IPath pathFull = pathsFull.get(i);
        Iterator<String> it = documentPaths.iterator();
        while (it.hasNext() && file == null) {
            String docPath = it.next();
            IPath path2DocFull = Path.fromOSString(docPath);
            IPath resPath = path2DocFull.makeRelativeTo(pathFull);
            if (pathOut != null) {
                resPath = resPath.makeRelativeTo(pathOut);
            }
            resPath = pathSrc.append(resPath);
            file = ResourcesPlugin.getWorkspace().getRoot().getFile(resPath);
            if (file == null || !file.exists()) {
                file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(resPath);
            }
            if (file != null && file.exists()) {
                if (elementInFile(consoleConfig, file, element)) {
                    break;
                }
            }
            file = null;
        }
    }
    return file;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) ConsoleConfiguration(org.hibernate.console.ConsoleConfiguration) IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) EntityResolver(org.xml.sax.EntityResolver) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IJavaProject(org.eclipse.jdt.core.IJavaProject) List(java.util.List) ArrayList(java.util.ArrayList) IService(org.jboss.tools.hibernate.runtime.spi.IService)

Example 67 with EntityResolver

use of org.xml.sax.EntityResolver in project omegat by omegat-org.

the class XHTMLFilterTest method setUp.

@Before
public final void setUp() {
    // Use custom EntityResolver to resolve DTD and entity files
    // to our locally provided files. Otherwise Java will actually
    // try to download them over the network each time, which is
    // *really* slow.
    // See http://stackoverflow.com/a/9398602
    EntityResolver er = new EntityResolver() {

        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            String filename = new File(systemId).getName();
            File localFile = new File("test/data/dtd", filename);
            if (localFile.exists()) {
                return new InputSource(new FileInputStream(localFile));
            }
            throw new IOException("Could not resolve: " + publicId + " / " + systemId);
        }
    };
    XMLUnit.setTestEntityResolver(er);
    XMLUnit.setControlEntityResolver(er);
}
Also used : InputSource(org.xml.sax.InputSource) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) Before(org.junit.Before)

Example 68 with EntityResolver

use of org.xml.sax.EntityResolver in project jvarkit by lindenb.

the class BlastMapAnnotations method doWork.

@Override
public int doWork(List<String> args) {
    try {
        /**
         * xml parser
         */
        DocumentBuilder docBuilder;
        /**
         * transforms XML/DOM to GBC entry
         */
        Unmarshaller unmarshaller;
        // create a DOM parser
        DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
        f.setCoalescing(true);
        // f.setNamespaceAware(true); no, why does it break the parsing of uniprot ??
        f.setValidating(false);
        f.setExpandEntityReferences(true);
        docBuilder = f.newDocumentBuilder();
        docBuilder.setEntityResolver(new EntityResolver() {

            @Override
            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                return new InputSource(new StringReader(""));
            }
        });
        // create a Unmarshaller for NCBI
        JAXBContext jc = JAXBContext.newInstance("gov.nih.nlm.ncbi.gb:gov.nih.nlm.ncbi.blast:org.uniprot");
        unmarshaller = jc.createUnmarshaller();
        LOG.info("reading entry " + IN);
        Document domEntry = docBuilder.parse(IN);
        GBSet gbSet = null;
        Uniprot uniprotSet = null;
        if ("GBSet".equals(domEntry.getDocumentElement().getNodeName())) {
            LOG.info("parsing as GBSet");
            gbSet = unmarshaller.unmarshal(domEntry, GBSet.class).getValue();
        } else if ("uniprot".equals(domEntry.getDocumentElement().getNodeName())) {
            LOG.info("parsing as Uniprot " + domEntry.getDocumentElement());
            uniprotSet = unmarshaller.unmarshal(domEntry, Uniprot.class).getValue();
        // LOG.info(uniprotSet.getEntry().size());
        // jc.createMarshaller().marshal(uniprotSet, System.err);
        } else {
            LOG.info("unknown root element:" + domEntry.getDocumentElement().getNodeName());
            return -1;
        }
        Document blastDom;
        if (args.size() == 1) {
            LOG.info("reading " + args.get(0));
            blastDom = docBuilder.parse(new File(args.get(0)));
        } else if (args.isEmpty()) {
            LOG.info("reading from stdin");
            blastDom = docBuilder.parse(stdin());
        } else {
            LOG.error("Illegal number of args");
            return -1;
        }
        this.blastOutput = unmarshaller.unmarshal(blastDom, BlastOutput.class).getValue();
        if (uniprotSet != null)
            printUniprot(uniprotSet);
        if (gbSet != null)
            printGB(gbSet);
        return 0;
    } catch (Exception err) {
        LOG.error(err);
        return -1;
    }
}
Also used : GBSet(gov.nih.nlm.ncbi.gb.GBSet) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) JAXBContext(javax.xml.bind.JAXBContext) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.w3c.dom.Document) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) Uniprot(org.uniprot.Uniprot) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File)

Example 69 with EntityResolver

use of org.xml.sax.EntityResolver in project cobar by alibaba.

the class ConfigUtil method getDocument.

public static Document getDocument(final InputStream dtd, InputStream xml) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    factory.setNamespaceAware(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setEntityResolver(new EntityResolver() {

        @Override
        public InputSource resolveEntity(String publicId, String systemId) {
            return new InputSource(dtd);
        }
    });
    builder.setErrorHandler(new ErrorHandler() {

        @Override
        public void warning(SAXParseException e) {
        }

        @Override
        public void error(SAXParseException e) throws SAXException {
            throw e;
        }

        @Override
        public void fatalError(SAXParseException e) throws SAXException {
            throw e;
        }
    });
    return builder.parse(xml);
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) EntityResolver(org.xml.sax.EntityResolver) SAXException(org.xml.sax.SAXException)

Example 70 with EntityResolver

use of org.xml.sax.EntityResolver in project musiccabinet by hakko.

the class AbstractSAXParserImpl method getSAXParser.

/*
	 * From http://docs.oracle.com/javase/1.4.2/docs/api/javax/xml/parsers/SAXParserFactory.html
	 * 
	 * An implementation of the SAXParserFactory class is NOT guaranteed to be thread safe.
	 * It is up to the user application to make sure about the use of the SAXParserFactory
	 * from more than one thread. Alternatively the application can have one instance of the
	 * SAXParserFactory per thread. An application can use the same instance of the factory
	 * to obtain one or more instances of the SAXParser provided the instance of the factory
	 * isn't being used in more than one thread at a time. 
	 * 
	 * TODO : woodstox parserfactory synchronization?
	 */
protected synchronized SAXParser getSAXParser() throws ParserConfigurationException, SAXException {
    SAXParser saxParser = parserFactory.newSAXParser();
    saxParser.getXMLReader().setEntityResolver(new EntityResolver() {

        @Override
        public InputSource resolveEntity(String arg0, String arg1) throws SAXException, IOException {
            return null;
        }
    });
    return saxParser;
}
Also used : InputSource(org.xml.sax.InputSource) SAXParser(javax.xml.parsers.SAXParser) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Aggregations

EntityResolver (org.xml.sax.EntityResolver)72 InputSource (org.xml.sax.InputSource)39 SAXException (org.xml.sax.SAXException)34 IOException (java.io.IOException)29 DocumentBuilder (javax.xml.parsers.DocumentBuilder)22 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)22 Document (org.w3c.dom.Document)17 DefaultHandler (org.xml.sax.helpers.DefaultHandler)17 Test (org.junit.Test)16 StringReader (java.io.StringReader)13 SAXParseException (org.xml.sax.SAXParseException)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)10 NodeList (org.w3c.dom.NodeList)10 InputStream (java.io.InputStream)9 ArrayList (java.util.ArrayList)8 Node (org.w3c.dom.Node)8 ErrorHandler (org.xml.sax.ErrorHandler)8 Element (org.w3c.dom.Element)7 File (java.io.File)6