1 11 package org.eclipse.pde.internal.ui.nls; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.IOException ; 15 import java.lang.reflect.InvocationTargetException ; 16 import java.util.Iterator ; 17 18 import org.eclipse.core.filebuffers.FileBuffers; 19 import org.eclipse.core.filebuffers.ITextFileBuffer; 20 import org.eclipse.core.filebuffers.ITextFileBufferManager; 21 import org.eclipse.core.resources.IContainer; 22 import org.eclipse.core.resources.IFile; 23 import org.eclipse.core.resources.IFolder; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IProgressMonitor; 26 import org.eclipse.core.runtime.NullProgressMonitor; 27 import org.eclipse.jface.text.BadLocationException; 28 import org.eclipse.jface.text.IDocument; 29 import org.eclipse.jface.text.TextUtilities; 30 import org.eclipse.osgi.util.NLS; 31 import org.eclipse.pde.core.IBaseModel; 32 import org.eclipse.pde.core.plugin.IPluginModelBase; 33 import org.eclipse.pde.internal.core.ibundle.IBundle; 34 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModel; 35 import org.eclipse.pde.internal.core.util.CoreUtility; 36 import org.eclipse.pde.internal.ui.util.ModelModification; 37 import org.eclipse.pde.internal.ui.util.PDEModelUtility; 38 import org.eclipse.text.edits.InsertEdit; 39 import org.eclipse.text.edits.MalformedTreeException; 40 import org.eclipse.text.edits.MultiTextEdit; 41 import org.eclipse.text.edits.ReplaceEdit; 42 import org.eclipse.ui.actions.WorkspaceModifyOperation; 43 44 public class ExternalizeStringsOperation extends WorkspaceModifyOperation { 45 46 private Object [] fChangeFiles; 47 48 public ExternalizeStringsOperation(Object [] changeFiles) { 49 fChangeFiles = changeFiles; 50 } 51 protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException , InterruptedException { 52 for (int i = 0; i < fChangeFiles.length; i++) { 53 if (fChangeFiles[i] instanceof ModelChangeFile) { 54 ModelChangeFile changeFile = (ModelChangeFile)fChangeFiles[i]; 55 ModelChange change = changeFile.getModel(); 56 IFile pFile = change.getPropertiesFile(); 57 checkPropertiesFile(pFile); 58 if (!change.localizationSet()) 59 addBundleLocalization(change, monitor); 60 61 ITextFileBufferManager pManager = FileBuffers.getTextFileBufferManager(); 62 try { 63 pManager.connect(pFile.getFullPath(), monitor); 64 ITextFileBuffer pBuffer = pManager.getTextFileBuffer(pFile.getFullPath()); 65 IDocument pDoc = pBuffer.getDocument(); 66 MultiTextEdit pEdit = new MultiTextEdit(); 67 68 doReplace(changeFile, pDoc, pEdit, monitor); 69 70 pEdit.apply(pDoc); 71 pBuffer.commit(monitor, true); 72 73 } catch (MalformedTreeException e) { 74 } catch (BadLocationException e) { 75 } finally { 76 pManager.disconnect(pFile.getFullPath(), monitor); 77 } 78 } 79 } 80 } 81 private void doReplace(ModelChangeFile changeFile, IDocument pDoc, MultiTextEdit pEdit, IProgressMonitor monitor) throws CoreException { 82 IFile uFile = changeFile.getFile(); 83 ITextFileBufferManager uManager = FileBuffers.getTextFileBufferManager(); 84 try { 85 uManager.connect(uFile.getFullPath(), monitor); 86 ITextFileBuffer uBuffer = uManager.getTextFileBuffer(uFile.getFullPath()); 87 IDocument uDoc = uBuffer.getDocument(); 88 MultiTextEdit uEdit = new MultiTextEdit(); 89 90 Iterator iter = changeFile.getChanges().iterator(); 91 92 while (iter.hasNext()) { 93 ModelChangeElement changeElement = (ModelChangeElement)iter.next(); 94 if (changeElement.isExternalized()) { 95 uEdit.addChild(new ReplaceEdit(changeElement.getOffset(), 96 changeElement.getLength(), 97 changeElement.getExternKey())); 98 pEdit.addChild(getPropertiesInsertEdit(pDoc, changeElement)); 99 } 100 } 101 uEdit.apply(uDoc); 102 uBuffer.commit(monitor, true); 103 104 } catch (MalformedTreeException e) { 105 } catch (BadLocationException e) { 106 } finally { 107 uManager.disconnect(uFile.getFullPath(), monitor); 108 } 109 } 110 111 private void addBundleLocalization(ModelChange change, IProgressMonitor mon) { 112 IPluginModelBase base = change.getParentModel(); 113 IFile manifest = base.getUnderlyingResource().getProject().getFile(PDEModelUtility.F_MANIFEST_FP); 114 final String localiz = change.getBundleLocalization(); 115 PDEModelUtility.modifyModel(new ModelModification(manifest) { 116 protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException { 117 if (model instanceof IBundlePluginModel) { 118 IBundlePluginModel bundleModel = (IBundlePluginModel) model; 119 IBundle bundle = bundleModel.getBundleModel().getBundle(); 120 bundle.setLocalization(localiz); 121 } 122 } 123 }, mon); 124 } 125 126 public static InsertEdit getPropertiesInsertEdit(IDocument doc, ModelChangeElement element) { 127 String nl = TextUtilities.getDefaultLineDelimiter(doc); 128 StringBuffer sb = new StringBuffer (nl); 129 sb.append(element.getKey()); 130 sb.append(" = "); sb.append(StringHelper.preparePropertiesString(element.getValue(), nl.toCharArray())); 132 return new InsertEdit(doc.getLength(), sb.toString()); 133 } 134 135 public static void checkPropertiesFile(IFile file) { 136 if (!file.exists()) { 137 String propertiesFileComment = NLS.bind("#Properties file for {0}", file.getProject().getName()); ByteArrayInputStream pStream = new ByteArrayInputStream (propertiesFileComment.getBytes()); 139 try { 140 IContainer container = file.getParent(); 141 if (!container.exists()) 142 CoreUtility.createFolder((IFolder)container); 144 file.create(pStream, true, new NullProgressMonitor()); 145 pStream.close(); 146 } catch (CoreException e1) { 147 } catch (IOException e) { 148 } 149 } 150 } 151 } 152 | Popular Tags |