Notes on WST StructuredDocument ------------------------------- Created: 2010/11/26 References: WST 3.1.x, Eclipse 3.5 Galileo To manipulate XML documents in refactorings, we sometimes use the WST/SEE "StructuredDocument" API. There isn't exactly a lot of documentation on this out there, so this is a short explanation of how it works, totally based on _empirical_ evidence. As such, it must be taken with a grain of salt. Examples of usage can be found in sdk/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/refactorings/ 1- Get a document instance -------------------------- To get a document from an existing IFile resource: IModelManager modelMan = StructuredModelManager.getModelManager(); IStructuredDocument sdoc = modelMan.createStructuredDocumentFor(file); Note that the IStructuredDocument and all the associated interfaces we'll use below are all located in org.eclipse.wst.sse.core.internal.provisional, meaning they _might_ change later. Also note that this parses the content of the file on disk, not of a buffer with pending unsaved modifications opened in an editor. There is a counterpart for non-existent resources: IModelManager.createNewStructuredDocumentFor(IFile) However our goal so far has been to _parse_ existing documents, find the place that we wanted to modify and then generate a TextFileChange for a refactoring operation. Consequently this document doesn't say anything about using this model to modify content directly. 2- Structured Document overview ------------------------------- The IStructuredDocument is organized in "regions", which are little pieces of text. The document contains a list of region collections, each one being a list of regions. Each region has a type, as well as text. Since we use this to parse XML, let's look at this XML example: \n \n Some Value \n This will result in the following regions and sub-regions: (all the constants below are located in DOMRegionContext) XML_PI_OPEN XML_PI_OPEN: XML_CONTENT XML_CONTENT:\n XML_TAG_NAME XML_TAG_OPEN:< XML_TAG_NAME:resources XML_TAG_CLOSE:> XML_CONTENT XML_CONTENT:\n + whitespace before color XML_TAG_NAME XML_TAG_OPEN:< XML_TAG_NAME:color XML_EMPTY_TAG_CLOSE:/> XML_CONTENT XML_CONTENT:\n + whitespace before string XML_TAG_NAME XML_TAG_OPEN:< XML_TAG_NAME:string XML_TAG_ATTRIBUTE_NAME:name XML_TAG_ATTRIBUTE_EQUALS:= XML_TAG_ATTRIBUTE_VALUE:"my_string" XML_TAG_CLOSE:> XML_CONTENT XML_CONTENT:Some Value XML_TAG_NAME XML_END_TAG_OPEN: XML_CONTENT XML_CONTENT: (2 spaces before the comment) XML_COMMENT_TEXT XML_COMMENT_OPEN: