Author: rob.stryker(a)jboss.com
Date: 2008-02-25 17:59:09 -0500 (Mon, 25 Feb 2008)
New Revision: 6566
Modified:
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/main/org/jboss/ide/eclipse/archives/core/model/ArchivesModel.java
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/main/org/jboss/ide/eclipse/archives/core/model/internal/xb/XMLBinding.java
Log:
API should throw exceptions when parsing has failed.
Modified:
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/main/org/jboss/ide/eclipse/archives/core/model/ArchivesModel.java
===================================================================
---
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/main/org/jboss/ide/eclipse/archives/core/model/ArchivesModel.java 2008-02-25
22:16:15 UTC (rev 6565)
+++
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/main/org/jboss/ide/eclipse/archives/core/model/ArchivesModel.java 2008-02-25
22:59:09 UTC (rev 6566)
@@ -45,6 +45,7 @@
import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbPackage;
import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbPackageNode;
import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbPackages;
+import org.jboss.ide.eclipse.archives.core.model.internal.xb.XMLBinding.XbException;
/**
* The root model which keeps track of registered projects
@@ -228,29 +229,29 @@
ArchiveModelNode root;
IPath packagesFile = project.append(PROJECT_PACKAGES_FILE);
- if (packagesFile.toFile().exists())
- {
+ if (packagesFile.toFile().exists()) {
+ XbPackages packages = null;
try {
FileInputStream is = new FileInputStream(packagesFile.toFile());
- XbPackages packages = XMLBinding.unmarshal(is, monitor);
+ packages = XMLBinding.unmarshal(is, monitor);
monitor.worked(1);
-
- if (packages == null) {
- // Empty / non-working XML file loaded
- ArchivesCore.getInstance().getLogger().log(IStatus.WARNING, "Could not
unmarshall packages file", null);
- return;
- }
- root = new ArchiveModelNode(project, packages, this);
- ArchiveModelNode oldRoot = archivesRoot.get(project);
- xbPackages.put(project, packages);
- archivesRoot.put(project, root);
- createPackageNodeImpl(project, packages, null);
- root.clearDeltas();
- fireRegisterProjectEvent(oldRoot, root);
- monitor.worked(1);
} catch (FileNotFoundException e) {
- ArchivesCore.getInstance().getLogger().log(IStatus.WARNING, e.getMessage(), e);
+ } catch( XbException xbe) {
}
+
+ if (packages == null) {
+ // Empty / non-working XML file loaded
+ ArchivesCore.getInstance().getLogger().log(IArchivesLogger.MSG_ERR, "Could not
unmarshall packages file", null);
+ return;
+ }
+ root = new ArchiveModelNode(project, packages, this);
+ ArchiveModelNode oldRoot = archivesRoot.get(project);
+ xbPackages.put(project, packages);
+ archivesRoot.put(project, root);
+ createPackageNodeImpl(project, packages, null);
+ root.clearDeltas();
+ fireRegisterProjectEvent(oldRoot, root);
+ monitor.worked(1);
} else {
// file not found, just create some default xbpackages and insert them
XbPackages packages = new XbPackages();
Modified:
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/main/org/jboss/ide/eclipse/archives/core/model/internal/xb/XMLBinding.java
===================================================================
---
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/main/org/jboss/ide/eclipse/archives/core/model/internal/xb/XMLBinding.java 2008-02-25
22:16:15 UTC (rev 6565)
+++
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/main/org/jboss/ide/eclipse/archives/core/model/internal/xb/XMLBinding.java 2008-02-25
22:59:09 UTC (rev 6566)
@@ -36,9 +36,9 @@
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
import org.jboss.ide.eclipse.archives.core.ArchivesCore;
import org.jboss.ide.eclipse.archives.core.model.IArchive;
+import org.jboss.ide.eclipse.archives.core.model.IArchivesLogger;
import org.jboss.ide.eclipse.archives.core.model.internal.ArchiveImpl;
import org.jboss.xb.binding.JBossXBException;
import org.jboss.xb.binding.Unmarshaller;
@@ -78,32 +78,36 @@
stream.close();
initialized = true;
} catch (IOException e) {
- ArchivesCore.getInstance().getLogger().log(IStatus.WARNING, e.getMessage(), e);
+ ArchivesCore.getInstance().getLogger().log(IArchivesLogger.MSG_ERR, e.getMessage(),
e);
}
}
- private static void binderSandbox (Runnable runnable)
- {
+ private static void binderSandbox (XbRunnable runnable) throws XbException {
ClassLoader original = Thread.currentThread().getContextClassLoader();
ClassLoader myCL = XMLBinding.class.getClassLoader();
Thread.currentThread().setContextClassLoader(myCL);
- runnable.run();
+ XbException e = null;
+ try {
+ runnable.run();
+ } catch( XbException ex ) {
+ e = ex;
+ }
Thread.currentThread().setContextClassLoader(original);
+ if( e != null )
+ throw e;
}
- private static XbPackages element = null;
-
- public static XbPackages unmarshal( String input, IProgressMonitor monitor ) {
+ public static XbPackages unmarshal( String input, IProgressMonitor monitor ) throws
XbException {
return unmarshal(new ByteArrayInputStream(input.getBytes()), monitor);
}
- public static XbPackages unmarshal (final InputStream in, final IProgressMonitor
monitor)
- {
+ public static XbPackages unmarshal (final InputStream in,
+ final IProgressMonitor monitor) throws XbException {
if( !initialized) init();
- element = null;
-
- binderSandbox(new Runnable() {
- public void run () {
+ final XbPackages[] element = new XbPackages[1];
+ element[0] = null;
+ XbRunnable runnable = new XbRunnable() {
+ public void run () throws XbException {
try {
Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller();
monitor.worked(1);
@@ -111,19 +115,20 @@
Object xmlObject = unmarshaller.unmarshal(in, binding);
monitor.worked(1);
- element = (XbPackages) xmlObject;
+ element[0] = (XbPackages) xmlObject;
monitor.worked(1);
} catch (JBossXBException e) {
- ArchivesCore.getInstance().getLogger().log(IStatus.WARNING, e.getMessage(), e);
+ throw new XbException(e);
}
}
- });
+ };
- return element;
+ binderSandbox(runnable);
+ return element[0];
}
- public static String marshall(IArchive topLevelArchive, IProgressMonitor monitor ) {
+ public static String marshall(IArchive topLevelArchive, IProgressMonitor monitor )
throws XbException {
if( topLevelArchive.isTopLevel() && topLevelArchive instanceof ArchiveImpl ) {
XbPackages packs =
(XbPackages)((ArchiveImpl)topLevelArchive).getNodeDelegate().getParent();
StringWriter sw = new StringWriter();
@@ -147,45 +152,71 @@
}
}
- public static void marshall (final XbPackages element, final Writer writer, final
IProgressMonitor monitor)
- {
+ public static void marshall (final XbPackages element, final Writer writer,
+ final IProgressMonitor monitor) throws XbException {
if( !initialized) init();
- binderSandbox(new Runnable() {
- public void run () {
+ binderSandbox(new XbRunnable() {
+ public void run () throws XbException {
+ Exception f = null;
+ InputStream stream = null;
try {
- InputStream stream = schema.openStream();
+ stream = schema.openStream();
monitor.worked(1);
XercesXsMarshaller marshaller = new XercesXsMarshaller();
marshaller.marshal(new InputStreamReader(stream), new XbPackagesObjectProvider(),
element, writer);
monitor.worked(1);
- stream.close();
} catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ f = e;
} catch (SAXException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ f = e;
} catch (ParserConfigurationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (Exception e )
- {
- e.printStackTrace();
+ f = e;
+ } catch (Exception e ) {
+ f = e;
+ } finally {
+ if( stream != null ) {
+ try {
+ stream.close();
+ } catch(IOException ioe) {}
+ }
}
+ if( f != null ) {
+ throw new XbException(f);
+ }
}
});
}
- public static String serializePackages(XbPackages packages, IProgressMonitor monitor) {
+ public static String serializePackages(XbPackages packages, IProgressMonitor monitor)
throws XbException {
+ OutputStreamWriter writer = null;
try {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
- OutputStreamWriter writer = new OutputStreamWriter(bytesOut);
+ writer = new OutputStreamWriter(bytesOut);
XMLBinding.marshall(packages, writer, monitor);
- writer.close();
return new String(bytesOut.toByteArray());
} catch( Exception e ) {
+ throw new XbException(e);
+ } finally {
+ if( writer != null )
+ try {
+ writer.close();
+ } catch( IOException ioe) {}
}
- return null;
}
+
+
+ public static interface XbRunnable {
+ public void run() throws XbException;
+ }
+
+ public static class XbException extends Exception {
+ private Exception parent;
+ public XbException(Exception e) {
+ parent = e;
+ }
+ public Exception getException() {
+ return parent;
+ }
+ }
}