A document has been updated:
http://cocoon.zones.apache.org/daisy/legacydocs/635.html
Document ID: 635
Branch: main
Language: default
Name: Writing a Cocoon 2 generator (unchanged)
Document Type: Document (unchanged)
Updated on: 10/14/05 2:01:50 PM
Updated by: Helma van der Linden
A new version has been created, state: publish
Parts
=====
Content
-------
This part has been updated.
Mime type: text/xml (unchanged)
File name: (unchanged)
Size: 63523 bytes (previous version: 63424 bytes)
Content diff:
(545 equal lines skipped)
<p>As a first test we decided to parse a string containing the following XML
content and feed the SAX events to the XMLConsumer:</p>
--- <pre>
--- <doc>My first Cocoon 2 generator!</doc>
+++ <pre><doc>My first Cocoon 2 generator!</doc>
</pre>
<p>First, we will give our code and then we will explain what it does and
why we
made these choices.</p>
--- <pre>
--- package test;
+++ <pre>package test;
import java.io.IOException;
import java.io.StringReader;
(37 equal lines skipped)
method, we define the variable <strong>message</strong> containing the XML
content we want to generate SAX events for.</p>
--- <pre>
--- XMLReader xmlreader = XMLReaderFactory.createXMLReader();
+++ <pre>XMLReader xmlreader = XMLReaderFactory.createXMLReader();
</pre>
(6 equal lines skipped)
example of how this is done is provided after we have discussed the rest of
the
code.</p>
--- <pre>
--- xmlreader.setContentHandler(super.xmlConsumer);
+++ <pre>xmlreader.setContentHandler(super.xmlConsumer);
</pre>
<p>With this line of code, we tell the XMLReader which object will receive
the
SAX events that will be generated when parsing. You can see that we use
<strong>super.xmlConsumer</strong> to receive the SAX events.</p>
--- <pre>
--- InputSource source = new InputSource(new StringReader(message));
+++ <pre>InputSource source = new InputSource(new StringReader(message));
xmlreader.parse(source);
</pre>
(43 equal lines skipped)
</li>
</ul>
--- <pre>
--- javac -classpath .:jar/cocoon.jar:jar/xerces.jar \
+++ <pre>javac -classpath .:jar/cocoon.jar:jar/xerces.jar \
-d compiled test/MyGenerator.java
</pre>
(25 equal lines skipped)
<p>Under the <strong>map:pipelines</strong> element, we added the
following:</p>
--- <pre>
--- <map:pipeline>
+++ <pre><map:pipeline>
<map:match pattern="mygenerator.xml">
<map:generate type="mygenerator"/>
<map:serialize type="xml"/>
(22 equal lines skipped)
should always do when receiving an error that is not so clear) showed that
the
following exception was the cause of that fatal error:</p>
--- <pre>
--- ERROR (2002-03-27) 23:21.40:190 [sitemap] (/cocoon/)
+++ <pre>ERROR (2002-03-27) 23:21.40:190 [sitemap] (/cocoon/)
Thread-23/Handler: Error compiling sitemap
java.lang.NoClassDefFoundError:
org/apache/cocoon/generation/AbstractGenerator
(74 equal lines skipped)
saying that the server could not be reached. Looking at the xterm from
which we
started Tomcat, we saw the following error:</p>
--- <pre>
--- IGSEGV 11* segmentation violation
+++ <pre>IGSEGV 11* segmentation violation
si_signo [11]: SIGSEGV 11* segmentation violation
si_errno [0]: Success
si_code [128]: unknown siginfo
(24 equal lines skipped)
(Cocoon 2 had to recompile its sitemap and some other components) we got
the see
our XML file. Cocoon 2 produced the following XML document:</p>
--- <pre>
--- <?xml version="1.0" encoding="UTF-8"?>
+++ <pre><?xml version="1.0" encoding="UTF-8"?>
<doc>My first Cocoon 2 generator!</doc>
</pre>
(23 equal lines skipped)
solution that is used throughout Cocoon 2. We will give the code fragments
here
and we will then explain what it does.</p>
--- <pre>
--- ...
+++ <pre>...
import org.apache.excalibur.xml.sax.SAXParser;
...
SAXParser parser = null;
(82 equal lines skipped)
that should be implemented by a program that wishes to serve as a server for
<strong>MyGenerator</strong>. This interface looks like this:</p>
--- <pre>
--- package test;
+++ <pre>package test;
import java.rmi.Remote;
import java.rmi.RemoteException;
(29 equal lines skipped)
(<a href="http://www.jdom.org">JDOM.org</a>) for reading in a XML document
and
converting it to a String.</p>
--- <pre>
--- package test;
+++ <pre>package test;
import java.rmi.Naming;
import java.rmi.RemoteException;
(73 equal lines skipped)
The <strong>sayHello</strong> method just returns a string representing the
following XML fragment:</p>
--- <pre>
--- <doc>My First RMI Server!
+++ <pre><doc>My First RMI Server!
</pre>
(23 equal lines skipped)
connect to our RMI server and invoke its methods. Once again, we will first
give
our code and then explain what it does.</p>
--- <pre>
--- package test;
+++ <pre>package test;
import java.rmi.Naming;
import java.rmi.RemoteException;
(46 equal lines skipped)
the directory "/home/erwin/cocoon2/generator/", which has the subdirectory
"test/" containing our files, we execute the following commands:</p>
--- <pre>
--- javac -classpath .:jar/jdom.jar:jar/xerces.jar -d compiled/ test/*.java
+++ <pre>javac -classpath .:jar/jdom.jar:jar/xerces.jar -d compiled/ test/*.java
rmic -classpath .:jar/jdom.jar:jar/xerces.jar -d compiled/ test.Server
rmiregistry &amp;
java -classpath compiled/:jar/jdom.jar:jar/xerces.jar \
(7 equal lines skipped)
property or give it a wrong value, you are most likely to get the following
exception:</p>
--- <pre>
--- HelloImpl err: RemoteException occurred in server thread; nested exception
is:
+++ <pre>HelloImpl err: RemoteException occurred in server thread; nested
exception is:
java.rmi.UnmarshalException: error unmarshalling arguments;
nested exception is:
java.lang.ClassNotFoundException: test.Server_Stub
(22 equal lines skipped)
path from where we started the server application. The file index.xml
contains
the following information:</p>
--- <pre>
--- <?xml version="1.0"?>
+++ <pre><?xml version="1.0"?>
<document>
<title>This is a document</title>
<para>This is the first paragraph.</para>
(3 equal lines skipped)
<p>The client is started with the following command:</p>
--- <pre>
--- [erwin generator]$ java -classpath compiled/ test.Client
+++ <pre>[erwin generator]$ java -classpath compiled/ test.Client
</pre>
<p>This resulted in the following output:</p>
--- <pre>
--- <doc>My First RMI Server!</doc>
+++ <pre><doc>My First RMI Server!</doc>
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>This is a document</title>
(12 equal lines skipped)
error when starting your server application (depending on the configuration
in
your <strong>java.policy</strong> file):</p>
--- <pre>
--- HelloImpl err: access denied
+++ <pre>HelloImpl err: access denied
(java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
java.security.AccessControlException: access denied
(java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
(24 equal lines skipped)
assigned this value. If for instance the following is a small part of the
sitemap:</p>
--- <pre>
--- <map:match pattern="mygenerator.xml">
+++ <pre><map:match pattern="mygenerator.xml">
<map:generate type="mygenerator" src="example.xml"/>
<map:serialize type="xml"/>
</map:match>
(17 equal lines skipped)
<strong>generate</strong> element. Our sitemap fragment will then look like
this:</p>
--- <pre>
--- <map:match pattern="mygenerator.xml">
+++ <pre><map:match pattern="mygenerator.xml">
<map:generate type="mygenerator" src="example.xml">
<map:parameter name="host" value="myhost.com"/>
<map:parameter name="port" value="1099"/>
(47 equal lines skipped)
bit of thinking about how to code all this, we eventually wrote the
following
generator:</p>
--- <pre>
--- package test;
+++ <pre>package test;
// import the necessary classes from the java.io package
import java.io.IOException;
(113 equal lines skipped)
ServerFunctions interface, in the right place. For compiling, we used the
following command:</p>
--- <pre>
--- javac -classpath .:jar/xerces.jar:jar/cocoon.jar:jar/framework.jar: \
+++ <pre>javac -classpath .:jar/xerces.jar:jar/cocoon.jar:jar/framework.jar: \
jar/excalibur.jar:jar/exc-scratchpad.jar \
-d compiled/ test/ServerFunctions.java test/MyGenerator.java
(37 equal lines skipped)
since the deadline of our thesis is only three weeks a way at the time of
writing).</p>
+++ <h2>Related files</h2>
+++
+++ <p><a href="daisy:748">RMIGenerator.java</a><br/>
+++ <a href="daisy:749">ServerFunctions.java</a></p>
+++
</body>
</html>
Fields
======
no changes
Links
=====
no changes
Custom Fields
=============
no changes
Collections
===========
no changes