/* * Replace an HTML/XML tag with another, preserving all the rest: * - attributes (even with incorrect syntax) * - whitespace * - character data * * by De Franciscis Dimitri * * megadix@gmail.com */ import java.util.regex.*; import java.util.LinkedList; LinkedList history = new LinkedList(); org_megadix_replaceTag() { dialog = new JDialog(view, "Replace HTML/XML tags", false); content = new JPanel(); dialog.setContentPane(content); fieldPanel = new JPanel(); srcTagLabel = new JLabel("Tag to be replaced:"); fieldPanel.add(srcTagLabel); srcTagField = new HistoryTextField("org.megadix.jedit.macro.replaceTag.srcTag"); srcTagField.setColumns(10); fieldPanel.add(srcTagField); content.add(fieldPanel); destTagLabel = new JLabel("Replace with:"); fieldPanel.add(destTagLabel); destTagField = new HistoryTextField("org.megadix.jedit.macro.replaceTag.destTag"); destTagField.setColumns(10); fieldPanel.add(destTagField); content.add(fieldPanel); buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); buttonPanel.add(Box.createGlue()); ok = new JButton("OK"); cancel = new JButton("Cancel"); ok.setPreferredSize(cancel.getPreferredSize()); dialog.getRootPane().setDefaultButton(ok); buttonPanel.add(ok); buttonPanel.add(Box.createHorizontalStrut(6)); buttonPanel.add(cancel); buttonPanel.add(Box.createGlue()); content.add(buttonPanel, "South"); // register this method as an ActionListener for // the buttons and text fields (see the section called "Register the Action Listeners") ok.addActionListener(this); cancel.addActionListener(this); srcTagField.addActionListener(this); // locate the dialog in the center of the // editing pane and make it visible (see the section called "Make the Dialog Visible") dialog.pack(); dialog.setLocationRelativeTo(view); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); // this method will be called when a button is clicked // or when ENTER is pressed (see the section called "The Action Listener") void actionPerformed(e) { if(e.getSource() != cancel) { processText(); } dialog.dispose(); } // this is where the work gets done to insert // the prefix and suffix (see the section called "Get the User's Input") void processText() { String srcTag = srcTagField.getText(); srcTag = srcTag.replaceAll("[<>]", ""); srcTag = "<(/?)" + srcTag + "([^>]*)>"; String destTag = destTagField.getText(); destTag = destTag.replaceAll("[<>\\[\\$\\\\]", ""); destTag = "<$1" + destTag + "$2>"; if(srcTag.length() == 0 || destTag.length() == 0) return; srcTagField.addCurrentToHistory(); destTagField.addCurrentToHistory(); try { buffer.beginCompoundEdit(); String source = textArea.getText(); Pattern pattern = Pattern.compile(srcTag, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(source); /* source = matcher.replaceAll(destTag); textArea.setText(source); */ //like matcher.replaceAll(): //source = matcher.replaceAll(replacement); // but with replace count int count = 0; StringBuffer sb = new StringBuffer(); while(matcher.find()){ count++; matcher.appendReplacement(sb, destTag); } matcher.appendTail(sb); source = sb.toString(); if (count == 0) { Macros.message(view, "No tags found"); } else { textArea.setText(source); Macros.message(view, count + " occurrences replaced"); } } finally { buffer.endCompoundEdit(); } } } org_megadix_replaceTag();