macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Applying "Digital Hub" Concepts to Enterprise Software Design, Part 4
Pages: 1, 2, 3, 4, 5, 6

Upload XML User Interface

Now we have all of the code required to parse an XML file and add data items to the database. However, we still need to create a user interface that allows people to upload the XML using their web browsers. Let's make some changes to the Main component that we created earlier.

  1. First, add the following import statements to the beginning of the Main.java file:

    import java.io.*;
    import org.xml.sax.*;
  2. Second, add the following class variables to the Main class in Main.java:

    public String aFileName;
    public String aMimeType;
    public InputStream is = null;
  3. Third, add the following methods to the Main class in Main.java:

public WOComponent uploadData(){
  System.out.println("data uploading");
  return null;
}
    
public String xmlResults(){
  System.out.println(aMimeType);
  if (is != null) {
    try {
      if ((aFileName!=null) &&
          (aFileName.length()>0) &&
          aMimeType.equals("text/xml")) {
         try {
           XML.addNewData(new InputSource(is));
                        
         } catch (Exception e) {
           System.out.println("Error: failed to read " +
                              "from the input stream." +
                               e.getMessage());
         }
       } else {
         return "File selected is not XML";
       }
       return "Parsed " + aFileName;
     } catch (Exception exception) {
       return "Error parsing XML.";
     }
   } else {
     return "No file uploaded";
   }
 }
  1. Here is a link to the complete Main.java file.

The class variables that we added will store information on the file being uploaded. They will be populated with information sent from the web browser. The uploadData method returns null, which tells WebObjects to reload the existing page when this method is called. We are going to call it when a user presses a submit button on the web page. The xmlResults calls our addNewData method that we already created in the XML class. It has a bunch of error checking to handle bad input or missing input. xmlResults also returns a status string that we will display in the web browser.

Next, we will edit the Main.html file.

  1. Add the following HTML in the body tag:

<br />

<h3>Add New Weather Data</h3>
        
<WEBOBJECT NAME=FileUploadForm>
  <ol>
    <li><p><WEBOBJECT NAME=FileUpload></WEBOBJECT></p></li>
    <li><p>
      <WEBOBJECT NAME=SubmitButton></WEBOBJECT>
    </p></li>
    <li><p>Results: <br />
      <WEBOBJECT NAME=String1></WEBOBJECT>
    </p></li>
  </ol>		
</WEBOBJECT>
  1. Here is a link to the complete Main.html

This code creates several user interface elements to be displayed on our web page. It will contain a file upload control, a submit button, and the return value of xmlResults. These three elements are wrapped in form tags. Because these elements all will be dynamically generated by WebObjects, we need to update the Main.wod file to set some attributes of these tags, as well as linking them to the Java code we just wrote.

  1. Add the following code to the end of Main.wod:

    FileUpload: WOFileUpload { 	
        inputStream = is;
        filePath = aFileName;
        size = "20";
        mimeType = aMimeType;
    }
    
    FileUploadForm: WOForm {
        enctype = "multipart/form-data";
        multipleSubmit = false;
    }
    
    String1: WOString {
        value = xmlResults;
        escapeHTML = false;
    }
    
    SubmitButton: WOSubmitButton {
        action = uploadData;
    }
  2. Here is a link to the complete Main.wod file.

The code we just added tells WebObjects where to store the data collected from the web page user (as is the case for FileUpload) and tells WebObjects where to put the values returned from the Java code (as is the case in String1). We are doing a bit of a trick to call the xmlResults method. Each time the page is requested by a web browser, WebObjects adds a string to the HTML, based on the return value of xmlResults. This forces the function to run every time the page is loaded. The first time it is loaded, there will be no XML to parse, so it will return "No file uploaded." When a user chooses a file to upload and hits submit, the web page will be loaded again, but this time there will be an XML file to parse.

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow