/* * Created on May 14, 2003 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package CDBuilder.couplingSyntaxEditor; import java.io.CharArrayReader; import java.io.FileWriter; import java.io.Writer; import java.util.LinkedList; import java.util.ListIterator; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IDocumentPartitioner; import org.eclipse.jface.text.rules.DefaultPartitioner; import org.eclipse.ui.editors.text.FileDocumentProvider; import org.eclipse.ui.part.FileEditorInput; import org.w3c.dom.DOMImplementation; import org.w3c.dom.Document; import org.w3c.dom.Element; import CDBuilder.couplingSyntaxEditor.convertToXML.Container; import CDBuilder.couplingSyntaxEditor.convertToXML.CouplingObject; import CDBuilder.couplingSyntaxEditor.convertToXML.containerCollect; import com.sun.org.apache.xml.internal.serialize.OutputFormat; import com.sun.org.apache.xml.internal.serialize.XMLSerializer; /** * This class specializes our application for file resources. It * extends from FileDocumentProvider which is a generic document * provider class. We overide 2 specific functions indicated below * to specialize file resources to our liking such as saving a * the ma files as .ma and .maml * * @author Sherwin Sim and Lucie Zhao * @version 1 May - August 2003 * @see FileDocumentProvider * */ public class cseDocumentProvider extends FileDocumentProvider { /** * This method which is overided from FileDocumentProvider * creates a new .ma document. It sets it's defualt attributes * * @param element the object that the document will be created from * @author */ protected IDocument createDocument(Object element) throws CoreException { IDocument document = super.createDocument(element); if (document != null) { IDocumentPartitioner partitioner = new DefaultPartitioner( new csePartitionScanner(), new String[] { csePartitionScanner.CD_DEFAULT, csePartitionScanner.CD_COMMENT }); partitioner.connect(document); document.setDocumentPartitioner(partitioner); } return document; } /** * This class saves document(.ma) that you are working on when the save button is * selected. We have overided this class to add functionality to save it to two * formats. It first sets up to save in the two formats gathering info for both file * types then it saves it in .ma format, and then saves it in .maml format * * @param monitor * @param element * @param document * @param overwrite */ protected void doSaveDocument(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite){ // a List Iterator Object to iterate through the list of coupling info objects ListIterator couplingInfo; //A string representing content, initialized to blank String Content = ""; FileEditorInput newelement = (FileEditorInput)element; IFile newFile = newelement.getFile(); IProject project = newFile.getProject(); IPath newPath =newFile.getLocation(); IPath anotherPath = newPath.removeLastSegments(1); try { //Use an xml scanner to scan the document CharArrayReader xmlScanner = new CharArrayReader(document.get().toCharArray()); //We created our xml collecting object and stored all the data in a link list containerCollect collector = new containerCollect(); LinkedList list = collector.collect(xmlScanner); //The original document is saved super.doSaveDocument(monitor, element, document, overwrite); //We create a new document builder with DOM implemted into it DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); // Create the maml document Document doc = impl.createDocument(null,"CDPlusPlus_Builder_Version_1.0.0", null); couplingInfo = list.listIterator(); Element root = doc.getDocumentElement(); Element MainNode; Element childNode; // The algorthm for writing our coupling info which is already in hierarchial // data format for xmling while(couplingInfo.hasNext()) { Object C1 = couplingInfo.next(); if(C1 instanceof CouplingObject) { CouplingObject C2 = (CouplingObject)C1; MainNode = doc.createElement(C2.getsyntax()); MainNode.appendChild(doc.createTextNode(C2.getData())); root.appendChild(MainNode); } if(C1 instanceof Container) { Container C3 = (Container)C1; MainNode = doc.createElement(C3.getName()); ListIterator ContList = C3.getListOfItems(); while(ContList.hasNext()) { CouplingObject C4 = (CouplingObject)ContList.next(); childNode = doc.createElement(C4.getsyntax()); childNode.appendChild(doc.createTextNode(C4.getData())); MainNode.appendChild(childNode); } root.appendChild(MainNode); } } //With the new document we set last minute settings and write it out as a file. // then we Serialize the file for general usage and refresh the project OutputFormat format = new OutputFormat(doc); format.setLineWidth(65); format.setIndenting(true); format.setIndent(2); int num = newelement.getName().length(); Writer writer = new FileWriter(anotherPath +"/" +newelement.getName().substring(0,num-3) + ".maml"); XMLSerializer serializer = new XMLSerializer(writer, format); serializer.serialize(doc); project.refreshLocal(1,null); } catch (Exception e) { System.out.println(e); } } }