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;
}
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);
}
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;
}
}
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);
}
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;
}
Aggregations