Quantcast

Correctness of my own Receiver

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Correctness of my own Receiver

cmarchand

Hello,


I've written my own receiver (extending ProxyReceiver), and I have a problem using it, when I have a Serializer where "indent" property is "yes". I do not have the exception when indent="no".

I think I miss calling a super.xxx() somewhere, but no idea where...

Here is the Exception stack :

Caused by: java.lang.RuntimeException: Internal error evaluating template rule  at line 12 in module identity.xsl
    at net.sf.saxon.expr.instruct.TemplateRule.applyLeavingTail(TemplateRule.java:369)

    at net.sf.saxon.trans.Mode.applyTemplates(Mode.java:456)

    at net.sf.saxon.expr.instruct.ApplyTemplates.apply(ApplyTemplates.java:301)

    at net.sf.saxon.expr.instruct.ApplyTemplates.process(ApplyTemplates.java:254)

    at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:366)

    at net.sf.saxon.expr.instruct.Copy.processLeavingTail(Copy.java:435)

    at net.sf.saxon.expr.instruct.TemplateRule.applyLeavingTail(TemplateRule.java:356)

    ... 59 more

Caused by: java.lang.NullPointerException

    at net.sf.saxon.tree.util.AttributeCollectionImpl.addAttribute(AttributeCollectionImpl.java:143)

    at net.sf.saxon.serialize.XMLIndenter.attribute(XMLIndenter.java:154)

    at net.sf.saxon.event.ProxyReceiver.attribute(ProxyReceiver.java:165)

    at net.sf.saxon.event.ComplexContentOutputter.startContent(ComplexContentOutputter.java:682)

    at net.sf.saxon.event.ProxyReceiver.startContent(ProxyReceiver.java:177)

    at net.sf.saxon.event.ProxyReceiver.startContent(ProxyReceiver.java:177)

    at eu.els.sie.efl.chaine.magneto.inject.InjectStepReceiver.startContent(InjectStepReceiver.java:116)

    at net.sf.saxon.event.NamespaceReducer.startContent(NamespaceReducer.java:216)

    at net.sf.saxon.event.ComplexContentOutputter.startContent(ComplexContentOutputter.java:689)

    at net.sf.saxon.event.ComplexContentOutputter.characters(ComplexContentOutputter.java:262)

    at net.sf.saxon.expr.instruct.Copy.processLeavingTail(Copy.java:451)

    at net.sf.saxon.expr.instruct.TemplateRule.applyLeavingTail(TemplateRule.java:356)

    ... 65 more


Running code is

Serializer serializer = processor.newSerializer(...);
serializer.setOutputProperty(Serializer.Property.INDENT,"yes");
Destination myJavaStep= new InjectStep();
myJavaStep.setDestination(serializer);
xslIdentity.setDestination(myJavaStep);
xslIdentity.setInitialContextNode(sourceDocument);
xslIdentity.transform();


InjectStep is :

public class InjectStep extends StepJava {
    private Receiver underlyingReceiver;
    @Override
    public Receiver getReceiver(Configuration config) throws SaxonApiException {
        String fileName = getParameter(FILE_NAME).toString();
        underlyingReceiver = new InjectStepReceiver(getNextReceiver(config), fileName);
        return underlyingReceiver;
    }
}

And InjectStepReceiver creates attributes on elements, based on link-resolving in a database. The code is :

public class InjectStepReceiver extends ProxyReceiver {
    private ReceiverState receiverState;
    private final String fileName;
    public InjectStepReceiver(Receiver nextReceiver, String fileName) {
        super(nextReceiver);
        this.fileName = fileName;
    }

    @Override
    public void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) throws XPathException {
        super.startElement(elemName, typeCode, location, properties);
        receiverState = new ReceiverState(new Element(elemName, typeCode, location, properties));
    }

    @Override
    public void attribute(NodeName nameCode, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException {
        super.attribute(nameCode, typeCode, value, locationId, properties);
        receiverState.addAttribute(new Attribute(nameCode, typeCode, value, locationId, properties));
    }

    @Override
    public void startContent() throws XPathException {
        if (receiverState.getElement().hasAttribute("is-logic-link")) {
            Link link = new Link(receiverState.getElement().getAttributes());

            if (link.getQualityEnum().equals(LinkQualityEnum.deterministic)) {
                Document result;
                switch (link.getModeEnum()) {
                    case text:
                        result = Context.INSTANCE.getSourceTexteDao().solvable(link);
                        break;
                    case jp:
                        result = Context.INSTANCE.getSourceReferenceDao().solvable(link);
                        break;
                    default:
                        result = Context.INSTANCE.getDocDao().solvable(link);
                        break;
                }

                if (result != null) {
                    link.setSolved(true);
                    nextReceiver.attribute(new FingerprintedQName("", "", "linkid"), AnySimpleType.getInstance(), result.getString("id"), null, 0);
                    nextReceiver.attribute(new FingerprintedQName("", "", "solved"), AnySimpleType.getInstance(), "true", null, 0);

                } else {
                    link.setSolved(false);
                    Attribute solved = new Attribute("solved", "false");
                    nextReceiver.attribute(solved.getNameCode(), solved.getTypeCode(), solved.getValue(), solved.getLocationId(), solved.getProperties());
                }

            } else {
                link.setSolved(false);
                Attribute solved = new Attribute("solved", "false");
                nextReceiver.attribute(solved.getNameCode(), solved.getTypeCode(), solved.getValue(), solved.getLocationId(), solved.getProperties());
            }

            link.addProperty("file-name", new Attribute("file-name", fileName));
            Context.INSTANCE.getLinkDao().insert(link);
        }
        super.startContent();
    }
}

The problem occurs for elements where @is-logic-link exists, link is deterministic, result is not null.

Thanks in advance,
Christophe


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Correctness of my own Receiver

Michael Kay
The NPE:

Caused by: java.lang.NullPointerException
    at net.sf.saxon.tree.util.AttributeCollectionImpl.addAttribute(AttributeCollectionImpl.java:143)

is caused by the locationId passed to addAttribute() being null, which in turn is caused by your call 

nextReceiver.attribute(new FingerprintedQName("", "", "linkid"), AnySimpleType.getInstance(), result.getString("id"), null, 0);
                    
where the penultimate argument is a locationId.

Instead, I suggest you pass ExplicitlLocation.UNKNOWN_LOCATION.

Michael Kay
Saxonica


On 10 Apr 2017, at 16:28, Christophe Marchand <[hidden email]> wrote:

Hello,


I've written my own receiver (extending ProxyReceiver), and I have a problem using it, when I have a Serializer where "indent" property is "yes". I do not have the exception when indent="no".

I think I miss calling a super.xxx() somewhere, but no idea where...

Here is the Exception stack :

Caused by: java.lang.RuntimeException: Internal error evaluating template rule  at line 12 in module identity.xsl
    at net.sf.saxon.expr.instruct.TemplateRule.applyLeavingTail(TemplateRule.java:369)

    at net.sf.saxon.trans.Mode.applyTemplates(Mode.java:456)

    at net.sf.saxon.expr.instruct.ApplyTemplates.apply(ApplyTemplates.java:301)

    at net.sf.saxon.expr.instruct.ApplyTemplates.process(ApplyTemplates.java:254)

    at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:366)

    at net.sf.saxon.expr.instruct.Copy.processLeavingTail(Copy.java:435)

    at net.sf.saxon.expr.instruct.TemplateRule.applyLeavingTail(TemplateRule.java:356)

    ... 59 more

Caused by: java.lang.NullPointerException

    at net.sf.saxon.tree.util.AttributeCollectionImpl.addAttribute(AttributeCollectionImpl.java:143)

    at net.sf.saxon.serialize.XMLIndenter.attribute(XMLIndenter.java:154)

    at net.sf.saxon.event.ProxyReceiver.attribute(ProxyReceiver.java:165)

    at net.sf.saxon.event.ComplexContentOutputter.startContent(ComplexContentOutputter.java:682)

    at net.sf.saxon.event.ProxyReceiver.startContent(ProxyReceiver.java:177)

    at net.sf.saxon.event.ProxyReceiver.startContent(ProxyReceiver.java:177)

    at eu.els.sie.efl.chaine.magneto.inject.InjectStepReceiver.startContent(InjectStepReceiver.java:116)

    at net.sf.saxon.event.NamespaceReducer.startContent(NamespaceReducer.java:216)

    at net.sf.saxon.event.ComplexContentOutputter.startContent(ComplexContentOutputter.java:689)

    at net.sf.saxon.event.ComplexContentOutputter.characters(ComplexContentOutputter.java:262)

    at net.sf.saxon.expr.instruct.Copy.processLeavingTail(Copy.java:451)

    at net.sf.saxon.expr.instruct.TemplateRule.applyLeavingTail(TemplateRule.java:356)

    ... 65 more


Running code is

Serializer serializer = processor.newSerializer(...);
serializer.setOutputProperty(Serializer.Property.INDENT,"yes");
Destination myJavaStep= new InjectStep();
myJavaStep.setDestination(serializer);
xslIdentity.setDestination(myJavaStep);
xslIdentity.setInitialContextNode(sourceDocument);
xslIdentity.transform();


InjectStep is :

public class InjectStep extends StepJava {
    private Receiver underlyingReceiver;
    @Override
    public Receiver getReceiver(Configuration config) throws SaxonApiException {
        String fileName = getParameter(FILE_NAME).toString();
        underlyingReceiver = new InjectStepReceiver(getNextReceiver(config), fileName);
        return underlyingReceiver;
    }
}

And InjectStepReceiver creates attributes on elements, based on link-resolving in a database. The code is :

public class InjectStepReceiver extends ProxyReceiver {
    private ReceiverState receiverState;
    private final String fileName;
    public InjectStepReceiver(Receiver nextReceiver, String fileName) {
        super(nextReceiver);
        this.fileName = fileName;
    }

    @Override
    public void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) throws XPathException {
        super.startElement(elemName, typeCode, location, properties);
        receiverState = new ReceiverState(new Element(elemName, typeCode, location, properties));
    }

    @Override
    public void attribute(NodeName nameCode, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException {
        super.attribute(nameCode, typeCode, value, locationId, properties);
        receiverState.addAttribute(new Attribute(nameCode, typeCode, value, locationId, properties));
    }

    @Override
    public void startContent() throws XPathException {
        if (receiverState.getElement().hasAttribute("is-logic-link")) {
            Link link = new Link(receiverState.getElement().getAttributes());

            if (link.getQualityEnum().equals(LinkQualityEnum.deterministic)) {
                Document result;
                switch (link.getModeEnum()) {
                    case text:
                        result = Context.INSTANCE.getSourceTexteDao().solvable(link);
                        break;
                    case jp:
                        result = Context.INSTANCE.getSourceReferenceDao().solvable(link);
                        break;
                    default:
                        result = Context.INSTANCE.getDocDao().solvable(link);
                        break;
                }

                if (result != null) {
                    link.setSolved(true);
                    nextReceiver.attribute(new FingerprintedQName("", "", "linkid"), AnySimpleType.getInstance(), result.getString("id"), null, 0);
                    nextReceiver.attribute(new FingerprintedQName("", "", "solved"), AnySimpleType.getInstance(), "true", null, 0);

                } else {
                    link.setSolved(false);
                    Attribute solved = new Attribute("solved", "false");
                    nextReceiver.attribute(solved.getNameCode(), solved.getTypeCode(), solved.getValue(), solved.getLocationId(), solved.getProperties());
                }

            } else {
                link.setSolved(false);
                Attribute solved = new Attribute("solved", "false");
                nextReceiver.attribute(solved.getNameCode(), solved.getTypeCode(), solved.getValue(), solved.getLocationId(), solved.getProperties());
            }

            link.addProperty("file-name", new Attribute("file-name", fileName));
            Context.INSTANCE.getLinkDao().insert(link);
        }
        super.startContent();
    }
}

The problem occurs for elements where @is-logic-link exists, link is deterministic, result is not null.

Thanks in advance,
Christophe

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Loading...