Search in sources :

Example 61 with EntityResolver

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

the class ConfigurationFacadeTest method testGetEntityResolver.

@Test
public void testGetEntityResolver() {
    EntityResolver testResolver = new DefaultHandler();
    Assert.assertNotSame(testResolver, configurationFacade.getEntityResolver());
    configuration.setEntityResolver(testResolver);
    Assert.assertSame(testResolver, configurationFacade.getEntityResolver());
}
Also used : EntityResolver(org.xml.sax.EntityResolver) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Test(org.junit.Test)

Example 62 with EntityResolver

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

the class ConfigurationFacadeTest method testSetEntityResolver.

@Test
public void testSetEntityResolver() {
    EntityResolver testResolver = new DefaultHandler();
    ConfigurationFacadeImpl facade = (ConfigurationFacadeImpl) configurationFacade;
    Assert.assertNull(facade.entityResolver);
    configurationFacade.setEntityResolver(testResolver);
    Assert.assertSame(testResolver, facade.entityResolver);
}
Also used : EntityResolver(org.xml.sax.EntityResolver) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Test(org.junit.Test)

Example 63 with EntityResolver

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

the class ConfigurationFacadeTest method testSetEntityResolver.

@Test
public void testSetEntityResolver() {
    EntityResolver testResolver = new DefaultHandler();
    ConfigurationFacadeImpl facade = (ConfigurationFacadeImpl) configurationFacade;
    Assert.assertNull(facade.entityResolver);
    configurationFacade.setEntityResolver(testResolver);
    Assert.assertSame(testResolver, facade.entityResolver);
}
Also used : EntityResolver(org.xml.sax.EntityResolver) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Test(org.junit.Test)

Example 64 with EntityResolver

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

the class OpenMappingUtils method searchInMappingFiles.

/**
 * Trying to find hibernate console config mapping file,
 * which is corresponding to provided element.
 *
 * @param consoleConfig
 * @param element
 * @return
 */
@SuppressWarnings("unchecked")
public static IFile searchInMappingFiles(ConsoleConfiguration consoleConfig, Object element) {
    IFile file = null;
    if (consoleConfig == null) {
        return file;
    }
    java.io.File configXMLFile = consoleConfig.getConfigXMLFile();
    if (!consoleConfig.hasConfiguration()) {
        consoleConfig.build();
        consoleConfig.buildSessionFactory();
    }
    EntityResolver entityResolver = consoleConfig.getConfiguration().getEntityResolver();
    Document doc = getDocument(configXMLFile, entityResolver);
    if (doc == null) {
        return file;
    }
    // 
    IPackageFragmentRoot[] packageFragments = getCCPackageFragmentRoots(consoleConfig);
    // 
    ArrayList<IPath> paths = new ArrayList<IPath>();
    for (int i = 0; i < packageFragments.length; i++) {
        paths.add(packageFragments[i].getPath());
    }
    // last chance to find file is the same place as configXMLFile
    paths.add(Path.fromOSString(configXMLFile.getParent()));
    // 
    for (int i = 0; i < paths.size() && file == null; i++) {
        Element sfNode = doc.getRootElement().element(HIBERNATE_TAG_SESSION_FACTORY);
        Iterator<Element> elements = sfNode.elements(HIBERNATE_TAG_MAPPING).iterator();
        while (elements.hasNext() && file == null) {
            Element subelement = elements.next();
            Attribute resourceAttr = subelement.attribute(HIBERNATE_TAG_RESOURCE);
            if (resourceAttr == null) {
                continue;
            }
            IPath path = paths.get(i).append(resourceAttr.getValue().trim());
            file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
            if (file == null || !file.exists()) {
                file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
            }
            if (file != null && file.exists()) {
                if (elementInFile(consoleConfig, file, element)) {
                    break;
                }
            }
            file = null;
        }
    }
    return file;
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) EntityResolver(org.xml.sax.EntityResolver) Document(org.dom4j.Document) IDocument(org.eclipse.jface.text.IDocument) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 65 with EntityResolver

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

the class OpenMappingUtils method rootClassInFile.

/**
 * Check has this particular rootClass correspondence in the file.
 * @param consoleConfig
 * @param file
 * @param rootClass
 * @return
 */
public static boolean rootClassInFile(ConsoleConfiguration consoleConfig, IFile file, IPersistentClass rootClass) {
    EntityResolver entityResolver = consoleConfig.getConfiguration().getEntityResolver();
    Document doc = getDocument(file.getLocation().toFile(), entityResolver);
    final String clName = getPersistentClassName(rootClass);
    final String clNameUnq = StringHelper.unqualify(clName);
    boolean res = false;
    // TODO: getElements - this is *extremely* inefficient - no need to scan the whole tree again and again.
    for (int i = 0; i < classPairs.length; i++) {
        res = getElements(doc, classPairs[i][0], classPairs[i][1], clNameUnq).hasNext();
        if (res)
            break;
        res = getElements(doc, classPairs[i][0], classPairs[i][1], clName).hasNext();
        if (res)
            break;
    }
    return res;
}
Also used : EntityResolver(org.xml.sax.EntityResolver) Document(org.dom4j.Document) IDocument(org.eclipse.jface.text.IDocument)

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