Opencms: Borrar un Locale

Para borrar un locale determinado de una página podemos hacerlo por código de la siguiente manera.


//Elegimos el locale a borrar

Locale locale=new Locale("en");

//Cogemos el recurso

CmsResource r = cmso.readResource("/prueba.html");

//Comprobamos que sea un contenido XML

I_CmsResourceType resType = OpenCms.getResourceManager().getResourceType(r.getTypeId());
if (resType instanceof CmsResourceTypeXmlContent) {

//Obtenemos el contenido XML
CmsXmlContent resourceDocument = CmsXmlContentFactory.unmarshal(cmso, cmso.readFile(r));

//Borramos el locale

resourceDocument.removeLocale(locale);

//Establecemos el XML

resourceDocument.getFile().setContents( resourceDocument.marshal() );
//Bloqueamos el recurso
cmso.lockResource( "/prueba.html" );

//Guardamos los cambios
cmso.writeFile(resourceDocument.getFile());

}

También podemos de esta forma borrar algún elemento del XML con

resourceDocument.removeValue("ELEMENTO",locale,0);

Como siempre, espero que os sirva. Os dejo el código competo de una página JSP que recorre recursivamente buscando los index.html para borrar el locale “en”


<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="java.util.Locale" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="org.opencms.file.CmsResource" %>
<%@ page import="org.opencms.file.CmsObject" %>
<%@ page import="org.opencms.util.CmsUUID" %>
<%@ page import="org.opencms.util.CmsStringUtil" %>
<%@ page import="org.opencms.main.*" %>
<%@ page import="org.opencms.jsp.*" %>
<%@ page import="org.opencms.file.*" %>
<%@ page import="org.opencms.file.types.*" %>
<%@ page import="org.opencms.xml.*" %>
<%@ page import="org.opencms.xml.content.*" %>
<%@ page import="org.opencms.xml.types.*" %>
<%@ page import="org.opencms.lock.*" %>
<%@ page import="org.opencms.db.CmsResourceState" %>
<%@ page import="org.opencms.xml.content.CmsXmlContent" %>
<%!
//FUNCION RECURSIVA
@SuppressWarnings({"unchecked"})
static void showFolder(CmsObject cmso,CmsJspActionElement cms, JspWriter out, CmsJspNavBuilder nav, List<CmsJspNavElement> filesInFolder,String folder) throws Exception {
String name;
Locale len=new Locale("en");
out.println("SHOWFOLDER "+folder+"<br>");
List<CmsJspNavElement> filesInSub;
for (CmsJspNavElement sub : filesInFolder) {
name = sub.getResourceName();
out.println("RECURSO "+name+"<br>");


name = sub.getResourceName();
if (!sub.isFolderLink()) {
if (name.endsWith(".config")) {
} else {
out.println(name+" ES INDEX "+"<br>");
CmsResource r = cmso.readResource(sub.getResourceName());
I_CmsResourceType resType = OpenCms.getResourceManager().getResourceType(r.getTypeId());
if (resType instanceof CmsResourceTypeXmlContent) {
try {
CmsXmlContent resourceDocument = CmsXmlContentFactory.unmarshal(cmso, cmso.readFile(r));
resourceDocument.removeLocale(len);
resourceDocument.getFile().setContents( resourceDocument.marshal() );
cmso.lockResource( sub.getResourceName() );
cmso.writeFile(resourceDocument.getFile());
} catch (CmsXmlException e) {
out.println("ERROR AL BORRAR LOCALE EN "+sub.getResourceName()+"<br>");
}
}
}
}
if ((!sub.getResourceName().endsWith("_documentos/"))&&(!sub.getResourceName().endsWith("_imagenes/"))) {
if (sub.isFolderLink()) {
filesInSub = nav.getNavigationForFolder(sub.getResourceName(),org.opencms.jsp.CmsJspNavBuilder.Visibility.all,org.opencms.file.CmsResourceFilter.ONLY_VISIBLE);
if (filesInSub.size() > 0) {
showFolder(cmso,cms, out, nav, filesInSub,sub.getResourceName());
}
}
}

}
}
%>
<%

Locale les=new Locale("es");
Locale len=new Locale("en");
Set<String> elementos = new HashSet<String>();
elementos.add("Containers");

CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
CmsJspNavBuilder nav = cms.getNavigation();
CmsObject cmso = cms.getCmsObject();
showFolder(cmso,cms,out,nav,nav.getNavigationForFolder("/uva/"),"/uva/");

List list = cms.getNavigation().getNavigationForFolder("/uva/");
Iterator i = list.iterator();
while (i.hasNext()) {
org.opencms.jsp.CmsJspNavElement ne = (org.opencms.jsp.CmsJspNavElement)i.next();
out.println(ne.getResourceName()+"<br>");
}

%>

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos necesarios están marcados *