You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by ro...@apache.org on 2007/06/22 13:37:07 UTC

svn commit: r549789 - in /incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo: ParserErrorSetter.h SAX2Namespaces.cpp SDOSchemaSAX2Parser.cpp SDOSchemaSAX2Parser.h XSDHelper.h XSDHelperImpl.cpp XSDHelperImpl.h

Author: robbinspg
Date: Fri Jun 22 04:37:06 2007
New Revision: 549789

URL: http://svn.apache.org/viewvc?view=rev&rev=549789
Log:
TUSCANY-1362 do not import namespaces that are already defined

Modified:
    incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/ParserErrorSetter.h
    incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/SAX2Namespaces.cpp
    incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/SDOSchemaSAX2Parser.cpp
    incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/SDOSchemaSAX2Parser.h
    incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/XSDHelper.h
    incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/XSDHelperImpl.cpp
    incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/XSDHelperImpl.h

Modified: incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/ParserErrorSetter.h
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/ParserErrorSetter.h?view=diff&rev=549789&r1=549788&r2=549789
==============================================================================
--- incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/ParserErrorSetter.h (original)
+++ incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/ParserErrorSetter.h Fri Jun 22 04:37:06 2007
@@ -26,29 +26,9 @@
 
 #include "commonj/sdo/disable_warn.h"
 
-#include <vector>
-#include <map>
-#include "libxml/xmlstring.h"
-
 namespace commonj{
 namespace sdo{
 
-template<class _Kty>
-struct HashCompare
-{
-    bool operator()(const _Kty& _Keyval1, const _Kty& _Keyval2) const
-    {
-        return strcmp((char*)_Keyval1, (char*)_Keyval2) < 0;
-    }
-};
-
-class SDOSchemaSAX2Parser;
-typedef std::map<xmlChar*, SDOSchemaSAX2Parser*, HashCompare<xmlChar*> > LocationParserMap;
-struct ParsedLocations: public LocationParserMap
-{
-    virtual ~ParsedLocations();
-};
-
 /**
  * The ParserErrorSetter builds a list of all the errors which 
  * occurred during a parse, so they can be displayed for the
@@ -62,9 +42,8 @@
     virtual void setError(const char* message) = 0;
     virtual void clearErrors() = 0;
 
-    SDOSchemaSAX2Parser* parseIfNot(const void* location, bool loadImportNamespace = false, const void* base=0);
 protected:
-    ParsedLocations parsedLocations;
+
 };
 };
 };

Modified: incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/SAX2Namespaces.cpp
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/SAX2Namespaces.cpp?view=diff&rev=549789&r1=549788&r2=549789
==============================================================================
--- incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/SAX2Namespaces.cpp (original)
+++ incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/SAX2Namespaces.cpp Fri Jun 22 04:37:06 2007
@@ -88,7 +88,7 @@
 
         void SAX2Namespaces::empty()
         {
-            namespaceMap.empty();
+            namespaceMap.clear();
         }
 
     } // End - namespace sdo

Modified: incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/SDOSchemaSAX2Parser.cpp
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/SDOSchemaSAX2Parser.cpp?view=diff&rev=549789&r1=549788&r2=549789
==============================================================================
--- incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/SDOSchemaSAX2Parser.cpp (original)
+++ incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/SDOSchemaSAX2Parser.cpp Fri Jun 22 04:37:06 2007
@@ -41,8 +41,12 @@
         
         SDOSchemaSAX2Parser::SDOSchemaSAX2Parser(SchemaInfo& schemaInf,
             ParserErrorSetter* insetter,
-            bool loadImpNamespace)
-            : schemaInfo(schemaInf), SAX2Parser(insetter), loadImportNamespace(loadImpNamespace)
+            PARSED_LOCATIONS& parsedLocs,
+            DEFINED_NAMESPACES& definedNSs)
+            : schemaInfo(schemaInf), 
+              SAX2Parser(insetter),
+              parsedLocations(parsedLocs),
+              definedNamespaces(definedNSs)
         {
             bInSchema = false;
             bInvalidElement = false;
@@ -229,10 +233,10 @@
                         }                        
                     }
 
-
                     currentType.uri = schemaInfo.getTargetNamespaceURI();
                     currentType.name = "RootType";
                     currentType.localname="RootType";
+                    definedNamespaces.push_back(currentType.uri);
                     return;
                     
                 } // end schema handling
@@ -532,38 +536,37 @@
             } 
 
         }
-        
-        void SDOSchemaSAX2Parser::free(xmlChar* absoluteUri)
-        {
-            delete &schemaInfo;
-            delete this;
-            xmlFree(absoluteUri);
-        }
-        ParsedLocations::~ParsedLocations()
-        {
-            for( iterator iter = begin(); iter != end(); iter++ )
-                iter->second->free(iter->first);
-        }
-        SDOSchemaSAX2Parser* ParserErrorSetter::parseIfNot(const void* location, bool loadImportNamespace, const void* base)
+
+        int SDOSchemaSAX2Parser::parseURI(const SDOXMLString& location, const SDOXMLString& base)
         {
-            xmlChar*const absoluteUri = xmlBuildURI((xmlChar*)location, (xmlChar*)base);
+            xmlChar* absoluteUri = xmlBuildURI(location, base);
             if (! absoluteUri)
-                SDO_THROW_EXCEPTION("parseIfNot", SDOFileNotFoundException, (char*)location);
-            LocationParserMap::iterator iter = parsedLocations.find(absoluteUri);
-            if (parsedLocations.end() == iter)
-            {
-                SDOSchemaSAX2Parser*const schemaParser = new SDOSchemaSAX2Parser(*new SchemaInfo(), this, loadImportNamespace);
-			    try {
-				    if (0 == schemaParser->parse((char*)absoluteUri))
-                        return parsedLocations[ absoluteUri ] = schemaParser;
-                }
-			    catch (SDORuntimeException e)
-			    {}
-                schemaParser->free(absoluteUri);
-                return 0;
+            {
+                SDO_THROW_EXCEPTION("parseURI", SDOFileNotFoundException, location);
             }
+            SDOXMLString absUri(absoluteUri);
             xmlFree(absoluteUri);
-            return iter->second;
+
+            // Determine if this location has been parsed already
+            PARSED_LOCATIONS::iterator iter;
+            iter = parsedLocations.find(absUri);
+            if (iter != parsedLocations.end())
+            {
+                schemaInfo.setTargetNamespaceURI(iter->second);
+                return 0;
+            }
+
+
+            try
+            {
+                int rc = parse(absUri);
+                // add new location to map
+                parsedLocations[absUri] = schemaInfo.getTargetNamespaceURI();
+                return rc;
+            }
+            catch (SDORuntimeException) {}
+            
+            return -1;
         }
 
         // ============================================================================
@@ -580,61 +583,70 @@
 
             if (!bInSchema) return;
 
-            TypeDefinitionsImpl* typedefs;
 
             SDOXMLString importNamespace = attributes.getValue("namespace");
             SDOXMLString schemaLocation = attributes.getValue("schemaLocation");
+
+            SchemaInfo schemaInf;
+            SDOSchemaSAX2Parser schemaParser(schemaInf, setter, parsedLocations, definedNamespaces);
+
             if (!schemaLocation.isNull())
             {
-                SDOSchemaSAX2Parser*const schemaParser = setter->parseIfNot((const char*)schemaLocation, false, getCurrentFile());
-                if (!schemaParser)
+                if (0 != schemaParser.parseURI(schemaLocation, getCurrentFile()))
                     return;
-                typedefs = &schemaParser->getTypeDefinitions();
             }
             else
             {
                 // schemaLocation isn't present. Try loading namespace for import
-                if (loadImportNamespace
-                    && localname.equalsIgnoreCase("import")
+                if (localname.equalsIgnoreCase("import")
                     && !importNamespace.isNull())
                 {
-                    SDOSchemaSAX2Parser*const sp = setter->parseIfNot((const char*)importNamespace);
-                    if (!sp)
+                    // Do not attempt to import namespaces alredy defined
+                    for (unsigned int i = 0; i < definedNamespaces.size(); i++)
+                    {
+                        if (definedNamespaces[i].equals(importNamespace))
+                        {
+                            return;
+                        }
+                    }
+
+                    if (0 != schemaParser.parseURI(importNamespace, SDOXMLString()))
                         return;
-                    typedefs = &sp->getTypeDefinitions();
                 }
                 else
                 {
                     return;
                 }
             }
-            
-            XMLDAS_TypeDefs types = typedefs->types;
-                XMLDAS_TypeDefs::iterator iter;
-                for (iter=types.begin(); iter != types.end(); iter++)
-                {    
-                    if ((*iter).second.name.equals("RootType")
+
+            // Add the parsed types to this parsers list
+            TypeDefinitionsImpl& typedefs = schemaParser.getTypeDefinitions();
+            XMLDAS_TypeDefs types = typedefs.types;
+            XMLDAS_TypeDefs::iterator iter;
+            for (iter=types.begin(); iter != types.end(); iter++)
+            {    
+                if ((*iter).second.name.equals("RootType")
                     && currentType.name.equals("RootType")
-                    &&  (*iter).second.uri.equals(currentType.uri))
+                    && (*iter).second.uri.equals(currentType.uri))
                 {
-                        // This must be true for an import/include to be
+                    // This must be true for an import/include to be
                     // legally positioned
-                    
+
                     XMLDAS_TypeDefs::iterator find = typeDefinitions.types.find(
                         (*iter).first);
-                    
+
                     std::list<PropertyDefinitionImpl>::iterator propit;
                     std::list<PropertyDefinitionImpl>::iterator currpropit;
                     bool found;
-                    
+
                     for (propit = (*iter).second.properties.begin() ; 
-                    propit != (*iter).second.properties.end(); ++ propit)
+                        propit != (*iter).second.properties.end(); ++ propit)
                     {
                         found = false;
                         // do not merge properties whose names clash
                         for ( currpropit = currentType.properties.begin();
-                        currpropit != currentType.properties.end();
-                        ++currpropit)
+                            currpropit != currentType.properties.end();
+                            ++currpropit)
                         {
                             if ((*currpropit).name.equals((*propit).name))
                             {
@@ -644,16 +656,16 @@
                         }
                         if (!found) 
                         {
-                           currentType.properties.push_back(*propit);
+                            currentType.properties.push_back(*propit);
                         }
                     }
                 }
-                    else 
-                    {
-                        typeDefinitions.types.insert(*iter);
-                    }
+                else 
+                {
+                    typeDefinitions.types.insert(*iter);
                 }
-            
+            }
+
         }
 
         

Modified: incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/SDOSchemaSAX2Parser.h
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/SDOSchemaSAX2Parser.h?view=diff&rev=549789&r1=549788&r2=549789
==============================================================================
--- incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/SDOSchemaSAX2Parser.h (original)
+++ incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/SDOSchemaSAX2Parser.h Fri Jun 22 04:37:06 2007
@@ -37,8 +37,6 @@
     namespace sdo
     {
 
-
-
 /**
  * SDOSAX2Parser implements SAX2Parser.
  * This class gets called back by the libxml library, and
@@ -49,10 +47,13 @@
         {
             
         public:
+            typedef std::map<SDOXMLString, SDOXMLString> PARSED_LOCATIONS;
+            typedef std::vector<SDOXMLString> DEFINED_NAMESPACES;
             
             SDOSchemaSAX2Parser(SchemaInfo& schemaInfo,
                 ParserErrorSetter* insetter,
-                bool loadImportNamespace = false);
+                PARSED_LOCATIONS& parsedLocations,
+                DEFINED_NAMESPACES& definedNamepaces);
             
             virtual ~SDOSchemaSAX2Parser();
 
@@ -73,7 +74,6 @@
             virtual void stream(std::istream& input);
 
             virtual int parse(const char* filename);
- 
             virtual void endDocument();
 
             
@@ -84,7 +84,8 @@
             friend std::istream& operator>>(std::istream& input, SDOSchemaSAX2Parser& parser);
             friend std::istringstream& operator>>(std::istringstream& input, SDOSchemaSAX2Parser& parser);
 
-            void free(xmlChar* absoluteUri);
+
+            int SDOSchemaSAX2Parser::parseURI(const SDOXMLString& location, const SDOXMLString& base);
 
         private:
 
@@ -223,8 +224,9 @@
             
             TypeDefinitionsImpl typeDefinitions;
 
-            bool loadImportNamespace;
-            
+            PARSED_LOCATIONS& parsedLocations;
+            DEFINED_NAMESPACES& definedNamespaces;
+          
         };
 
 

Modified: incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/XSDHelper.h
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/XSDHelper.h?view=diff&rev=549789&r1=549788&r2=549789
==============================================================================
--- incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/XSDHelper.h (original)
+++ incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/XSDHelper.h Fri Jun 22 04:37:06 2007
@@ -53,11 +53,11 @@
              *
              */
 
-            SDO_API virtual const char* defineFile(const char* schemaFile, bool loadImportNamespace = false) = 0;
-            SDO_API virtual const char* defineFile(const SDOString& schemaFile, bool loadImportNamespace = false) = 0;
-            SDO_API virtual const char* define(std::istream& schema, bool loadImportNamespace = false) = 0;
-            SDO_API virtual const char* define(const char* schema, bool loadImportNamespace = false) = 0;
-            SDO_API virtual const char* define(const SDOString& schema, bool loadImportNamespace = false) = 0;
+            SDO_API virtual const char* defineFile(const char* schemaFile) = 0;
+            SDO_API virtual const char* defineFile(const SDOString& schemaFile) = 0;
+            SDO_API virtual const char* define(std::istream& schema) = 0;
+            SDO_API virtual const char* define(const char* schema) = 0;
+            SDO_API virtual const char* define(const SDOString& schema) = 0;
 
             /**  generate buildsXSD from types/properties
              *

Modified: incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/XSDHelperImpl.cpp
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/XSDHelperImpl.cpp?view=diff&rev=549789&r1=549788&r2=549789
==============================================================================
--- incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/XSDHelperImpl.cpp (original)
+++ incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/XSDHelperImpl.cpp Fri Jun 22 04:37:06 2007
@@ -78,60 +78,53 @@
          *
          */
 
-        const char* XSDHelperImpl::defineFile(const char* schema, bool loadImportNamespace)
+        const char* XSDHelperImpl::defineFile(const char* schema)
         {
             clearErrors();
-            SDOSchemaSAX2Parser* schemaParser;
+            SDOSchemaSAX2Parser schemaParser(schemaInfo, this, parsedLocations, definedNamespaces);
 
-        /*  Build URI allowing for Windows path
-        */
-            xmlChar*const uri = xmlCanonicPath((xmlChar*)schema);
-            try
-            {
-                schemaParser = parseIfNot(uri, loadImportNamespace);
-            }
-            catch(...)
+            // Build URI allowing for Windows path
+            xmlChar* uri = xmlCanonicPath((xmlChar*)schema);
+            SDOXMLString schemaUri(uri);
+            if (uri)
             {
                 xmlFree(uri);
-                throw;
             }
-            xmlFree(uri);
 
-            if (schemaParser)
-            {
-                defineTypes(schemaParser->getTypeDefinitions());
-                return schemaParser->getTargetNamespaceURI();
-            }
-            return 0;
+            if (0 != schemaParser.parseURI(schemaUri, SDOXMLString()))
+                return 0;
+            defineTypes(schemaParser.getTypeDefinitions());
+            return schemaInfo.getTargetNamespaceURI();
         }
-        const char* XSDHelperImpl::defineFile(const SDOString& schema, bool loadImportNamespace)
+
+        const char* XSDHelperImpl::defineFile(const SDOString& schema)
         {
-            return defineFile(schema.c_str(), loadImportNamespace);
+            return defineFile(schema.c_str());
         }
         
-        const char*  XSDHelperImpl::define(std::istream& schema, bool loadImportNamespace)
+        const char*  XSDHelperImpl::define(std::istream& schema)
         {
-            SDOSchemaSAX2Parser schemaParser(schemaInfo, this, loadImportNamespace);
+            SDOSchemaSAX2Parser schemaParser(schemaInfo, this, parsedLocations, definedNamespaces);
             clearErrors();
             schema  >> schemaParser;
             defineTypes(schemaParser.getTypeDefinitions());
             return schemaInfo.getTargetNamespaceURI();
         }
         
-        const char*  XSDHelperImpl::define(const char* schema, bool loadImportNamespace)
+        const char*  XSDHelperImpl::define(const char* schema)
         {
             std::istringstream str(schema);
-            SDOSchemaSAX2Parser schemaParser(schemaInfo, this, loadImportNamespace);
+            SDOSchemaSAX2Parser schemaParser(schemaInfo, this, parsedLocations, definedNamespaces);
             clearErrors();
             str  >> schemaParser;
             defineTypes(schemaParser.getTypeDefinitions());
             return schemaInfo.getTargetNamespaceURI();
             return define(str);
         }
-        const char*  XSDHelperImpl::define(const SDOString& schema, bool loadImportNamespace)
+        const char*  XSDHelperImpl::define(const SDOString& schema)
         {
             std::istringstream str(schema);
-            SDOSchemaSAX2Parser schemaParser(schemaInfo, this, loadImportNamespace);
+            SDOSchemaSAX2Parser schemaParser(schemaInfo, this, parsedLocations, definedNamespaces);
             clearErrors();
             str  >> schemaParser;
             defineTypes(schemaParser.getTypeDefinitions());

Modified: incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/XSDHelperImpl.h
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/XSDHelperImpl.h?view=diff&rev=549789&r1=549788&r2=549789
==============================================================================
--- incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/XSDHelperImpl.h (original)
+++ incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/XSDHelperImpl.h Fri Jun 22 04:37:06 2007
@@ -31,6 +31,7 @@
 #include "commonj/sdo/SchemaInfo.h"
 #include "commonj/sdo/TypeDefinitionsImpl.h"
 #include "commonj/sdo/ParserErrorSetter.h"
+#include "commonj/sdo/SDOSchemaSAX2Parser.h"
 
 namespace commonj
 {
@@ -64,11 +65,11 @@
              * The return value is the URI of the root Type
              *
              */
-            virtual const char* defineFile(const char* schemaFile, bool loadImportNamespace = false);
-            virtual const char* defineFile(const SDOString& schemaFile, bool loadImportNamespace = false);
-            virtual const char* define(std::istream& schema, bool loadImportNamespace = false);
-            virtual const char* define(const char* schema, bool loadImportNamespace = false);
-            virtual const char* define(const SDOString& schema, bool loadImportNamespace = false);
+            virtual const char* defineFile(const char* schemaFile);
+            virtual const char* defineFile(const SDOString& schemaFile);
+            virtual const char* define(std::istream& schema);
+            virtual const char* define(const char* schema);
+            virtual const char* define(const SDOString& schema);
             
             /** getErrorCount gets number of parse errors
              *
@@ -159,7 +160,10 @@
 
             std::vector<char*> parseErrors;
             
-            TypeDefinitions definedTypes;            
+            TypeDefinitions definedTypes;  
+            SDOSchemaSAX2Parser::PARSED_LOCATIONS parsedLocations;
+
+            SDOSchemaSAX2Parser::DEFINED_NAMESPACES definedNamespaces;        
         };
         
     } // End - namespace sdo



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org