[opencms-dev] Problem with CmsXmlPageFactory.unmarshal creating only empty pages

Eska sven.kiesow at interone.de
Thu Feb 11 16:32:05 CET 2010


Hi Paul-Inge,

I already tried a way like yours after discovering the "problem" I had, but
to input the content of one
CmlXmlContent into another is not so straight-forward as one might think,
especially with nested content.

Regarding "xmlpage", is that a special content type and how does one
create/use one?

In the meantime I found another solution. I just should have remembered that
there is a Copy-Locale
inside of the Content-Editing. It uses the CmsXmlContent#copyLocale(Locale
source, Locale destination,
Set elements) method.

I made myself a quick hack that I can use for copying:


public static CmsXmlContent copyLocale(CmsObject cmsObj, CmsXmlContent
contentFrom, CmsXmlContent contentTo, Locale from) throws CmsException
{
	
	Document fromDoc = CmsXmlUtils.unmarshalHelper(contentFrom.marshal(), new
CmsXmlEntityResolver(null));
	Document toDoc = CmsXmlUtils.unmarshalHelper(contentTo.marshal(), new
CmsXmlEntityResolver(null));
	
	Element fromRoot = fromDoc.getRootElement();
	Element toRoot = toDoc.getRootElement();
	
    Element sourceElement = null;

    Iterator i = fromRoot.elementIterator();
    String localeStr = from.toString();
    while (i.hasNext()) {
        Element element = (Element)i.next();
        String language =
element.attributeValue(CmsXmlContentDefinition.XSD_ATTRIBUTE_VALUE_LANGUAGE,
null);
        if ((language != null) && (localeStr.equals(language))) {
            // detach node with the locale
            sourceElement = element.createCopy();
            // there can be only one node for the locale
            break;
        }
    }

    // switch locale value in attribute of copied node
   
sourceElement.addAttribute(CmsXmlContentDefinition.XSD_ATTRIBUTE_VALUE_LANGUAGE,
from.toString());
    

    // attach the copied node to the root node
    toRoot.add(sourceElement);    
    
    return CmsXmlContentFactory.unmarshal(cmsObj, toDoc,
contentTo.getEncoding(), new CmsXmlEntityResolver(null));

}

The returned CmsXmlContent can now be marshaled to a byte array that can now
be used to for writing
my resulting content to the Vfs.

Eska


Paul-Inge Flakstad wrote:
> 
> Hi Eska
> 
> I just tested some equivalent code on my system (OpenCms v7.5.1), with the
> same result.
> 
> I think maybe the class CmsXmlPage is intended for the resource type
> "xmlpage" and not "xmlcontents" (AKA structured content).
> 
> Maybe you can use somehting like this instead, it's what I normally do
> (although this is a very simplified example, you'll need to look at the
> docs):
> 
> CmsResource res = cmsObj.readResource("/your/file/name");
> I_CmsXmlDocument xmlDoc = CmsXmlContentFactory.unmarshal(cmso,
> cmso.readFile(res));
> I_CmsXmlContentValue val = xmlDoc.getValue(("Headline[1]"), locale); //
> Index starts at 1 not 0
> out.println("Headline[1] = " + val.getStringValue(cmsObj));
> 
> HTH.
> 
> Cheers,
> Paul
> 
>> -----Original Message-----
>> From: opencms-dev-bounces at opencms.org 
>> [mailto:opencms-dev-bounces at opencms.org] On Behalf Of Eska
>> Sent: 11. februar 2010 12:11
>> To: opencms-dev at opencms.org
>> Subject: [opencms-dev] Problem with 
>> CmsXmlPageFactory.unmarshal creating only empty pages
>> 
>> 
>> Hi List,
>> 
>> I am trying to program me a little jsp tool that does the same as
>> CmsMergePages.mergePages(),
>> but somehow the call to CmsXmlPageFactory.unmarshal() only 
>> creates empty
>> CmsXmlPage 
>> objects for me.
>> 
>> 
>> String resPathDe = "/sites/abc/de/test.html";
>> String resPathEn = "/sites/abc/en/testa.html"; 
>> 
>> CmsFile resFileDe = cmsObj.readFile(resDe);
>> CmsFile resFileEn = cmsObj.readFile(resEn);
>> 
>> Locale de = new Locale("de");
>> Locale en = new Locale("en");
>> 
>> 
>> CmsXmlPage xmlPageDe = CmsXmlPageFactory.unmarshal(cmsObj, resFileDe);
>> CmsXmlPage xmlPageEn = CmsXmlPageFactory.unmarshal(cmsObj, resFileEn);
>> 
>> Locale locDe = new Locale("de");
>> Locale locEn = new Locale("en");
>> 
>> List textElements2 = xmlPageEn.getNames(locEn );
>> Iterator j = textElements2.iterator();
>> while (j.hasNext()) {
>>    String textElementName = (String)j.next();
>>    String textElement = xmlPageEn.getValue(textElementName,
>> locEn).getStringValue(cmsObj);
>>                     
>>    if (!xmlPageDe.hasValue(textElementName, locEn)) {
>>       xmlPageDe.addValue(textElementName, locEn);
>>    }
>>    xmlPageDe.setStringValue(cmsObj, textElementName, locEn, 
>> textElement);
>> }
>> ...
>> 
>> When now trying to combine both page contents like in the
>> CmsMergePages.mergePages() method,
>> an empy content is created only containing only the following:
>> 
>> <?xml version="1.0" encoding="UTF-8"?>
>> 
>> <pages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> xsi:noNamespaceSchemaLocation="http://www.opencms.org/dtd/6.0/
>> xmlpage.xsd"></pages>
>> 
>> 
>> I gathered the following data:
>> 
>> resFileDe.getContents().length: 889
>> resFileEn.getContents().length: 1024
>> xmlPageDe.getNames(de).size(): 0
>> xmlPageEn.getNames(en).size(): 0
>> xmlPageDe.getValues(de).size(): 0
>> xmlPageEn.getValues(en).size(): 0
>> xmlPageDe.getLocales().size(): 0
>> xmlPageEn.getLocales().size(): 0
>> textElements2.size()=0
>> 
>> My two files have normal content like this:
>> 
>> <?xml version="1.0" encoding="UTF-8"?>
>> 
>> <ArticlePages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> xsi:noNamespaceSchemaLocation="opencms://system/modules/my.tes
>> t.module/schemas/articlepage.xsd">
>>   <ArticlePage language="de">
>>     <Status><![CDATA[7]]></Status>
>>     <TopProject>false</TopProject>
>>     <Headline><![CDATA[This is a Headline!]]></Headline>
>>     <Text name="Text0">
>> ...
>> 
>>    </ArticlePage>
>> </ArticlePages>
>> 
>> Is it the wrong way to try to merge to contents?
>> 
>> Thanks,
>> 
>> Eska.
>> -- 
>> View this message in context: 
>> http://old.nabble.com/Problem-with-CmsXmlPageFactory.unmarshal
> -creating-only-empty-pages-tp27544324p27544324.html
>> Sent from the OpenCMS - Dev mailing list archive at Nabble.com.
>> 
>> 
>> _______________________________________________
>> This mail is sent to you from the opencms-dev mailing list
>> To change your list options, or to unsubscribe from the list, 
>> please visit
>> http://lists.opencms.org/mailman/listinfo/opencms-dev
>> 
> 
> _______________________________________________
> This mail is sent to you from the opencms-dev mailing list
> To change your list options, or to unsubscribe from the list, please visit
> http://lists.opencms.org/mailman/listinfo/opencms-dev
> 
> 

-- 
View this message in context: http://old.nabble.com/Problem-with-CmsXmlPageFactory.unmarshal-creating-only-empty-pages-tp27544324p27547812.html
Sent from the OpenCMS - Dev mailing list archive at Nabble.com.




More information about the opencms-dev mailing list