You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by mp...@apache.org on 2003/05/27 21:49:07 UTC

cvs commit: db-torque/src/java/org/apache/torque/engine/database/transform XmlToAppData.java

mpoeschl    2003/05/27 12:49:07

  Modified:    src/java/org/apache/torque/engine/database/transform Tag:
                        TORQUE_3_0_BRANCH XmlToAppData.java
  Log:
  TRQ19: recursive external schemas fail
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.16.2.1  +104 -6    db-torque/src/java/org/apache/torque/engine/database/transform/Attic/XmlToAppData.java
  
  Index: XmlToAppData.java
  ===================================================================
  RCS file: /home/cvs/db-torque/src/java/org/apache/torque/engine/database/transform/Attic/XmlToAppData.java,v
  retrieving revision 1.16
  retrieving revision 1.16.2.1
  diff -u -r1.16 -r1.16.2.1
  --- XmlToAppData.java	8 Oct 2002 18:17:06 -0000	1.16
  +++ XmlToAppData.java	27 May 2003 19:49:07 -0000	1.16.2.1
  @@ -3,7 +3,7 @@
   /* ====================================================================
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -58,6 +58,8 @@
   import java.io.FileReader;
   import java.io.FileNotFoundException;
   import java.io.File;
  +import java.util.Vector;
  +import java.util.Stack;
   
   import org.apache.torque.engine.database.model.AppData;
   import org.apache.torque.engine.database.model.Column;
  @@ -107,6 +109,12 @@
   
       private static SAXParserFactory saxFactory;
   
  +    /** remember all files we have already parsed to detect looping. */
  +    private Vector alreadyReadFiles;
  +
  +    /** this is the stack to store parsing data */
  +    private Stack parsingStack = new Stack();
  +
       static
       {
           saxFactory = SAXParserFactory.newInstance();
  @@ -146,6 +154,20 @@
               {
                   throw new Error("No more double pass");
               }
  +            // check to see if we alread have parsed the file
  +            if ((alreadyReadFiles != null)
  +                    && alreadyReadFiles.contains(xmlFile))
  +            {
  +                return app;
  +            }
  +            else if (alreadyReadFiles == null)
  +            {
  +                alreadyReadFiles = new Vector(3, 1);
  +            }
  +
  +            // remember the file to avoid looping
  +            alreadyReadFiles.add(xmlFile);
  +
               currentXmlFile = xmlFile;
   
               SAXParser parser = saxFactory.newSAXParser();
  @@ -163,6 +185,7 @@
               BufferedReader br = new BufferedReader(fr);
               try
               {
  +                System.err.println("Parsing file: '" + (new File(xmlFile)).getName() + "'");
                   InputSource is = new InputSource(br);
                   parser.parse(is, this);
               }
  @@ -227,11 +250,14 @@
                       else
                       {
                           currDB = app.addDatabase(attributes);
  +                        if (currDB.getPackage() == null)
  +                        {
  +                            currDB.setPackage(defaultPackage);
  +                        }
                       }
                   }
                   else if (rawName.equals("external-schema"))
                   {
  -                    isExternalSchema = true;
                       String xmlFile = attributes.getValue("filename");
                       if (xmlFile.charAt(0) != '/')
                       {
  @@ -239,8 +265,14 @@
                           xmlFile = new File(f.getParent(), xmlFile).getPath();
                       }
   
  +                    // put current state onto the stack
  +                    ParseStackElement.pushState(this);
  +
  +                    isExternalSchema = true;
  +
                       parseFile(xmlFile);
  -                    isExternalSchema = false;
  +                    // get the last state from the stack
  +                    ParseStackElement.popState(this);
                   }
                   else if (rawName.equals("table"))
                   {
  @@ -342,9 +374,75 @@
           printParseError("Fatal Error", spe);
       }
   
  +    /**
  +     * Write an error to System.err.
  +     *
  +     * @param type error type
  +     * @param spe The parse exception that caused the callback to be invoked.
  +     */
       private final void printParseError(String type, SAXParseException spe)
       {
  -        System.err.println(type + " [line " + spe.getLineNumber()
  -                + ", row " + spe.getColumnNumber() + "]: " + spe.getMessage());
  +        System.err.println(type + "[file '"
  +                + (new File(currentXmlFile)).getName() + "', line "
  +                + spe.getLineNumber() + ", row " + spe.getColumnNumber()
  +                + "]: " + spe.getMessage());
  +    }
  +
  +    /**
  +     * When parsing multiple files that use nested <external-schema> tags we
  +     * need to use a stack to remember some values.
  +     */
  +    private static class ParseStackElement
  +    {
  +        private boolean isExternalSchema;
  +        private String currentPackage;
  +        private String currentXmlFile;
  +        private boolean firstPass;
  +
  +        /**
  +         *
  +         * @param parser
  +         */
  +        public ParseStackElement(XmlToAppData parser)
  +        {
  +            // remember current state of parent object
  +            isExternalSchema = parser.isExternalSchema;
  +            currentPackage = parser.currentPackage;
  +            currentXmlFile = parser.currentXmlFile;
  +            firstPass = parser.firstPass;
  +
  +            // push the state onto the stack
  +            parser.parsingStack.push(this);
  +        }
  +
  +        /**
  +         * Removes the top element from the stack and activates the stored state
  +         *
  +         * @param parser
  +         */
  +        public static void popState(XmlToAppData parser)
  +        {
  +            if (!parser.parsingStack.isEmpty())
  +            {
  +                ParseStackElement elem = (ParseStackElement)
  +                        parser.parsingStack.pop();
  +
  +                // activate stored state
  +                parser.isExternalSchema = elem.isExternalSchema;
  +                parser.currentPackage = elem.currentPackage;
  +                parser.currentXmlFile = elem.currentXmlFile;
  +                parser.firstPass = elem.firstPass;
  +            }
  +        }
  +
  +        /**
  +         * Stores the current state on the top of the stack.
  +         *
  +         * @param parser
  +         */
  +        public static void pushState(XmlToAppData parser)
  +        {
  +            new ParseStackElement(parser);
  +        }
       }
   }