Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * #Purpose: Batch search and replace in the whole project
- * #Files: Requires 'search_replace.ini' in the current project's root
- * #File format: Plain text, where *each* line is:
- * [Search Pattern] [Tab] [Replace Pattern];
- * only the last line *must* be empty.
- * #Details: http: // <a class="linkification-ext" href="http://wp.me/p3fHEs-5e" title="Linkification: http://wp.me/p3fHEs-5e">wp.me/p3fHEs-5e</a>
- *
- *
- * @author Kos Ivantsov
- * @based on Didier Briel's "search and replace script"
- * @date 2013-07-22
- * @version 0.1
- */
- import static javax.swing.JOptionPane.*
- import static org.omegat.util.Platform.*
- import groovy.swing.SwingBuilder
- import java.awt.Component
- import javax.swing.JButton
- import javax.swing.JTable
- import javax.swing.table.*
- import javax.swing.event.*
- import java.awt.event.*
- import java.awt.BorderLayout as BL
- /*
- * set 'GUI' to anything but 'true' (with quotes) in the next line
- * if you don't need a GUI listing of all changed segments
- */
- def GUI = 'true'
- def prop = project.projectProperties
- if (!prop) {
- final def title = 'Batch search and replace'
- final def msg = 'Please try again after you open a project.'
- showMessageDialog null, msg, title, INFORMATION_MESSAGE
- return
- }
- //def folder = prop.projectRoot
- //def fileloc = folder+'/subst_template.txt'
- subst_file = new File(prop.projectRoot+'/search_replace.ini')
- if (! subst_file.exists()) {
- final def title = 'No file'
- final def msg = 'Template file for batch search and replace'+'\n' + subst_file +'\n'+'doesn\'t exist.'
- showMessageDialog null, msg, title, INFORMATION_MESSAGE
- return
- }
- length = subst_file.readLines().size()
- search_array = []
- replace_array = []
- data = []
- def count = 0
- while ( count < length ) {
- ln = subst_file.readLines().get(count).tokenize('\t')
- sr = ln[0]
- rp = ln[1]
- search_array.add(sr)
- replace_array.add(rp)
- count++ ;
- }
- def range = 0..(search_array.size() - 1)
- def segment_count = 0
- console.println "Do a search and replace.\n${'-'*10}\n"
- project.allEntries.each { ste ->
- source = ste.getSrcText();
- target = project.getTranslationInfo(ste) ? project.getTranslationInfo(ste).translation : null;
- initial_target = target
- // Skip untranslated segments
- if (target == null) return
- for ( i in range) {
- ser = search_array[i]
- rep = (replace_array[i] =~ /\$(\d+)/ ).replaceAll( '\\${(it[$1] as String) }' )
- shell = new GroovyShell()
- eval = {statement, arg -> shell.setVariable 'it', arg; shell.evaluate '"' + statement + '"' }
- target = target.replaceAll(/null/, 'repl0')
- target = target.replaceAll(ser) { eval rep, it }
- target = target.replaceAll(/null/, '')
- target = target.replaceAll(/repl0/, 'null')
- }
- if (initial_target != target) {
- data.add([ seg: ste.entryNum(), source: source, in_target: initial_target, target: target ])
- segment_count++
- editor.gotoEntry(ste.entryNum())
- console.println(ste.entryNum() + "\t" + ste.srcText + "\t" + target )
- editor.replaceEditText(target)
- }
- }
- swing = new SwingBuilder()
- frame = swing.frame(title:'Changed segments', preferredSize: [1024, 500]) {
- scrollPane {
- table() {
- tableModel(list:data) {
- propertyColumn(editable: true, header:'Segment', propertyName:'seg', minWidth: 80, maxWidth: 80, preferredWidth: 80,
- cellEditor: new TableCellEditor()
- {
- public void cancelCellEditing() {}
- public boolean stopCellEditing() { return false; }
- public Object getCellEditorValue() { return value; }
- public boolean isCellEditable(EventObject anEvent) { return true; }
- public boolean shouldSelectCell(EventObject anEvent) {return true; }
- public void addCellEditorListener(CellEditorListener l) {}
- public void removeCellEditorListener(CellEditorListener l) {}
- public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
- {
- println("value: " + value);
- org.omegat.core.Core.getEditor().gotoEntry(value);
- }
- },
- cellRenderer: new TableCellRenderer()
- {
- public Component getTableCellRendererComponent(JTable table,
- Object value,
- boolean isSelected,
- boolean hasFocus,
- int row,
- int column)
- {
- def btn = new JButton()
- btn.setText(value.toString())
- return btn
- }
- }
- )
- propertyColumn(editable: false, header:'Source',propertyName:'source', minWidth: 150, preferredWidth: 350)
- propertyColumn(editable: false, header:'Initial Target',propertyName:'in_target', minWidth: 150, preferredWidth: 350)
- propertyColumn(editable: false, header:'Target',propertyName:'target', minWidth: 150, preferredWidth: 350)
- }
- }
- }
- panel(constraints: BL.SOUTH){
- button('Quit', actionPerformed:{
- frame.visible = false
- })
- }
- }
- console.println "\n${'-'*10}"+"\n"+"Segments modified: " + segment_count
- if (segment_count == 0){
- final def title = 'Search and Replace'
- final def msg = 'No segments can be changed.'
- showMessageDialog null, msg, title, INFORMATION_MESSAGE
- return
- }
- if (GUI == 'true') {
- if (segment_count != 0){
- frame.pack()
- frame.show()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement