package formsdumper; import java.io.File; import java.io.PrintWriter; import java.io.FileWriter; import java.text.MessageFormat; import oracle.forms.jdapi.*; /** * Dumps passed forms JdapiObjects 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 */ /* changed following b */ /* boolean m_dumpAllProps = false; boolean m_dumpBoolProps = false; boolean m_dumpNumProps = false; boolean m_dumpTextProps = false; boolean m_dumpPropNames = false; */ boolean m_dumpAllProps = true; boolean m_dumpBoolProps = true; boolean m_dumpNumProps = true; boolean m_dumpTextProps = true; boolean m_dumpPropNames = true; 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() { } /** * 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()); 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); 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 JdapiObject and its children to the output stream */ protected void dump(JdapiObject jo) { String className = jo.getClassName(); // print out a context line for the JdapiObject // 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 JdapiObjects this // JdapiObject can have b commented this JdapiMetaObject meta = JdapiMetadata.getJdapiMetaObject(jo.getClass()); JdapiIterator props = meta.getChildObjectMetaProperties(); JdapiMetaProperty prop = null; JdapiIterator iter = null; JdapiObject child = null; // loop through every possible kind of child JdapiObject this JdapiObject // can have b commented this while (props.hasNext()) { prop = (JdapiMetaProperty)props.next(); // only bother if we can access these JdapiObjects if (!prop.allowGet()) { continue; } // get the actual values for the current child JdapiObject 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 = (JdapiObject)iter.next(); // recursively navigate to it m_indentation += " "; dump(child); if (m_indentation.length() > 2) m_indentation = m_indentation.substring(0, m_indentation.length() - 2); } } } } /** * Dump list elements * * The JdapiObject 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) + " " + // changed by b propertyVal); } else { println(m_indentation + propertyVal); } propertyVal = null; } } // End loop over every property } /** * Dump the source JdapiObject 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 JdapiObject 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)) { if (m_dumpPropNames) { println(m_indentation + " " + JdapiMetaProperty.getPropertyName(propertyId) + " " + propertyVal); } else { println(m_indentation + propertyVal); } } } } /** * Dump the source JdapiObject 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); //changed by b } 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(); for (int i = 0; i < args.length; i++) { dmp.dumpForm(args[i]); } System.out.println(""); System.out.println("Dumps complete"); System.out.println(""); } }