public static void performOnRootElement( IFile resource, NodeOperation<Element> operation, boolean autoSave) throws IOException, CoreException { assert resource != null; assert operation != null; IDOMModel domModel = null; try { domModel = (IDOMModel) StructuredModelManager.getModelManager().getModelForRead(resource); if (domModel == null) { throw new IllegalArgumentException("Document is not structured: " + resource); } IStructuredDocument document = domModel.getStructuredDocument(); Element root = domModel.getDocument().getDocumentElement(); operation.process(root, document); if (autoSave && domModel.getReferenceCountForEdit() == 0) { domModel.save(); } } finally { if (domModel != null) { domModel.releaseFromRead(); } } }
/** * this method grabs the IDOMModel for the IDocument, performs the passed operation on the root * element of the document and then releases the IDOMModel root Element value is also an instance * of IndexedRegion * * @param doc * @param operation */ public static void performOnRootElement(IDocument doc, NodeOperation<Element> operation) { assert doc != null; assert operation != null; IDOMModel domModel = null; try { domModel = (IDOMModel) StructuredModelManager.getModelManager().getExistingModelForRead(doc); if (domModel == null) { throw new IllegalArgumentException("Document is not structured: " + doc); } IStructuredDocument document = domModel.getStructuredDocument(); Element root = domModel.getDocument().getDocumentElement(); operation.process(root, document); } finally { if (domModel != null) { domModel.releaseFromRead(); } } }