Hey All:
I'm very new to the Java language and have been given a fairly complicated (to me) program to do for a course I'm taking. The following is the scenario. I'll post code examples I have and any help will be greatly appreciated. Let me apologize ahead of time for all the code involved and say thank you in advance :).
The program is the follow the following logic:
Organizations A's Client (Org_A_Client.java) uses Organization A's interface (Org_A_Interface.java) but we want A's Client to also be able to use Organization B's services as well (Org_B_FileAuthorMgr.java, Org_B_FileDateMgr.java, Org_B_FileIOMgr.java).
Now a portion of this program also involves validating an xml file to it's dtd, extracting information from that source xml file through the use of a XMLTransformation file, and applying the transformation to produce a targetxml file which is then validated against a target DTD. (I've done this portion as I have a much better understanding of XML).
At this point we have been given the following java classes:
Org_A_Client.java
package project4;
/* This class is the Organization A Client.
It reads a source xml file as input and it invokes methods defined in the
Org_A_Doc_Interface Interface on a class that implements that interface */
import java.io.*;
import java.util.Scanner;
public class Org_A_Client {
// Define a document object of type Org_A_Doc_Interface
private Org_A_Doc_Interface document;
// The Org_A_Client constructor
public Org_A_Client() {
// Instanciate the document object with a class that implements the
// Org_A_Doc_Interface
this.document = new Adapter();
}
// The Main Method
public static void main(String Args[]) {
// Instanciate a Client Object
Org_A_Client client = new Org_A_Client();
// Create a string to store user input
String inputFile = null;
System.out.print("Input file name: ");
// Read the Source xml file name provided as a command line argument
Scanner scanner = new Scanner(System.in);
inputFile = scanner.next();
// Create a string to store user input
String fileID = null;
System.out.print("Input file ID: ");
// Read the Source xml file name provided as a command line argument
fileID = scanner.next();
//Convert the String fileID to an integer value
int intFileID = Integer.parseInt(fileID);
if (inputFile != null && !inputFile.equals("")) {
// Create and empty string to store the source xml file
String file = "";
try {
// Open the file
FileInputStream fstream = new FileInputStream(inputFile);
// Convert our input stream to a
// BufferedReader
BufferedReader d = new BufferedReader(new InputStreamReader(
fstream));
// Continue to read lines while
// there are still some left to read
String temp = "";
while ((temp = d.readLine()) != null) {
// Add file contents to a String
file = file + temp;
}
d.close();
// The Client Calls the archiveDoc Method on the Org_A_Document
// object
if (!file.equals("")) {
client.document.archiveDoc(file, intFileID);
}
} catch (Exception e) {
System.err.println("File input error");
}
} else
System.out.println("Error: Invalid Input");
}
}
Org_A_Doc_Interface.java
package project4;
/* This class is the Standard Organization A Document Interface.
* It defines various methods that any XML document object
* in Organization A should understand
*/
public interface Org_A_Doc_Interface {
void archiveDoc(String XMLSourceDoc, int fileID);
String getDoc(int fileID);
String getDocDate(int fileID);
void setDocDate(String date, int fileID);
String[] getDocAuthors(int fileID);
void setDocAuthor(String authorname, int position, int fileID);
}
Org_B_FileAuthorMgr.java
package project4;
public class Org_B_FileAuthorMgr {
// This function returns the list of file authors for the file that matches
// the given fileID. For the purpose of the assignment we have not
// provided any implementation
public String[] getFileAuthors(String fileID) {
// Since we do not have any implementation, we just return a
// null String array of size 2
return new String[2];
}
// This function sets the authorname at a given position for the file that
// matches the given fileID.
// For the purpose of the assignment we have not provided any
// implementation
public void setFileAuthor(String authorname, int position, String fileID) {
}
}
Org_B_FileDateMgr.java
package project4;
public class Org_B_FileDateMgr {
// This function returns the creation date for the file that matches
// the given fileID. For the puprposes of the assignment we have not
// provided any implementation but only return a date string.
String getFileDate(String fileID) {
return "1st Nov 2007";
}
// This function sets the creation datefor the file that
// matches the given fileID.
// For the puprposes of the assignment we have not provided any
// implementation
void setFileDate(String date, String fileID) {
}
}
Org_B_FileIOMgr.java
package project4;
import java.io.*;
public class Org_B_FileIOMgr {
// This class variable stores the file location for all files
private String fileLocation;
// This function stores the given String of XMLTargetFile at the
// fileLocation which is set using the setFileLocation method
boolean storeFile(String XMLTargetFile, String fileID) {
if (this.fileLocation != null) {
FileOutputStream out; // declare a file output object
PrintStream p; // declare a print stream object
try {
// Create a new file output stream
// connected to "myfile.txt"
out = new FileOutputStream(fileLocation);
// Connect print stream to the output stream
p = new PrintStream(out);
p.println(XMLTargetFile);
p.close();
System.out.println("MSG from Org_B_FileIOMgr: Target File Successfully Saved with ID " + fileID);
} catch (Exception e) {
System.err.println("Error writing to file");
return false;
}
return true;
}
System.out.println("MSG from Org_B_FileIOMgr: Please set the File Location before storing a file");
return false;
}
// This function sets the fileLocation where the file will be stored for
// archive
void setFileLocation(String fileLocation) {
this.fileLocation = fileLocation;
}
// This function retreives the file that matches the given fileID and
// returns its contents as a string
// Only for the puprposes of the assignment we have not provided any
// implementation
String retrieveFile(String fileID) {
return "This is the retreived file";
}
}
Also, we've been given the following two classes which I believe are used to help with the xml transformation using SAX (I've done alot of research regarding parsing XML using SAX/DOM so I understand how it works, but I'm really struggling with the integration...)
FileDetailsProvider.java
package project4;
/* This is the FileDetailsProvider Class which implements the Singleton design pattern.
The class can be used in the following manner:
// Declare a object of the class type
FileDetailsProvider fp;
// Get the single instance of this class by calling the getInstance static method
fp= FileDetailsProvider.getInstance();
// Initialize the class with providing it the file name of our configuration XML file
fp.loadConfigFile("C:\\assignment4\\XMLTransformerConfig.xml");
*/
import java.io.File;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class FileDetailsProvider {
private InputHandler handler;
private SAXParserFactory factory;
private SAXParser saxParser;
private final static FileDetailsProvider INSTANCE = new FileDetailsProvider();
// Private constructor suppresses generation of a (public) default
// constructor
private FileDetailsProvider() {
// Create the content handler
handler = new InputHandler();
// Use the default (non-validating) parser
factory = SAXParserFactory.newInstance();
// Validate the XML as it is parsed by the SAX Parser: only works
// for dtd's
factory.setValidating(true);
try {
saxParser = factory.newSAXParser();
} catch (Throwable t) {
t.printStackTrace();
System.exit(0);
}
}
// This is the public static method that returns a single instance of the
// class everytime it is invoked
public static FileDetailsProvider getInstance() {
return INSTANCE;
}
// After instantiation this method needs to be called to load the XMLTransformer Configuration xml
// file that includes the xsl file details needed
// for our assignment
public void loadConfigFile(String configFile) {
try {
INSTANCE.saxParser.parse(new File(configFile), INSTANCE.handler);
} catch (Throwable t) {
t.printStackTrace();
// Exceptions thrown if validation fails or file not found
System.out.println();
System.out.println("C:\\Documents and Settings\\Jig\\Desktop\\Project 4\\Project4\\Transform.xsl");
System.exit(0);
}
}
// This method return the xsl file name
public String getXslfileName() {
return handler.getXslfileName();
}
// This method returns the xsl file location
public String getXslfileLocation() {
return handler.getXslfileLocation();
}
}
InputHandler.java
package project4;
/* This class is used by the FileDetailsProvider Class to read the XMLTranformerConfig xml
* file using a SAX parser which is a event based parser
*
*/
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
public class InputHandler extends DefaultHandler {
private String xslfileName = "";
private String xslfileLocation = "";
int fileName = 0, fileLoc = 0, DTDUrl = 0;
boolean endOfFile = false;
public InputHandler() {
}
// Start XML Document Event
public void startDocument() throws SAXException {
super.startDocument();
}
// End XML Document Event
public void endDocument() throws SAXException {
super.endDocument();
// display();
}
public void display() {
System.out.println(xslfileName);
System.out.println(xslfileLocation);
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
String eName = localName; // element name
if ("".equals(eName))
eName = qName; // not namespace-aware
if (eName.equals("File Name:")) {
fileName++;
} else if (eName.equals("File Location:")) {
fileLoc++;
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
String eName = localName; // element name
if ("".equals(eName))
eName = qName; // not namespace-aware
}
public void characters(char ch[], int start, int length)
throws SAXException {
String str = new String(ch, start, length);
// Getting the Transform File Location
if (fileLoc == 1 && xslfileLocation.equals("C:\\Documents and Settings\\Jig\\Desktop\\Project 4\\Project4\\")) {
xslfileLocation = str;
}
// Getting the Transform File Name
if (fileName == 1 && xslfileName.equals("Transform.xsl")) {
xslfileName = str;
}
}
public void processingInstruction(String target, String data)
throws SAXException {
}
// treat validation errors as fatal
public void error(SAXParseException e) throws SAXParseException {
throw e;
}
// This method return the xsl file name
public String getXslfileName() {
return xslfileName;
}
// This method returns the xsl file location
public String getXslfileLocation() {
return xslfileLocation;
}
}
I need to do the following:
1. Create an adapter class and a facade class that allows Client A through the use of Organization's A interface to to use Organization B's services.
2. Validate the Source XML against its DTD
3. Extract information regarding the XSL file from the given XMLTransformerConfig xml file
4. Apply the XSL Transformation to the source XML file to produce the target XML
5. Validate the Target XML against its DTD
Now I'm not asking for a free handout with this program completed as I really want to learn how to do a program like this, but I really don't have ANY prior java experience other than creating basic classes and methods. I don't know how to bring the whole program together in order to make it all work, so any guidance for making this work would be greatly appreciated.
I've researched over 100 links on the web and found alot of useful information on adapter patterns with java and facade patterns, as well as SAX/DOM examples for parsing xml documents and validation of the DTD, but I can't find anything that ties this all together. Your help will be saving my grade...I hope :). Thanks so much for reading this.