Announcement Announcement Module
Collapse
No announcement yet.
Custom namespace extension - no declaration found exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom namespace extension - no declaration found exception

    Hi,

    I've created simple testing application according the example in Extensible XML authoring chapter of reference manual (3.0.0.RC1) to test how to use custom namespace extension, but I'm not able to get it work due to SAX parse exception - it complains that it cannot find declaration of my newly created namespace (I've updated now to 3.0.0.RC2, but had the same problem also with 3.0.0.RC1)

    Code:
    Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'myns:dateformat'.
            at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
            at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
            at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
            at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
            at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:410)
            at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3165)
            at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1898)
            at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.emptyElement(XMLSchemaValidator.java:705)
            at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:377)
            at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2747)
            at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
            at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
            at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
            at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
            at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
            at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
            at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:225)
            at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283)
            at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)
            at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:370)
            ... 46 more

    I'm using Netbeans IDE - and if I run validation of my application-context.xml with custom namespace declaration - it passes (all xml files are valid and properly linked) - so I expect it is not just typo in configuration. I tried to find org/springframework/samples/xml (as referenced in manual) but with no success - if you could give me the link to this app, it would be very helpfull.

    Anyway, here is my current complete configuration:

    META-INF/spring.handlers
    Code:
    http\://www.mycompany.com/schema/myns=cz.alvap.misc.customns.MyNamespaceHandler
    META-INF/spring.schemas
    Code:
    http\://www.mycompany.com/schema/myns/myns.xsd=cz/alvap/misc/customns/myns.xsd
    cz.alvap.misc.customns.MyNamespaceHandler

    Code:
    package cz.alvap.misc.customns;
    import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
    public class MyNamespaceHandler extends NamespaceHandlerSupport {
        public void init() {
            registerBeanDefinitionParser("dateformat", new SimpleDateFormatBeanDefinitionParser());
        }
    }
    cz.alvap.misc.customns.SimpleDateFormatBeanDefinit ionParser

    Code:
    package cz.alvap.misc.customns;
    
    import org.springframework.beans.factory.support.BeanDefinitionBuilder;
    import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
    import org.springframework.util.StringUtils;
    import org.w3c.dom.Element;
    
    import java.text.SimpleDateFormat;
    
    public class SimpleDateFormatBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
    
       protected Class getBeanClass(Element element) {
          return SimpleDateFormat.class;
       }
    
       protected void doParse(Element element, BeanDefinitionBuilder bean) {
          // this will never be null since the schema explicitly requires that a value be supplied
          String pattern = element.getAttribute("pattern");
          bean.addConstructorArg(pattern);
    
          // this however is an optional property
          String lenient = element.getAttribute("lenient");
          if (StringUtils.hasText(lenient)) {
             bean.addPropertyValue("lenient", Boolean.valueOf(lenient));
          }
       }
    }
    cz/alvap/misc/customns/myns.xsd

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns="http://www.mycompany.com/schema/myns"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:beans="http://www.springframework.org/schema/beans"
        targetNamespace="http://www.mycompany.com/schema/myns"
        elementFormDefault="qualified"
        attributeFormDefault="unqualified">
    
       <xsd:import namespace="http://www.springframework.org/schema/beans"/>
    
       <xsd:element name="dateformat">
          <xsd:complexType>
             <xsd:complexContent>
                <xsd:extension base="beans:identifiedType">
                <xsd:attribute name="lenient" type="xsd:boolean"/>
                   <xsd:attribute name="pattern" type="xsd:string" use="required"/>
                </xsd:extension>
             </xsd:complexContent>
          </xsd:complexType>
       </xsd:element>
    </xsd:schema>
    application-context.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:myns="http://www.mycompany.com/schema/myns"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.mycompany.com/schema/myns http://www.mycompany.com/schema/myns/myns.xsd">
        <myns:dateformat id="dateFormat"
        pattern="yyyy-MM-dd HH:mm"
        lenient="true"/>
    </beans>
    Please, advise. Thank you very much.
    Best regards, Pavla Novakova

  • #2
    Most probable reason is that META-INF with your custom handler and schema is not put to classpath. Anyway, you can sort that out easily via debugging 'resolveEntity()' method of PluggableSchemaResolver class.

    Comment


    • #3
      Hi Denis,

      thanks for kick me right direction ... finally I found, that the problem is in my maven configuration only - .xsd files placed in sources directory were not included in target directory during build, just changing maven configuration to include non-java sources to target classes directory resolved it.

      Best regards, Pavla

      Comment


      • #4
        Pavla,

        Welcome. I'd also suggest you to put META-INF folder with schema and handlers definitions to 'src/main/resources' directory. That allow to automatically copy the stuff to target 'classes' directory by Maven.

        Regards, Denis.

        Comment

        Working...
        X