PI Documenter Java Wrapper
Recently, I worked on PI documentation for one of our projects. I used PI documenter tool for mapping objects released by Daniel Graversen (blogs The easy PI message mapping documentation service, PI mapping diff tool). I see this as a very good tool for PI mapping documentation. But, since I have too many mapping objects that should be documented and each time I need to upload one mapping object at a time, I felt this is a bit exhausting and time consuming job. Hence, I have written a java program wrapper on top of this online tool. This will basically increase the speed of documentation. Here, I am sharing the initial version of java program. Based on your comments I will try to improve the quality of this blog.
We need a Java SDK and some apache common jars (commons-codec-1.3.jar, commons-httpclient-3.0-rc4.jar and commons-logging.jar) for compiling and running this application. Apache jar archives available @http://archive.apache.org/dist/commons/
PI Documenter mode
PI Difference mode
FigafPIDocumenter Java Code
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FilenameFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class FigafPIDocumenterWrapper implements FilenameFilter
{
protected String pattern;
public FigafPIDocumenterWrapper (String str) {
pattern = str;
}
public boolean accept (File dir, String name) {
return name.toLowerCase().endsWith(pattern.toLowerCase());
}
public static void main(String[] args)
{
FigafPIDocumenterWrapperJFrame f = new FigafPIDocumenterWrapperJFrame();
f.setTitle("FigafPIDocumenterWrapper");
f.pack();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}});
f.setVisible(true);
}
public static class FigafPIDocumenterWrapperJFrame extends JFrame
{
private File targetFile;
private JTextArea taTextResponse;
private DefaultComboBoxModel cmbURLModel;
private static String downloadedFileLocation;
private static String downloadedFileLocationOLD;
String[] allximFilesInPwd; // = null;
String excelVer = "2007";
FigafPIDocumenterWrapper nf = null;
File dir = null;
File[] selFiles;
private static String baseUrl = "http://figaf.com";
public FigafPIDocumenterWrapperJFrame()
{
String[] aURLs = { "http://figaf.com/pimaplib/figaf/Web/upload.php", "http://figaf.com/pimaplib/figaf/Web/diffUpload.php" };
cmbURLModel = new DefaultComboBoxModel(aURLs);
final JComboBox cmbURL = new JComboBox(cmbURLModel);
cmbURL.setToolTipText("Enter a URL");
cmbURL.setEditable(true);
cmbURL.setSelectedIndex(0);
final JLabel lblTargetFile = new JLabel("XIM Files Directory");
final JTextField tfdTargetFile = new JTextField(30);
tfdTargetFile.setEditable(false);
final JRadioButton piDocumenter = new JRadioButton("PIDocumenter", true);
final JRadioButton piDiff = new JRadioButton("PIDifference", false);
final JRadioButton excel5 = new JRadioButton("Excel5", false); // else it is by default Excel 2007
final JCheckBox cbxExpectHeader = new JCheckBox("Use Expect header");
cbxExpectHeader.setSelected(false);
final JCheckBox allXIMFiles = new JCheckBox("All XIM Files in the selected directory");
allXIMFiles.setSelected(false);
final JButton btnDoUpload = new JButton("GenerateDocumentation");
btnDoUpload.setEnabled(false);
final JButton btnSelectFile = new JButton("PickDirectory");
final JLabel lblTargetFileOLD = new JLabel("XIM(OLD) Files Directory");
final JTextField tfdTargetFileOLD = new JTextField(30);
tfdTargetFileOLD.setEditable(false);
final JButton btnSelectFileOLD = new JButton("PickDirectory");
btnSelectFileOLD.setEnabled(false);
piDocumenter.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
piDocumenter.setSelected(true);
if (piDocumenter.isSelected())
{
piDiff.setSelected(false);
btnSelectFileOLD.setEnabled(false);
cmbURL.setSelectedIndex(0);
//tfdTargetFileOLD.setText("");
System.out.println("piDocumenter.isSelected(): " + piDocumenter.isSelected());
}
else
{
piDiff.setSelected(true);
btnSelectFileOLD.setEnabled(true);
System.out.println("piDocumenter.isSelected(): " + piDocumenter.isSelected());
}
}
});
piDiff.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
piDiff.setSelected(true);
if (piDiff.isSelected())
{
piDocumenter.setSelected(false);
btnSelectFileOLD.setEnabled(true);
cmbURL.setSelectedIndex(1);
System.out.println("piDiff.isSelected(): " + piDiff.isSelected());
}
else
{
piDocumenter.setSelected(true);
btnSelectFileOLD.setEnabled(false);
System.out.println("piDiff.isSelected(): " + piDiff.isSelected());
}
}
});
btnSelectFile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Open");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES ); //chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setMultiSelectionEnabled(true);
chooser.setAcceptAllFileFilterUsed(true);
if (chooser.showOpenDialog(FigafPIDocumenterWrapperJFrame.this) == JFileChooser.APPROVE_OPTION)
{
downloadedFileLocation = chooser.getCurrentDirectory().toString();
System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
System.out.println("getSelectedFile() : " + chooser.getSelectedFiles().toString());
selFiles = chooser.getSelectedFiles();
tfdTargetFile.setText(chooser.getCurrentDirectory().toString());
btnDoUpload.setEnabled(true);
}
else
{
System.out.println("No Selection ");
System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
}
}});
btnSelectFileOLD.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Open");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES ); //chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setMultiSelectionEnabled(true);
chooser.setAcceptAllFileFilterUsed(true);
if (chooser.showOpenDialog(FigafPIDocumenterWrapperJFrame.this) == JFileChooser.APPROVE_OPTION)
{
downloadedFileLocationOLD = chooser.getCurrentDirectory().toString();
System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
System.out.println("getSelectedFile() : " + chooser.getSelectedFiles().toString());
tfdTargetFileOLD.setText(chooser.getCurrentDirectory().toString());
btnDoUpload.setEnabled(true);
}
else
{
System.out.println("No Selection ");
System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
}
}});
taTextResponse = new JTextArea(10, 40);
taTextResponse.setEditable(false);
final JLabel lblURL = new JLabel("URL:");
//Uploading Figaf Site
btnDoUpload.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
for (int i = 0; i < selFiles.length ; i ++)
{
System.out.println("selFiles[" + i + "]" + selFiles[i]);
}
nf = new FigafPIDocumenterWrapper ("xim");
dir = new File (downloadedFileLocation);
if (!allXIMFiles.isSelected())
{
allximFilesInPwd = new String[selFiles.length];
for (int i = 0; i < selFiles.length ; i ++)
{
allximFilesInPwd[i] = (String) selFiles[i].getName() ; //.toString();
}
}
else
{
allximFilesInPwd = null;
allximFilesInPwd = dir.list(nf);
for (int i = 0; i < allximFilesInPwd.length; i++)
{
System.out.println (allximFilesInPwd[i]);
}
}
taTextResponse.setText(null);
String targetURL = cmbURL.getSelectedItem().toString();
if (!targetURL
.equals(
cmbURLModel.getElementAt(
cmbURL.getSelectedIndex()))) {
cmbURLModel.addElement(targetURL);
}
PostMethod postMethod;// = new PostMethod(targetURL);
for (int i = 0; i < allximFilesInPwd.length ; i++)
{
postMethod = null;
postMethod = new PostMethod(targetURL);
postMethod.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, cbxExpectHeader.isSelected());
try
{
if (piDocumenter.isSelected())
{
File f = new File(downloadedFileLocation + "/" + allximFilesInPwd[i]); //File f = new File(allximFilesInPwd[i]);
System.out.println("FilePath: " + allximFilesInPwd[i]);
FilePart fp = new FilePart("uploadedfile", f);
appendMessage((i+1) + ". Uploading.. \"" + allximFilesInPwd[i] + "\" to \"" + targetURL + "\"");
if (excel5.isSelected())
excelVer = "excel5";
else
excelVer = "2007";
Part[] partsDoc = { fp, new StringPart("format", excelVer) }; // parts = { fp };
postMethod.setRequestEntity(new MultipartRequestEntity(partsDoc, postMethod.getParams()));
}
else
{
File fOld = new File(downloadedFileLocationOLD + "/" + allximFilesInPwd[i]);
File fNew = new File(downloadedFileLocation + "/" + allximFilesInPwd[i]);
//File f = new File(allximFilesInPwd[i]);
System.out.println("FilePath: " + allximFilesInPwd[i]);
FilePart fpOld = new FilePart("oldxim", fOld);
FilePart fpNew = new FilePart("newxim", fNew);
appendMessage((i+1) + ". Uploading Old & New files.. \"" + allximFilesInPwd[i] + "\" to \"" + targetURL + "\"");
if (excel5.isSelected())
excelVer = "excel5";
else
excelVer = "2007";
Part[] partsDiff = { fpOld, fpNew, new StringPart("format", excelVer) }; // parts = { fp };
postMethod.setRequestEntity(new MultipartRequestEntity(partsDiff, postMethod.getParams()));
}
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(20000);
if (postMethod.validate())
{
int statusCode1 = client.executeMethod(postMethod);
if (statusCode1 == HttpStatus.SC_NOT_IMPLEMENTED)
{
appendMessage(" Upload failed, response=" + HttpStatus.getStatusText(statusCode1));
System.err.println("The Post method is not implemented by this URI");
postMethod.getResponseBodyAsString();
}
else
{
Header location = postMethod.getResponseHeader("Location");
System.out.println("location: " + location);
//appendMessage("Locaton: " + location);
System.out.println(location.getValue());
if (piDocumenter.isSelected())
appendMessage(" Upload complete.. Writing response File \"" + location.getValue().substring(location.getValue().lastIndexOf("/")+1) + "\" to " + downloadedFileLocation);
else
appendMessage(" Upload complete.. Writing response File \"" + allximFilesInPwd[i].substring(0, allximFilesInPwd[i].indexOf(".")) + "_" + location.getValue().substring(location.getValue().lastIndexOf("/")+1) + "\" to " + downloadedFileLocation);
//appendMessage(location.getValue());
GetMethod get = new GetMethod(baseUrl + location.getValue());
int statusCode2 = client.executeMethod(get);
if (statusCode2 == HttpStatus.SC_OK)
{
File fol = new File(downloadedFileLocation);
if (!fol.exists())
{
fol.mkdirs();
}
InputStream in = get.getResponseBodyAsStream();
FileOutputStream out = null;
if (piDocumenter.isSelected())
out = new FileOutputStream(downloadedFileLocation + "/" + location.getValue().substring(location.getValue().lastIndexOf("/")+1));
else
out = new FileOutputStream(downloadedFileLocation + "/" + allximFilesInPwd[i].substring(0, allximFilesInPwd[i].indexOf(".")) + "_" + location.getValue().substring(location.getValue().lastIndexOf("/")+1));
byte[] buffer = new byte[1024];
int count = -1;
while ((count = in.read(buffer)) != -1)
{
out.write(buffer, 0, count);
}
out.flush();
out.close();
}
get.releaseConnection();
}
appendMessage("\n");
System.out.println("statusLine>>>" + postMethod.getStatusLine());
}
postMethod.releaseConnection();
}
catch (Exception ex)
{
appendMessage("Error: " + ex.getMessage());
ex.printStackTrace();
}
finally
{
postMethod.releaseConnection();
}
}//for loop close
}});
getContentPane().setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.EAST;
c.fill = GridBagConstraints.NONE;
c.gridheight = 1;
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(10, 5, 5, 0);
c.weightx = 0;
c.weighty = 0;
getContentPane().add(lblURL, c);
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth = 2;
c.gridx = 1;
c.insets = new Insets(5, 5, 5, 10);
getContentPane().add(cmbURL, c);
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(10, 10, 10, 10);
c.gridwidth = 2;
c.gridx = 5;
c.gridy = 0;
getContentPane().add(piDocumenter, c);
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(10, 10, 10, 10);
c.gridwidth = 2;
c.gridx = 10;
c.gridy = 0;
getContentPane().add(piDiff, c);
c.anchor = GridBagConstraints.EAST;
c.fill = GridBagConstraints.NONE;
c.insets = new Insets(10, 5, 5, 0);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 1;
getContentPane().add(lblTargetFile, c);
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(5, 5, 5, 5);
c.gridwidth = 1;
c.gridx = 1;
getContentPane().add(tfdTargetFile, c);
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.NONE;
c.insets = new Insets(5, 5, 5, 10);
c.gridwidth = 1;
c.gridx = 2;
getContentPane().add(btnSelectFile, c);
// For PI Diff Documenter
c.anchor = GridBagConstraints.EAST;
c.fill = GridBagConstraints.NONE;
c.insets = new Insets(10, 5, 5, 0);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 2;
getContentPane().add(lblTargetFileOLD, c);
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(5, 5, 5, 5);
c.gridwidth = 1;
c.gridx = 1;
getContentPane().add(tfdTargetFileOLD, c);
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.NONE;
c.insets = new Insets(5, 5, 5, 10);
c.gridwidth = 1;
c.gridx = 2;
getContentPane().add(btnSelectFileOLD, c);
// For PI Diff Documenter
c.anchor = GridBagConstraints.EAST;
c.fill = GridBagConstraints.NONE;
c.insets = new Insets(10, 10, 10, 10);
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 3;
getContentPane().add(cbxExpectHeader, c);
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.NONE;
c.insets = new Insets(10, 10, 10, 10);
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 3;
getContentPane().add(allXIMFiles, c);
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.NONE;
c.insets = new Insets(10, 10, 10, 10);
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 3;
getContentPane().add(excel5, c);
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.NONE;
c.insets = new Insets(10, 10, 10, 10);
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 4;
getContentPane().add(btnDoUpload, c);
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(10, 10, 10, 10);
c.gridwidth = 3;
c.gridheight = 3;
c.weighty = 3;
c.gridx = 0;
c.gridy = 5;
getContentPane().add(new JScrollPane(taTextResponse), c);
}
private void appendMessage(String m) {
taTextResponse.append(m + "\n");
}
}
}
Download Executable Jar: FigafPIDocumenterWrapper.zip
Application requires JRE version>=1.4.2_19. Download, extract and then execute the FigafPIDocumenterWrapper JAR file.
Thanks for building on top of my application. I'm glad that you like the service and expand the abilities of the service.
Could you send me an email, I would like to hear more about the usage of the service.
Daniel
Thanks for your comments. It is really nice to have first comments from the application owner 🙂
The application is user friendly. I tested the program only in windows environment.
I sent you an email in detail regarding the usage.
- Praveen Gujjeti