/**
* @param file
* @return the Element
*/
public Element previewFile(IFile file) {
IDOMModel xmlModel = null;
DocumentPageVariableAdapter provider = null;
boolean pushedPageVarProvider = false;
try {
xmlModel = (IDOMModel) StructuredModelManager.getModelManager().getModelForRead(
file);
if (xmlModel != null) {
IDOMDocument doc = xmlModel.getDocument();
// XXX: need to also register page variable adapters. In the
// future, this should go to some
// SSE system registry mechanism.
xmlModel.getFactoryRegistry().addFactory(
new PageVariableAdapterFactory());
provider = new DocumentPageVariableAdapter(doc);
doc.addAdapter(provider);
provider.refresh();
PageExpressionContext.getCurrent()
.pushPageVarProvider(provider);
Node child = xmlModel.getDocument().getFirstChild();
PreviewConvertContext context = new PreviewConvertContext(this
.getDestDocument());
List results = new ArrayList();
while (child != null) {
Node node = context.previewConvert(child);
if (node != null) {
results.add(node);
}
child = child.getNextSibling();
}
if (results.size() == 0) {
return null;
} else if (results.size() == 1
&& results.get(0) instanceof Element) {
return (Element) results.get(0);
} else {
Element ret = createElement(IHTMLConstants.TAG_SPAN);
for (int i = 0, n = results.size(); i < n; i++) {
ret.appendChild((Node) results.get(i));
}
return ret;
}
}
} catch (CoreException e) {
_log.error("PreviewUtil.previewFile.CoreException", e); //$NON-NLS-1$
} catch (IOException e) {
_log.error("PreviewUtil.previewFile.IOException", e); //$NON-NLS-1$
} catch (Exception ex) {
_log.error("PreviewUtil.previewFile.CoreException", ex); //$NON-NLS-1$
} finally {
if (pushedPageVarProvider) {
PageExpressionContext.getCurrent().popPageVarProvider(provider);
}
if (xmlModel != null) {
xmlModel.releaseFromRead();
}
}
return null;
}