Skip to content

Instantly share code, notes, and snippets.

@arahansa
Last active August 29, 2015 14:27
Show Gist options
  • Save arahansa/3d9bc15700d42bbfb34e to your computer and use it in GitHub Desktop.
Save arahansa/3d9bc15700d42bbfb34e to your computer and use it in GitHub Desktop.
mybatis xml statement reader for making html form
package testDate;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class NewParser extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private static final String PROPERTY = "property=\"";
private static final String sharp = "#";
JTextArea jta = new JTextArea();
JTextArea jta2 = new JTextArea();
public NewParser() {
JPanel jpanel = new JPanel();
jpanel.setLayout(new GridLayout(2, 1));
JButton btn = new JButton("입력!");
btn.addActionListener(this);
jpanel.add(jta);
jpanel.add(jta2);
add(jpanel, BorderLayout.CENTER);
add(btn, BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setBounds(200, 200, 1200, 700);
setTitle("NewParser");
}
public static void main(String[] args){
new NewParser();
}
@Override
public void actionPerformed(ActionEvent e) {
Set<String> variableList = new HashSet<>();
String origianlText = jta.getText();
variableList.addAll(checkContentsAddItems(origianlText, sharp, sharp));
variableList.addAll(checkContentsAddItems(origianlText, PROPERTY, "\""));
jta2.setText(makeHtmlForm(variableList));
}
public Set<String> checkContentsAddItems(String contents, String firstCriteria, String lastCriteria ){
int firstLocation=0, lastLocation=0;
Set<String> items = new HashSet<>();
String item=null;
while(true){
try{
if(!contents.contains(firstCriteria))
break;
firstLocation = contents.indexOf(firstCriteria)+firstCriteria.length();
lastLocation = contents.indexOf(lastCriteria, firstLocation);
item = contents.substring(firstLocation, lastLocation);
items.add(item);
contents = contents.substring(lastLocation+1);
}catch(StringIndexOutOfBoundsException err){
System.out.println("반복 루프 종료"); break;
}
}
return items;
}
public String makeHtmlForm(Set<String> items){
StringBuilder builder = new StringBuilder();
builder.append("<hr>\n<h4>제목</h4> \n <form action=\"\" method=\"get\"> \n");
for (String item : items) {
builder.append("\t("+item+") <input type=\"text\" name=\""+item+ "\" value=\"\" /> \n");
}
builder.append("<input type=\"submit\" value=\"확인\"> \n");
builder.append("</form>");
return builder.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment