This form dumper utility program will dump a representation of any Form, Menu, PL/SQL, or Object Library module to standard output as a text representation of that module's object tree. Apart from being generally useful for debugging, it also contains examples for accessing any type of Forms object and/or property.
package oracle.forms.jdapi.util; import java.io.File; import java.io.PrintWriter; import java.io.FileWriter; import java.text.MessageFormat; import oracle.forms.jdapi.*; import oracle.forms.util.getopt.*; /** * Dumps passed forms objects to an output stream as text. * * Set command line options for more output, else only the * basic form tree structure will be dumped. * * See printUsage for command line options. */
public class FormDumper {/** * Need this to parse the command line options * * The string represents valid command options as detailed in the * Getopt class */
private Getopt m_options = null; boolean m_dumpAllProps = false; boolean m_dumpBoolProps = false; boolean m_dumpNumProps = false; boolean m_dumpTextProps = false; boolean m_dumpPropNames = false; String m_dumpPath = null;/** * Output stream, default to STDOUT */
private PrintWriter m_out = new PrintWriter(System.out, true);/** * Use this to indent children */
private String m_indentation = "";/** * Constructor */
public FormDumper() { try { m_options = new Getopt("abnto::p"); } catch (GetoptException e) { System.err.println("invalid command options"); printUsage(); System.exit(1); } }/** * Special constructor that does not take command line arguments. * * @param out The output writer where to send dump information. */
public FormDumper(PrintWriter out) { m_out = out; m_dumpAllProps = true; m_dumpBoolProps = true; m_dumpNumProps = true; m_dumpTextProps = true; m_dumpPropNames = true; }/** * Set the dump path. * * @param path The file where the dumper must send the information */
public void setDumpPath(String path) { m_dumpPath = path; }/** * Indirect output */
public void println(String s) { m_out.println(s); }/** * Dump a form to the output stream */
public void dumpForm(String fileName) throws Exception { FormModule fmb = FormModule.open(fileName); System.out.println("Dumping module " + fmb.getName());// Create a dump file if requested
if (m_dumpPath != null) {// use this form's FILE name to name the dump file
String thisFormName = new File(fileName).getName(); thisFormName = thisFormName.substring(0, (thisFormName.length()-4)); StringBuffer dmpFilename = new StringBuffer(); dmpFilename.append(m_dumpPath); if (!dmpFilename.toString().endsWith("/")) { dmpFilename.append("/"); } dmpFilename.append(thisFormName); dmpFilename.append(MigrationConstants.DMP_EXTENSION); m_out = new PrintWriter(new FileWriter(dmpFilename.toString()), true); }// Call the actual 'dump' method
dump(fmb);// Dump the coordinate system used by the module
m_indentation = " "; dump(new Coordinate(fmb)); m_indentation = ""; println("Dumped " + fmb.getName());// Close the module
fmb.destroy(); }/** * Recursively dump a forms object and its children to the output stream */
protected void dump(JdapiObject jo) { String className = jo.getClassName();// print out a context line for the object // If it is a coordinate system, it does not have a name
if(className.equals("Coordinate")) { println(m_indentation + "Coordinate System "); } else { println(m_indentation + className + " " + jo.getName()); }// Property classes need special treatment
if(className.equals("PropertyClass")) { dumpPropertyClass((PropertyClass)jo); } else// Generically dump the required property types only
{ if (m_dumpTextProps) { dumpTextProps(jo); } if (m_dumpBoolProps) { dumpBoolProps(jo); } if (m_dumpNumProps) { dumpNumProps(jo); }// Additionally, dump any Item list elements
if(className.equals("Item")) { dumpListElements((Item)jo); } }// use Form's metadata to get a list of all the child objects
this object can have
JdapiMetaObject meta = JdapiMetadata.getJdapiMetaObject(jo.getClass()); JdapiIterator props = meta.getChildObjectMetaProperties(); JdapiMetaProperty prop = null; JdapiIterator iter = null; JdapiSingleObject child = null;// loop through every possible kind of child object this object can have
while(props.hasNext()) { prop = (JdapiMetaProperty)props.next();// only bother if we can access these objects
if(!prop.allowGet()) { continue; }// get the actual values for the current child object type, // e.g. get the Items on a Block
iter = jo.getChildObjectProperty(prop.getPropertyId());// null is returned if there are no property values
if(iter != null) {// loop over every child value
while(iter.hasNext()) { child = (JdapiSingleObject)iter.next();// recursively navigate to it
m_indentation += " "; dump(child); m_indentation = m_indentation.substring(0, m_indentation.length()-2); } } } }/** * Dump list elements * * The object is an item; if it is a list item, * dump the list elements. * * @param item */
private void dumpListElements(Item item) { if( item.getItemType() == JdapiTypes.ITTY_LS_CTID) { if (m_dumpPropNames) { println(m_indentation + "dumping list elements"); } for(int i = 1; i <= item.getListElementCount(); i++) { String label = item.getElementLabel(i); String value = item.getElementValue(i); println( m_indentation + " " + i + ": '" + label + "' '" + value + "'"); } } }/** * Dump the property class properties */
private void dumpPropertyClass(PropertyClass pc) { String propertyVal = null;// test for every single possible property
// this is a bit hacky :)
for(int propertyId = 1; propertyId < JdapiTypes.MAXIMUM_PTID; ++propertyId) { if(!pc.hasProperty(propertyId)) { continue;// this property is not in the set
} if(pc.hasDefaultedProperty(propertyId) && !m_dumpAllProps) { continue; } Class pt = JdapiMetaProperty.getPropertyType(propertyId); if(pt == Boolean.class) { if(m_dumpBoolProps) { propertyVal = String.valueOf(pc.getBooleanProperty(propertyId)); } } else if(pt == Integer.class) { if(m_dumpNumProps) { propertyVal = String.valueOf(pc.getIntegerProperty(propertyId)); } } else if(pt == String.class) { if(m_dumpTextProps) { propertyVal = pc.getStringProperty(propertyId); } } if(null != propertyVal) { if (m_dumpPropNames) { println(m_indentation + " " + JdapiMetaProperty.getPropertyName(propertyId) + " " + propertyVal); } else { println(m_indentation + propertyVal); } propertyVal = null; } }// End loop over every property
}/** * Dump the source object text properties */
private void dumpTextProps(JdapiObject jo) { JdapiMetaObject meta = JdapiMetadata.getJdapiMetaObject(jo.getClass()); JdapiIterator props = meta.getStringMetaProperties();// for each text property
while(props.hasNext()) { JdapiMetaProperty prop = (JdapiMetaProperty) props.next(); int propertyId = prop.getPropertyId(); String propertyVal = null; try { propertyVal = jo.getStringProperty(propertyId); } catch(Exception e) { println(m_indentation + "Could_not_get_property " + JdapiMetaProperty.getPropertyName(propertyId)); continue; } if ( jo.hasProperty(propertyId) && (m_dumpAllProps || !(jo.hasDefaultedProperty(propertyId)) ) ) { if(m_dumpPropNames) { println( m_indentation + " " + JdapiMetaProperty.getPropertyName(propertyId) + " " + propertyVal); } else { println(m_indentation + propertyVal); } } } }/** * Dump the source object boolean properties */
private void dumpBoolProps(JdapiObject jo) { JdapiMetaObject meta = JdapiMetadata.getJdapiMetaObject(jo.getClass()); JdapiIterator props = meta.getBooleanMetaProperties();// for each boolean property
while(props.hasNext()) { JdapiMetaProperty prop = (JdapiMetaProperty) props.next(); int propertyId = prop.getPropertyId(); boolean propertyVal = false; try { propertyVal = jo.getBooleanProperty(propertyId); } catch(Exception e) { println(m_indentation + "Could_not_get_property " + JdapiMetaProperty.getPropertyName(propertyId)); continue; } if ( jo.hasProperty(propertyId) && (m_dumpAllProps || !(jo.hasDefaultedProperty(propertyId)) ) ) { if(m_dumpPropNames) { println(m_indentation + " " + JdapiMetaProperty.getPropertyName(propertyId) + " " + propertyVal); } else { println(m_indentation + propertyVal); } } } }/** * Dump the source object numeric properties */
private void dumpNumProps(JdapiObject jo) { JdapiMetaObject meta = JdapiMetadata.getJdapiMetaObject(jo.getClass()); JdapiIterator props = meta.getIntegerMetaProperties();// for each numeric property
while(props.hasNext()) { JdapiMetaProperty prop = (JdapiMetaProperty) props.next(); int propertyId = prop.getPropertyId(); int propertyVal = 0; try { propertyVal = jo.getIntegerProperty(propertyId); } catch(Exception e) { println(m_indentation + "Could_not_get_property " + JdapiMetaProperty.getPropertyName(propertyId)); continue; } if ( jo.hasProperty(propertyId) && (m_dumpAllProps || !(jo.hasDefaultedProperty(propertyId)) ) ) { if (m_dumpPropNames) { println(m_indentation + " " + JdapiMetaProperty.getPropertyName(propertyId) + " " + propertyVal); } else { println(m_indentation + propertyVal); } } } }/** * Output usage info to STDOUT */
public void printUsage() { System.out.println(""); System.out.println("Jdapi Form Dumper Utility"); System.out.println("Valid arguments:"); System.out.println("-a : dump all properties, not just overridden ones"); System.out.println("-b : dump boolean properties"); System.out.println("-n : dump numeric properties"); System.out.println("-t : dump text properties"); System.out.println("-p : dump property names, not just values"); System.out.println("-o : file path to output to"); }/** * Main method */
public static void main(String[] args) throws Exception { FormDumper dmp = new FormDumper(); if (args.length > 0) {// parse the command line first // this method returns the number of "real" arguments (not options)
int argCount = 0; try { argCount = dmp.m_options.parse(args); } catch(GetoptNoValueException e) { System.err.println(e.getMessage()); dmp.printUsage(); System.exit(1); } catch(GetoptParseException e) { System.err.println(e.getMessage()); dmp.printUsage(); System.exit(1); } catch(GetoptRequireException e) { System.err.println(e.getMessage()); dmp.printUsage(); System.exit(1); } if (argCount < 1) { dmp.printUsage(); System.exit(1); }// Set up migration options from command line
dmp.m_dumpAllProps = dmp.m_options.hasOption('a'); dmp.m_dumpBoolProps = dmp.m_options.hasOption('b'); dmp.m_dumpNumProps = dmp.m_options.hasOption('n'); dmp.m_dumpTextProps = dmp.m_options.hasOption('t'); dmp.m_dumpPropNames = dmp.m_options.hasOption('p'); dmp.m_dumpPath = dmp.m_options.getOption('o'); for (int i = 0; i < argCount; i++) { dmp.dumpForm(args[i]); } System.out.println(""); System.out.println("Dumps complete"); System.out.println(""); } else { dmp.printUsage(); } } }