From: <jbo...@li...> - 2005-11-04 17:52:46
|
Author: unibrew Date: 2005-11-04 12:52:25 -0500 (Fri, 04 Nov 2005) New Revision: 1512 Added: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCounterDescriptor.java Modified: trunk/forge/portal-extensions/forge-common/project.xml trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/ProjectDescriptor.java trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/ProjectsHelper.java Log: [JBLAB-263] DownloadCounter sources for descriptor parsing. Modified: trunk/forge/portal-extensions/forge-common/project.xml =================================================================== --- trunk/forge/portal-extensions/forge-common/project.xml 2005-11-04 17:48:52 UTC (rev 1511) +++ trunk/forge/portal-extensions/forge-common/project.xml 2005-11-04 17:52:25 UTC (rev 1512) @@ -44,7 +44,7 @@ <version>1.0</version> <jar>javax.servlet.jsp.jar</jar> </dependency> - + <dependency> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> Added: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCounterDescriptor.java =================================================================== --- trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCounterDescriptor.java 2005-11-04 17:48:52 UTC (rev 1511) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCounterDescriptor.java 2005-11-04 17:52:25 UTC (rev 1512) @@ -0,0 +1,174 @@ + + /* + * JBoss, Home of Professional Open Source + * Copyright 2005, JBoss Inc., and individual contributors as indicated + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This 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 software 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. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.jboss.forge.common.projects; + +import java.io.File; +import java.io.IOException; +import java.util.LinkedList; +import java.util.List; + +import javax.xml.transform.TransformerException; + +import org.apache.xerces.parsers.DOMParser; +import org.jboss.forge.common.XmlTools; +import org.jboss.forge.common.projects.XmlInputFactory.XmlNotFoundException; +import org.jboss.shotoku.ContentManager; +import org.jboss.shotoku.aop.Inject; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +public class DownloadCounterDescriptor extends AbstractDescriptor { + + @Inject + private ContentManager contentManager; + + public DownloadCounterDescriptor (XmlInputFactory isf, DomToXmlTransformer xht, + String portalName, String projectId) throws SAXException, IOException, + XmlNotFoundException, TransformerException { + + // The isf is already suffixed with the members directory, so we just + // need to add project id and descriptor name. + String isfPathToXml = File.separator+projectId + File.separator + ProjectsHelper.DOWNLOADCOUNTER_DESC; + // Getting the categories and files descriptors + DOMParser parser = new DOMParser(); + InputSource is = isf.getInputSource(isfPathToXml); + parser.parse(is); + org.w3c.dom.Document doc = parser.getDocument(); + Node n=null,property=null; + NodeList nodes = doc.getDocumentElement().getChildNodes(); + + List<String> links = new LinkedList<String>(); + for (int i = 0; i < nodes.getLength(); i++) { + n = nodes.item(i); + if (n.getNodeType() == Node.ELEMENT_NODE) { + if (n.getNodeName().equals("counter")) { + NodeList counterProps = n.getChildNodes(); + for (int j=0;j< counterProps.getLength() ; j++) { + property = counterProps.item(j); + if (property.getNodeType()== Node.ELEMENT_NODE){ + if (property.getNodeName().equals("link") && XmlTools.unmarshallText(property) != null + && !XmlTools.unmarshallText(property).trim().equals("")) { + links.add(XmlTools.unmarshallText(property).trim()); + } + } + } + } + } + } + + if (!links.isEmpty()) { + addLinksToCounter(projectId,portalName,links,isf,xht); + } + } + + /** + * Mathod adds new links to the main download counter xml file. + * @param projectId + * @param portalName + * @param links + * @param isf + * @param xht + * @throws SAXException + * @throws IOException + * @throws XmlNotFoundException + * @throws TransformerException + */ + private synchronized void addLinksToCounter (String projectId,String portalName, + List<String> links, XmlInputFactory isf, DomToXmlTransformer xht) + throws SAXException, IOException, XmlNotFoundException, TransformerException { + + // Opening the main download counter xml descriptor for parsing. + DOMParser parser = new DOMParser(); + parser.parse(isf.getInputSource(ProjectsHelper.DOWNLOADCOUNTERMAIN_DESC)); + Document doc = parser.getDocument(); + NodeList nodes = doc.getDocumentElement().getChildNodes(); + Node n=null,property=null; + + + // This loop will remove all links from the "List<String> link" + // which are already in the main download counter descriptor. + for (int i=0;i <nodes.getLength(); i++) { + n = nodes.item(i); + if (n.getNodeType() == Node.ELEMENT_NODE) { + if (n.getNodeName().equals("counter")) { + NodeList counterSettings = n.getChildNodes(); + for (int j=0; j <counterSettings.getLength() ; j++) { + property = counterSettings.item(j); + if (property.getNodeType() == Node.ELEMENT_NODE) { + String nodeName = property.getNodeName(); + String nodeNodeValue = XmlTools.unmarshallText(property).trim(); + if (nodeName.equals("link") && nodeNodeValue!=null && links.contains(nodeNodeValue)) { + links.remove(nodeNodeValue); + } + } + } + } + } + } + + + // Here the new links from the "List<String> links" will be added. + if (!links.isEmpty()) { + + for (String link:links) { + Node newCounter = doc.createElement("counter"); + + Node newLink = doc.createElement("link"); + Node newLinkText = doc.createTextNode(link); + newLink.appendChild(newLinkText); + + Node newProjectId = doc.createElement("id"); + Node newProjectIdText = doc.createTextNode(projectId); + newProjectId.appendChild(newProjectIdText); + + Node newValue = doc.createElement("value"); + Node newValueText = doc.createTextNode("0"); + newValue.appendChild(newValueText); + + newCounter.appendChild(newLink); + newCounter.appendChild(newProjectId); + newCounter.appendChild(newValue); + + doc.getDocumentElement().appendChild(newCounter); + } + } + + // Making path to main counter xml located in members directory. + String pathToCountersXml = File.separator + portalName + File.separator + + ProjectsHelper.MEMBERS_DIR + File.separator + ProjectsHelper.DOWNLOADCOUNTERMAIN_DESC; + + // Getting string containing whole xml. + String xmlString = xht.transformNode(doc.getDocumentElement()); + + // Saving the main download counter xml descriptor with a new content. + org.jboss.shotoku.Node xmlFile = contentManager.getNode(pathToCountersXml); + xmlFile.setContent(xmlString); + xmlFile.save ("[DownlaodCounter] Main xml descriptor file update."); + + } + +} Modified: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/ProjectDescriptor.java =================================================================== --- trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/ProjectDescriptor.java 2005-11-04 17:48:52 UTC (rev 1511) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/ProjectDescriptor.java 2005-11-04 17:52:25 UTC (rev 1512) @@ -52,6 +52,8 @@ private static final Logger log = Logger.getLogger(ProjectDescriptor.class); private DownloadsDescriptor downloads; + + private DownloadCounterDescriptor downloadCounter; private String portalName; @@ -116,6 +118,18 @@ " descriptor: "+e); downloads = null; } + + // Trying to create a downloads counter descriptor. + try { + downloadCounter = new DownloadCounterDescriptor(isf, xht, portalName, getId()); + } catch (XmlNotFoundException e1) { + // It means that this project doesn't have downlaod counter descriptor. + } + catch (Exception e) { + log.warn("Project "+getId()+", unable te parse download counter" + + " descriptor: "+e); + downloadCounter = null; + } } /** Modified: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/ProjectsHelper.java =================================================================== --- trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/ProjectsHelper.java 2005-11-04 17:48:52 UTC (rev 1511) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/ProjectsHelper.java 2005-11-04 17:52:25 UTC (rev 1512) @@ -81,6 +81,8 @@ protected final static String PROJECT_DESC = "project.xml"; protected final static String PROJECTS_DESC = "projects.xml"; protected final static String DOWNLOADS_DESC = "downloads.xml"; + protected final static String DOWNLOADCOUNTER_DESC = "counter.xml"; + protected final static String DOWNLOADCOUNTERMAIN_DESC = "counters.xml"; /** * <code>MEMBERS_DIRECTORY</code> - base repository directory in which |