[opencms-dev] open questions about XML content

Mike Bachrynowski mikeb at tableau.co.uk
Thu Nov 11 18:44:07 CET 2004


I have a number of "generic" OpenCms schema that are capturing
information for variants of Docbook XML fragments (Chapters with one or
more Sections, media objects etc).  

I am attempting to write a single "generic" JSP script which detects the
schema and depending on the particular schema undertakes various
processing (spell checking, validation etc).  I have hacked together a
JSP script (see key parts below) that extracts all the information I
need.  The problem is that the code is not very elegant.  Is there an
easier way?  Are there other classes in the "core" that would simplify
the coding?  Is this how the designers thought the classes would be
used? 

I would appreciate comments as to how this code could be improved. 

========================================
========================================  
<%@page buffer="none" session="false" 
 import="java.util.*,
         org.opencms.file.*,
         org.opencms.jsp.*,
         org.opencms.main.*,
         org.opencms.util.*,
         org.opencms.xml.*,
         org.opencms.xml.content.* " 
%><%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" 
%><%

// Reference File Name
String mytemplate = "/system/modules/uk.co.tableau/templates/docbook-b";

// HTTPServlet values
String myRequestURI = request.getRequestURI();

// Cms Access
CmsJspActionElement    cms = new CmsJspActionElement(pageContext,
request, response);

// Find the XML and Namespace
CmsObject              myCmsObject       = cms.getCmsObject();
CmsRequestContext      myContext         =
myCmsObject.getRequestContext();
String                 myCollectorName   = "singleFile";
I_CmsResourceCollector myCollector       =
OpenCms.getResourceManager().getContentCollector( myCollectorName );
List                   myCollectorResult = myCollector.getResults(
myCmsObject, myCollectorName, myContext.getUri() );
// --- FOR MULTIPLE RESOURCES (needs different CollectorName) ---
//   CmsResource myCmsResource    = null;
//   String      myResourceName   = ""; 
//   int         myResourceLength = 0;   
//   for (int count=0; count < myCollectorResult.size(); count ++ ) {
//       myCmsResource = (CmsResource) myCollectorResult.get(count);
//       myResourceName   = myCmsResource.getName();
//       myResourceLength = myCmsResource.getLength();
//   }
CmsResource            myCmsResource     = (CmsResource)
myCollectorResult.get(0);
CmsFile                myCmsFile         = (CmsFile) myCmsResource;
A_CmsXmlDocument       myCmsXmlDocument  =
CmsXmlContentFactory.unmarshal( myCmsObject, myCmsFile );
String                 myContent         = myCmsXmlDocument.toString(); 

// Reformat the XML to escape tags (for output)     
String                 myContent1 = CmsStringUtil.substitute( myContent,
"<", "<");
String                 myContent2 = CmsStringUtil.substitute(
myContent1, ">", "><BR/>");

// Find the Namespace
String                 find_head         =
"noNamespaceSchemaLocation=\"";
String                 find_tail         = ".xsd\">";
String                 myNamespace       = myContent.substring( 
                                               myContent.indexOf(
find_head ) + find_head.length(), 
                                               myContent.indexOf(
find_tail ) + find_tail.length() - 2 );
// Cms Context Properties
String            myContextEncoding  = myContext.getEncoding();
String            myContextFolderUri = myContext.getFolderUri(); 
String            myContextSiteRoot  = myContext.getSiteRoot(); 
String            myContextVfsUri    = myContext.getUri(); 
Locale            myContextLocale    = myContext.getLocale();

// Properties
Map    props            = cms.properties();
String propsAll         = props.toString();
String propsTitle       = cms.property("Title");
String propsDescription = cms.property("Description");
String propsKeywords    = cms.property("Keywords");
 
// Elements
String elementAuthor           =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument, "Author",
myContextLocale, 0, request );
String elementRelease          =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument, "Release",
myContextLocale, 0, request );
String elementCategories       =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument,
"Categories",       myContextLocale, 0, request );
String elementRules            =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument, "Rules",
myContextLocale, 0, request );

String elementChapterTitle     =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument,
"ChapterTitle",     myContextLocale, 0, request );
String elementChapterSummary   =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument,
"ChapterSummary",   myContextLocale, 0, request );
String elementChapterKeywords  =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument,
"ChapterKeywords",  myContextLocale, 0, request );
String elementChapterText      =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument,
"ChapterText",      myContextLocale, 0, request );

String elementSection1Title    =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument,
"Section1Title",    myContextLocale, 0, request );
String elementSection1Summary  =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument,
"Section1Summary",  myContextLocale, 0, request );
String elementSection1Text     =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument,
"Section1Text",     myContextLocale, 0, request );

String elementSection2Title    =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument,
"Section2Title",    myContextLocale, 0, request );
String elementSection2Summary  =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument,
"Section2Summary",  myContextLocale, 0, request );
String elementSection2Text     =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument,
"Section2Text",     myContextLocale, 0, request );

String elementFlagAName        =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument,
"FlagAName",        myContextLocale, 0, request );
String elementFlagAValue       =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument,
"FlagAValue",       myContextLocale, 0, request );

String elementParagraphText    =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument,
"ParagraphText",    myContextLocale, 0, request );

String elementFileRef          =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument, "FileRef",
myContextLocale, 0, request );
String elementFormatGIF        =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument,
"FormatGIF",        myContextLocale, 0, request );
String elementFormatJPEG       =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument,
"FormatJPEG",       myContextLocale, 0, request );
String elementFormatMPEG       =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument,
"FormatMPEG",       myContextLocale, 0, request );
String elementWidth            =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument, "Width",
myContextLocale, 0, request );
String elementDepth            =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument, "Depth",
myContextLocale, 0, request );
String elementTextObjectPhrase =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument,
"TextObjectPhrase", myContextLocale, 0, request );
String elementCaption          =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument, "Caption",
myContextLocale, 0, request );
String elementFormalParaTitle  =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument,
"FormalParaTitle",  myContextLocale, 0, request );
String elementFormalParaText   =
CmsJspTagContentShow.contentShowTagAction( myCmsXmlDocument,
"FormalParaText",   myContextLocale, 0, request );

%><HTML><HEAD> <TITLE> Test docbook-b.jsp </TITLE> </HEAD>
<BODY>
<h1> Test docbook-b.jsp </h1>
<p> mytemplate: <%= mytemplate %> </p>
<p> myRequestURI: <%= myRequestURI %> </p>

<h3> Properties </h3>
<p> propsAll:         <%= propsAll         %> </p>
<p> propsTitle:       <%= propsTitle       %> </p>
<p> propsDescription: <%= propsDescription %> </p>
<p> propsKeywords:    <%= propsKeywords    %> </p>

<h3> Context </h3>
<p> ContextEncoding:  <%= myContextEncoding  %> </p>
<p> ContextFolderUri: <%= myContextFolderUri %> </p>
<p> ContextSiteRoot:  <%= myContextSiteRoot  %> </p>
<p> ContextVfsUri:    <%= myContextVfsUri    %> </p>

<h3> XML Content </h3>
<p> <%= myContent2 %> </p>

<h3> XML Schema Namespace </h3>
<p> Namespace:    <%= myNamespace %> </p>

<h3> XML Elements </h3>
<table>
<tr><td> Author:          </td><td> <%= elementAuthor          %>
</td></tr>
<tr><td> Release:         </td><td> <%= elementRelease         %>
</td></tr>
<tr><td> Categories:      </td><td> <%= elementCategories      %>
</td></tr>
<tr><td> Rules:           </td><td> <%= elementRules           %>
</td></tr>
<tr><td> ChapterTitle:    </td><td> <%= elementChapterTitle    %>
</td></tr>
<tr><td> ChapterSummary:  </td><td> <%= elementChapterSummary  %>
</td></tr>
<tr><td> ChapterKeywords: </td><td> <%= elementChapterKeywords %>
</td></tr>
<tr><td> ChapterText:     </td><td> <%= elementChapterText     %>
</td></tr>
<tr><td> Section1Title:   </td><td> <%= elementSection1Title   %>
</td></tr>
<tr><td> Section1Summary: </td><td> <%= elementSection1Summary %>
</td></tr>
<tr><td> Section1Text:    </td><td> <%= elementSection1Text    %>
</td></tr>
<tr><td> Section2Title:   </td><td> <%= elementSection2Title   %>
</td></tr>
<tr><td> Section2Summary: </td><td> <%= elementSection2Summary %>
</td></tr>
<tr><td> Section2Text:    </td><td> <%= elementSection2Text    %>
</td></tr>
<tr><td> FlagAName:       </td><td> <%= elementFlagAName       %>
</td></tr>
<tr><td> FlagAValue:      </td><td> <%= elementFlagAValue      %>
</td></tr>
<tr><td> ParagraphText:   </td><td> <%= elementParagraphText   %>
</td></tr>
<tr><td> FileRef:          </td><td> <%= elementFileRef          %>
</td></tr>
<tr><td> FormatGIF:        </td><td> <%= elementFormatGIF        %>
</td></tr>
<tr><td> FormatJPEG:       </td><td> <%= elementFormatJPEG       %>
</td></tr>
<tr><td> FormatMPEG:       </td><td> <%= elementFormatMPEG       %>
</td></tr>
<tr><td> Width:            </td><td> <%= elementWidth            %>
</td></tr>
<tr><td> Depth:            </td><td> <%= elementDepth            %>
</td></tr>
<tr><td> TextObjectPhrase: </td><td> <%= elementTextObjectPhrase %>
</td></tr>
<tr><td> Caption:          </td><td> <%= elementCaption          %>
</td></tr>
<tr><td> FormalParaTitle:  </td><td> <%= elementFormalParaTitle  %>
</td></tr>
<tr><td> FormalParaText:   </td><td> <%= elementFormalParaText   %>
</td></tr>
</table>

<h3> XML Elements From Tags </h3>
<cms:contentload collector="singleFile" param="opencms:uri"
editable="true">
<table>
<tr><td> Author:          </td><td> <cms:contentshow element="Author"
/> </td></tr>
<tr><td> Release:         </td><td> <cms:contentshow element="Release"
/> </td></tr>
<tr><td> Categories:      </td><td> <cms:contentshow
element="Categories"      /> </td></tr>
<tr><td> Rules:           </td><td> <cms:contentshow element="Rules"
/> </td></tr>
<tr><td> ChapterTitle:    </td><td> <cms:contentshow
element="ChapterTitle"    /> </td></tr>
<tr><td> ChapterSummary:  </td><td> <cms:contentshow
element="ChapterSummary"  /> </td></tr>
<tr><td> ChapterKeywords: </td><td> <cms:contentshow
element="ChapterKeywords" /> </td></tr>
<tr><td> ChapterText:     </td><td> <cms:contentshow
element="ChapterText"     /> </td></tr>
<tr><td> Section1Title:   </td><td> <cms:contentshow
element="Section1Title"   /> </td></tr>
<tr><td> Section1Summary: </td><td> <cms:contentshow
element="Section1Summary" /> </td></tr>
<tr><td> Section1Text:    </td><td> <cms:contentshow
element="Section1Text"    /> </td></tr>
<tr><td> Section2Title:   </td><td> <cms:contentshow
element="Section2Title"   /> </td></tr>
<tr><td> Section2Summary: </td><td> <cms:contentshow
element="Section2Summary" /> </td></tr>
<tr><td> Section2Text:    </td><td> <cms:contentshow
element="Section2Text"    /> </td></tr>
<tr><td> FlagAName:       </td><td> <cms:contentshow element="FlagAName"
/> </td></tr>
<tr><td> FlagAValue:      </td><td> <cms:contentshow
element="FlagAValue"      /> </td></tr>
<tr><td> ParagraphText:   </td><td> <cms:contentshow
element="ParagraphText"   /> </td></tr>
<tr><td> FileRef:          </td><td> <cms:contentshow element="FileRef"
/> </td></tr>
<tr><td> FormatGIF:        </td><td> <cms:contentshow
element="FormatGIF"        /> </td></tr>
<tr><td> FormatJPEG:       </td><td> <cms:contentshow
element="FormatJPEG"       /> </td></tr>
<tr><td> FormatMPEG:       </td><td> <cms:contentshow
element="FormatMPEG"       /> </td></tr>
<tr><td> Width:            </td><td> <cms:contentshow element="Width"
/> </td></tr>
<tr><td> Depth:            </td><td> <cms:contentshow element="Depth"
/> </td></tr>
<tr><td> TextObjectPhrase: </td><td> <cms:contentshow
element="TextObjectPhrase" /> </td></tr>
<tr><td> Caption:          </td><td> <cms:contentshow element="Caption"
/> </td></tr>
<tr><td> FormalParaTitle:  </td><td> <cms:contentshow
element="FormalParaTitle"  /> </td></tr>
<tr><td> FormalParaText:   </td><td> <cms:contentshow
element="FormalParaText"   /> </td></tr>
</table>
</cms:contentload>
</BODY> </HTML>

========================================
========================================
Regards

Mike

 

-----Original Message-----
From: opencms-dev-bounces at opencms.org
[mailto:opencms-dev-bounces at opencms.org] On Behalf Of Andy Kriger
Sent: 11 November 2004 17:24
To: 'The OpenCms mailing list'
Subject: RE: [opencms-dev] open questions about XML content


However, it's certainly not the intention of the XML content to have
schema
changes many times. Think of the XML content schema as of database
schemas:
Once you got them right on you development machine, they should be
deployed
to production and not change often.

-----

I agree with that logic in principle. However, envision a client-driven
OpenCMS deployment where they decide they want to new pieces of
information
to an existing type of XML content (clients love to change their minds
and
aren't very understanding of 'it would be very difficult to accomodate
you'). Unlike a db schema where you can add a pointer to a new table to
handle a change, the content schema is locked into one file (afaik).
Since
the controlcode has a reference to the schema, on edit, it should be
possible for the content file to update itself with the missing fields
(I've
done this successfully by hand).

I will definitely look into the XmlContent API and see what is possible
there. It also looks like CmsFile can get the byte[] of XML content
files
(the controlcode) and you could use DOM to add fields programmatically
that
way (though I've only done a simple test of this).

-a



_______________________________________________
This mail is send to you from the opencms-dev mailing list
To change your list options, or to unsubscribe from the list, please
visit
http://mail.opencms.org/mailman/listinfo/opencms-dev





More information about the opencms-dev mailing list