jaxb
  1. jaxb
  2. JAXB-977

XMLCatalog not used from xjc/xjctask when strict validation is enabled (JAXB-616 not actually fixed)

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 2.2.7
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None

      Description

      When using XJC from ANT and providing an XML Catalog and using strict schema validation, the XML Catalog is ignored. This was originally reported in JAXB-616 (and again in JAXB-795) and was supposed to have been fixed in 2.2.5. However, the fix does not solve the problem. Specifically, the fix was made in class:

      com.sun.tools.xjc.reader.xmlschema.parser.SchemaConstraintChecker

      Notice the commented out line though... which means the EntityResolver isn't actually used:

      private static Source[] [More ...] getSchemaSource(InputSource[] schemas, EntityResolver entityResolver) throws SAXException {
      SAXSource[] sources = new SAXSource[schemas.length];
      for (int i = 0; i < schemas.length; i++)

      { sources[i] = new SAXSource(schemas[i]); // sources[i].getXMLReader().setEntityResolver(entityResolver); }

      return sources;
      }

      I'm not an expert with respect to JAXB, but after examining some other code I modified it as follows, recompiled, and this does resolve the problem (at least for me):

      private static Source[] getSchemaSource(InputSource[] schemas, EntityResolver entityResolver) throws SAXException {
      SAXSource[] sources = new SAXSource[schemas.length];
      for (int i = 0; i < schemas.length; i++) {
      sources[i] = new SAXSource(schemas[i]);
      if (entityResolver != null)
      {
      XMLReader reader = sources[i].getXMLReader();
      if (reader == null)
      {
      SAXParserFactory spFactory = SAXParserFactory.newInstance();
      spFactory.setNamespaceAware(true);
      try

      { reader = spFactory.newSAXParser().getXMLReader(); }

      catch (ParserConfigurationException ex)

      { throw new SAXException(ex); }

      }
      reader.setEntityResolver(entityResolver);
      sources[i].setXMLReader(reader);
      }
      }
      return sources;
      }

        Activity

        Hide
        christopherincanada added a comment -

        Here is an improved version of the fix that is more efficient:

        private static Source[] getSchemaSource(InputSource[] schemas, EntityResolver entityResolver) throws SAXException
        {
        SAXSource[] sources = new SAXSource[schemas.length];
        for (int i = 0; i < schemas.length; i++)
        {
        XMLReader reader = null;
        if (entityResolver != null)
        {
        try

        { reader = getSAXParserFactory().newSAXParser().getXMLReader(); }

        catch (ParserConfigurationException ex)

        { throw new SAXException(ex); }

        reader.setEntityResolver(entityResolver);
        }
        sources[i] = new SAXSource(reader, schemas[i]);
        }
        return sources;
        }

        private static SAXParserFactory spFactory;

        private static SAXParserFactory getSAXParserFactory()
        {
        if (spFactory == null)

        { spFactory = SAXParserFactory.newInstance(); spFactory.setNamespaceAware(true); }

        return spFactory;
        }

        Show
        christopherincanada added a comment - Here is an improved version of the fix that is more efficient: private static Source[] getSchemaSource(InputSource[] schemas, EntityResolver entityResolver) throws SAXException { SAXSource[] sources = new SAXSource [schemas.length] ; for (int i = 0; i < schemas.length; i++) { XMLReader reader = null; if (entityResolver != null) { try { reader = getSAXParserFactory().newSAXParser().getXMLReader(); } catch (ParserConfigurationException ex) { throw new SAXException(ex); } reader.setEntityResolver(entityResolver); } sources [i] = new SAXSource(reader, schemas [i] ); } return sources; } private static SAXParserFactory spFactory; private static SAXParserFactory getSAXParserFactory() { if (spFactory == null) { spFactory = SAXParserFactory.newInstance(); spFactory.setNamespaceAware(true); } return spFactory; }

          People

          • Assignee:
            Iaroslav Savytskyi
            Reporter:
            christopherincanada
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated: