Dynamically insert SVG graphics in the XML passed to FOP

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Dynamically insert SVG graphics in the XML passed to FOP

Pierre Caron
Hello,

I have a rather trivial problem that I haven't found a solution yet.

I want to insert dynamically generated SVG code Inside the XML data which is supplied to FOP.

To make things simpler, suppose you start from the ExampleObj2PDF found in FOP Embedding page (https://xmlgraphics.apache.org/fop/2.2/embedding.html#ExampleObj2PDF) and that you add a method in the ProjectTeam entity that generates a SVG String :


public String getDynamicSVG()  {
     return " <svg:svg width=\"20\" height=\"20\">\n" + ... + "</svg:svg>";
}



And that this method would be called by the following line inside ProjectTeamXMLReader class

handler.element("SVGdynamique", projectMember.getDynamicSVG());


The question is : how to prevent the SVG tags from being escaped into &lt; and &gt;?  Below is the XML code I'm getting :

...
<svgDynamique> &lt;svg:svg width="20" height="20"&gt;
  &lt;svg:g style="fill:red; stroke:#000000"&gt;
     &lt;svg:rect x="0" y="0" width="15" height="15"/&gt;
     &lt;svg:rect x="5" y="5" width="15" height="15"/&gt;
  &lt;/svg:g&gt;
&lt;/svg:svg&gt;</svgDynamique>
...

In the final step, the SVG string would be merged by the projectteam2fo.xsl like this :

                <fo:block space-before.optimum="15pt"> <fo:instream-foreign-object>
                        <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20">
                            <xsl:copy-of select="svgDynamique" />
                        </svg>
                    </fo:instream-foreign-object>]
                </fo:block>

Is there a way to prevent the escaping?

Note that the SVG tags get escaped during the XML processing by SAX not during the insertion into the xsl file.

Any help would be greatly appreciated!

Pierre Caron

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Dynamically insert SVG graphics in the XML passed to FOP

Matthias Reischenbacher

Hi,

you should try to write out the SVG Elements with the following method (instead of using a string):

handler.startElement(String namespaceURI, String localName,
                        String qName, Attributes atts)

handler.endElement(String namespaceURI, String localName,
                        String qName)

e.g.

handler.startElement("http://www.w3.org/2000/svg", "svg", "svg:svg", atts)

handler.startElement("http://www.w3.org/2000/svg", "svg", "svg:g", atts)

handler.endElement("http://www.w3.org/2000/svg", "svg", "svg:svg")

handler.endElement("http://www.w3.org/2000/svg", "svg", "svg:g")

etc.

Best regards,
Matthias

On 21.06.2017 13:53, Pierre Caron wrote:
Hello,

I have a rather trivial problem that I haven't found a solution yet.

I want to insert dynamically generated SVG code Inside the XML data which is supplied to FOP.

To make things simpler, suppose you start from the ExampleObj2PDF found in FOP Embedding page (https://xmlgraphics.apache.org/fop/2.2/embedding.html#ExampleObj2PDF) and that you add a method in the ProjectTeam entity that generates a SVG String :


public String getDynamicSVG()  {
     return " <svg:svg width=\"20\" height=\"20\">\n" + ... + "</svg:svg>";
}



And that this method would be called by the following line inside ProjectTeamXMLReader class

handler.element("SVGdynamique", projectMember.getDynamicSVG());

The question is : how to prevent the SVG tags from being escaped into &lt; and &gt;?  Below is the XML code I'm getting :

...
<svgDynamique> &lt;svg:svg width="20" height="20"&gt;
  &lt;svg:g style="fill:red; stroke:#000000"&gt;
     &lt;svg:rect x="0" y="0" width="15" height="15"/&gt;
     &lt;svg:rect x="5" y="5" width="15" height="15"/&gt;
  &lt;/svg:g&gt;
&lt;/svg:svg&gt;</svgDynamique>
...

In the final step, the SVG string would be merged by the projectteam2fo.xsl like this :

                <fo:block space-before.optimum="15pt"> <fo:instream-foreign-object>
                        <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20">
                            <xsl:copy-of select="svgDynamique" />
                        </svg>
                    </fo:instream-foreign-object>]
                </fo:block>

Is there a way to prevent the escaping?

Note that the SVG tags get escaped during the XML processing by SAX not during the insertion into the xsl file.

Any help would be greatly appreciated!

Pierre Caron


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Dynamically insert SVG graphics in the XML passed to FOP

Pierre Caron
Thank you Matthias. I was able to generate valid SVG code with this :

        handler.startElement("svgDynamique");
            handler.startElement("http://www.w3.org/2000/svg", "svg", "svg width=\"20\" height=\"20\"", EMPTY_ATTS);
            handler.startElement("g style=\"fill:red; stroke:#000000\"");
            handler.element("rect x=\"0\" y=\"0\" width=\"15\" height=\"15\"", "");
            handler.element("rect x=\"5\" y=\"5\" width=\"15\" height=\"15\"", "");
            handler.endElement("g");
            handler.endElement("http://www.w3.org/2000/svg", "svg", "svg");
        handler.endElement("svgDynamique");

which yielded :

                        <svgDynamique>
                                <svg width="20" height="20" xmlns="http://www.w3.org/2000/svg">
                                        <g style="fill:red; stroke:#000000">
                                                <rect x="0" y="0" width="15" height="15"/>
                                                <rect x="5" y="5" width="15" height="15"/>
                                        </g>
                                </svg>
                        </svgDynamique>

Now, the problem is inserting this code in the fo template.  I have tried the following :

            <fo:table-cell border-width="0.25mm" border-style="solid">
                <fo:block space-before.optimum="15pt">
                    <fo:instream-foreign-object>
                        <xsl:value-of select="svgDynamique" disable-output-escaping="yes" />
                    </fo:instream-foreign-object>]
                </fo:block>
            </fo:table-cell>

but I get the following message which suggest that the SVG code isn't present :

org.apache.fop.fo.ValidationException: "fo:instream-foreign-object" is missing child elements. Required content model: one (1) non-XSL namespace child (No context info available)

I have also tried without success either :

 <xsl:copy-of select="svgDynamique"  />

My second question is : if the SVG is generated through Batik, how do you embed it in the fo file?

Again, thank you very much!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Dynamically insert SVG graphics in the XML passed to FOP

Matthias Reischenbacher
Hi,

On 24.06.2017 00:50, Pierre Caron wrote:
> Thank you Matthias. I was able to generate valid SVG code with this :
>
>         handler.startElement("svgDynamique");
>             handler.startElement("http://www.w3.org/2000/svg", "svg", "svg
> width=\"20\" height=\"20\"", EMPTY_ATTS);
>             handler.startElement("g style=\"fill:red; stroke:#000000\"");
Don't use handler.startElement witout specifying a namespace and the
attributes (such as "style") should be specified as last parameter of
the startElement method call.
>             handler.element("rect x=\"0\" y=\"0\" width=\"15\"
> height=\"15\"", "");
>             handler.element("rect x=\"5\" y=\"5\" width=\"15\"
> height=\"15\"", "");
Same here. Use startElement with namespace and fix attribute.

>             handler.endElement("g");
>             handler.endElement("http://www.w3.org/2000/svg", "svg", "svg");
>         handler.endElement("svgDynamique");
>
> which yielded :
>
> <svgDynamique>
> <svg width="20" height="20" xmlns="http://www.w3.org/2000/svg">
> <g style="fill:red; stroke:#000000">
> <rect x="0" y="0" width="15" height="15"/>
> <rect x="5" y="5" width="15" height="15"/>
> </g>
> </svg>
> </svgDynamique>
>
> Now, the problem is inserting this code in the fo template.  I have tried
> the following :
>
>             <fo:table-cell border-width="0.25mm" border-style="solid">
>                 <fo:block space-before.optimum="15pt">
>                     <fo:instream-foreign-object>
>                         <xsl:value-of select="svgDynamique"
> disable-output-escaping="yes" />
>                     </fo:instream-foreign-object>]
>                 </fo:block>
>             </fo:table-cell>
>
> but I get the following message which suggest that the SVG code isn't
> present :
>
> org.apache.fop.fo.ValidationException: "fo:instream-foreign-object" is
> missing child elements. Required content model: one (1) non-XSL namespace
> child (No context info available)
>
> I have also tried without success either :
>
>  <xsl:copy-of select="svgDynamique"  />
<xsl:copy-of select="svgDynamique/*"/> should work.
> My second question is : if the SVG is generated through Batik, how do you
> embed it in the fo file?
You will need to parse the SVG file and insert it into the FO file via
DOM operations or you use the fo:external-graphic element and specify as
source the SVG file.

Best regards,
Matthias

> Again, thank you very much!
>
>
>
> --
> View this message in context: http://apache-fop.1065347.n5.nabble.com/Dynamically-insert-SVG-graphics-in-the-XML-passed-to-FOP-tp45237p45239.html
> Sent from the FOP - Users mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Dynamically insert SVG graphics in the XML passed to FOP

Szeak (Register Man)
Hi,

<xsl:copy-of select="svgDynamique/*"/>

may select nodes only under svgDynamique element, and not svgDynamique
itself. If don't work try:

<xsl:copy-of select="//svgDynamique"/>

or

<xsl:copy-of select="//svgDynamique|svgDynamique/*"/>

Best regards, szeak

2017-06-26 12:41 keltezéssel, Matthias Reischenbacher írta:

> Hi,
>
> On 24.06.2017 00:50, Pierre Caron wrote:
>> Thank you Matthias. I was able to generate valid SVG code with this :
>>
>>          handler.startElement("svgDynamique");
>>              handler.startElement("http://www.w3.org/2000/svg", "svg", "svg
>> width=\"20\" height=\"20\"", EMPTY_ATTS);
>>              handler.startElement("g style=\"fill:red; stroke:#000000\"");
> Don't use handler.startElement witout specifying a namespace and the
> attributes (such as "style") should be specified as last parameter of
> the startElement method call.
>>              handler.element("rect x=\"0\" y=\"0\" width=\"15\"
>> height=\"15\"", "");
>>              handler.element("rect x=\"5\" y=\"5\" width=\"15\"
>> height=\"15\"", "");
> Same here. Use startElement with namespace and fix attribute.
>>              handler.endElement("g");
>>              handler.endElement("http://www.w3.org/2000/svg", "svg", "svg");
>>          handler.endElement("svgDynamique");
>>
>> which yielded :
>>
>> <svgDynamique>
>> <svg width="20" height="20" xmlns="http://www.w3.org/2000/svg">
>> <g style="fill:red; stroke:#000000">
>> <rect x="0" y="0" width="15" height="15"/>
>> <rect x="5" y="5" width="15" height="15"/>
>> </g>
>> </svg>
>> </svgDynamique>
>>
>> Now, the problem is inserting this code in the fo template.  I have tried
>> the following :
>>
>>              <fo:table-cell border-width="0.25mm" border-style="solid">
>>                  <fo:block space-before.optimum="15pt">
>>                      <fo:instream-foreign-object>
>>                          <xsl:value-of select="svgDynamique"
>> disable-output-escaping="yes" />
>>                      </fo:instream-foreign-object>]
>>                  </fo:block>
>>              </fo:table-cell>
>>
>> but I get the following message which suggest that the SVG code isn't
>> present :
>>
>> org.apache.fop.fo.ValidationException: "fo:instream-foreign-object" is
>> missing child elements. Required content model: one (1) non-XSL namespace
>> child (No context info available)
>>
>> I have also tried without success either :
>>
>>   <xsl:copy-of select="svgDynamique"  />
> <xsl:copy-of select="svgDynamique/*"/> should work.
>> My second question is : if the SVG is generated through Batik, how do you
>> embed it in the fo file?
> You will need to parse the SVG file and insert it into the FO file via
> DOM operations or you use the fo:external-graphic element and specify as
> source the SVG file.
>
> Best regards,
> Matthias
>
>> Again, thank you very much!
>>
>>
>>
>> --
>> View this message in context: http://apache-fop.1065347.n5.nabble.com/Dynamically-insert-SVG-graphics-in-the-XML-passed-to-FOP-tp45237p45239.html
>> Sent from the FOP - Users mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Dynamically insert SVG graphics in the XML passed to FOP

Szeak (Register Man)
Sorry, what i wrote is totally wrong. :) you don't need svgDynamique
element :)

sorry again

2017-06-26 13:04 keltezéssel, Szeak (Register Man) írta:

> Hi,
>
> <xsl:copy-of select="svgDynamique/*"/>
>
> may select nodes only under svgDynamique element, and not svgDynamique
> itself. If don't work try:
>
> <xsl:copy-of select="//svgDynamique"/>
>
> or
>
> <xsl:copy-of select="//svgDynamique|svgDynamique/*"/>
>
> Best regards, szeak
>
> 2017-06-26 12:41 keltezéssel, Matthias Reischenbacher írta:
>> Hi,
>>
>> On 24.06.2017 00:50, Pierre Caron wrote:
>>> Thank you Matthias. I was able to generate valid SVG code with this :
>>>
>>>          handler.startElement("svgDynamique");
>>> handler.startElement("http://www.w3.org/2000/svg", "svg", "svg
>>> width=\"20\" height=\"20\"", EMPTY_ATTS);
>>>              handler.startElement("g style=\"fill:red;
>>> stroke:#000000\"");
>> Don't use handler.startElement witout specifying a namespace and the
>> attributes (such as "style") should be specified as last parameter of
>> the startElement method call.
>>>              handler.element("rect x=\"0\" y=\"0\" width=\"15\"
>>> height=\"15\"", "");
>>>              handler.element("rect x=\"5\" y=\"5\" width=\"15\"
>>> height=\"15\"", "");
>> Same here. Use startElement with namespace and fix attribute.
>>>              handler.endElement("g");
>>>              handler.endElement("http://www.w3.org/2000/svg", "svg",
>>> "svg");
>>>          handler.endElement("svgDynamique");
>>>
>>> which yielded :
>>>
>>>             <svgDynamique>
>>>                 <svg width="20" height="20"
>>> xmlns="http://www.w3.org/2000/svg">
>>>                     <g style="fill:red; stroke:#000000">
>>>                         <rect x="0" y="0" width="15" height="15"/>
>>>                         <rect x="5" y="5" width="15" height="15"/>
>>>                     </g>
>>>                 </svg>
>>>             </svgDynamique>
>>>
>>> Now, the problem is inserting this code in the fo template.  I have
>>> tried
>>> the following :
>>>
>>>              <fo:table-cell border-width="0.25mm" border-style="solid">
>>>                  <fo:block space-before.optimum="15pt">
>>>                      <fo:instream-foreign-object>
>>>                          <xsl:value-of select="svgDynamique"
>>> disable-output-escaping="yes" />
>>>                      </fo:instream-foreign-object>]
>>>                  </fo:block>
>>>              </fo:table-cell>
>>>
>>> but I get the following message which suggest that the SVG code isn't
>>> present :
>>>
>>> org.apache.fop.fo.ValidationException: "fo:instream-foreign-object" is
>>> missing child elements. Required content model: one (1) non-XSL
>>> namespace
>>> child (No context info available)
>>>
>>> I have also tried without success either :
>>>
>>>   <xsl:copy-of select="svgDynamique"  />
>> <xsl:copy-of select="svgDynamique/*"/> should work.
>>> My second question is : if the SVG is generated through Batik, how
>>> do you
>>> embed it in the fo file?
>> You will need to parse the SVG file and insert it into the FO file via
>> DOM operations or you use the fo:external-graphic element and specify as
>> source the SVG file.
>>
>> Best regards,
>> Matthias
>>
>>> Again, thank you very much!
>>>
>>>
>>>
>>> --
>>> View this message in context:
>>> http://apache-fop.1065347.n5.nabble.com/Dynamically-insert-SVG-graphics-in-the-XML-passed-to-FOP-tp45237p45239.html
>>> Sent from the FOP - Users mailing list archive at Nabble.com.
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Loading...