/*
This library is part of octools
Copyright (c) 2000-2007 Valtech A/S (http://www.valtech.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
Alkacon OpenCms and the OpenCms logo are registered trademarks of
Alkacon Software GmbH in Germany, the USA and other countries
For further information about Alkacon Software GmbH, please see the
company website: http://www.alkacon.com
For further information about OpenCms, please see the
project website: http://www.opencms.org
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package dk.valtech.octools.tools;
import java.util.List;
import java.util.Locale;
import org.opencms.file.CmsFile;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProject;
import org.opencms.main.CmsIllegalArgumentException;
import org.opencms.main.CmsRuntimeException;
import org.opencms.main.OpenCms;
import org.opencms.report.CmsLogReport;
import org.opencms.xml.content.CmsXmlContent;
import org.opencms.xml.content.CmsXmlContentValueSequence;
import org.opencms.xml.types.CmsXmlBooleanValue;
import org.opencms.xml.types.I_CmsXmlContentValue;
public class XmlContentHelper {
private CmsObject cms;
private CmsXmlContent content;
private Locale locale;
/**
* @param content
* @param locale
*/
public XmlContentHelper(CmsObject cms, CmsXmlContent content, Locale locale) {
this.cms = cms;
this.content = content;
this.locale = locale;
}
public String getStringValue(String element) throws ClassCastException {
return content.getStringValue(cms, element, locale);
}
public String getStringValue(String element, int index) {
return content.getStringValue(cms, element, locale, index);
}
public void setStringValue(String element, String value) {
content.getValue(element, locale).setStringValue(cms, value);
}
public boolean getBooleanValue(String element) throws ClassCastException {
return ((CmsXmlBooleanValue) content.getValue(element, locale)).getBooleanValue();
}
/* (non-Javadoc)
* @see org.opencms.xml.A_CmsXmlDocument#getValues(java.lang.String, java.util.Locale)
*/
@SuppressWarnings("unchecked")
public List<I_CmsXmlContentValue> getValues(String name) {
return content.getValues(name, locale);
}
public I_CmsXmlContentValue getValue(String name) {
return content.getValue(name, locale);
}
/**
* @return Returns the content.
*/
public CmsXmlContent getXmlContent() {
return content;
}
public Locale getLocale() {
return locale;
}
public I_CmsXmlContentValue addValue(String path, int index) throws CmsIllegalArgumentException, CmsRuntimeException {
return content.addValue(cms, path, locale, index);
}
public CmsXmlContentValueSequence getValueSequence(String name) {
return content.getValueSequence(name, locale);
}
public void removeValue(String name, int index) {
content.removeValue(name, locale, index);
}
/**
* This uses the project named Offline to lock, write, unlock and publish the xmlcontent file
*
* @throws Exception
*/
public void saveAndPublish() throws Exception {
saveAndPublish("Offline");
}
/**
* This uses the given projectName to lock, write, unlock and publish the xmlcontent file
*
* @param projectName
* @throws Exception
*/
public void saveAndPublish(String projectName) throws Exception {
CmsProject oldProject = cms.getRequestContext().currentProject();
cms.getRequestContext().setCurrentProject(cms.readProject(projectName));
CmsFile file = content.getFile();
file.setContents(content.marshal());
cms.lockResource(file.getRootPath());
cms.writeFile(file);
cms.unlockResource(cms.getSitePath(file));
OpenCms.getPublishManager().publishResource(cms, cms.getSitePath(file), false, new CmsLogReport(locale, getClass()));
cms.getRequestContext().setCurrentProject(oldProject);
}
}