2010-02-03 5 views
3

OpenCMS를 기반으로하는 인트라넷 웹 사이트에서 작업 중이며 사이트에 태그 클라우드를 포함하고 싶습니다. OpenCloud와 같은 몇 가지 opensource tagcloud Java 라이브러리를 찾았습니다. 이 두 가지 (또는 다른 태그 클라우드 라이브러리 + OpenCMS)를 함께 연결 한 경험이 있습니까?OpenCMS 웹 사이트에 태그 클라우드 추가

답변

4

좋아요. 그래서이 부분적으로 나 자신을 부분적으로 해결했습니다. Richard Friedman의 tag cloud 코드도 사용했습니다.

내가하는 일은 다음과 같습니다. 지정된 간격으로 OpenCMS는 Lucene 색인을 읽고, 키워드 필드에서 모든 용어를 추출하며 (VFS의 모든 파일에 대해 채울 수 있음) 예약 된 작업을 실행합니다. 태그 클라우드를 생성하고 내 OpenCMS 템플릿의 일부인 파일에 결과를 저장합니다. Cloud.java와 BuildTagCloud.java라는 두 개의 Java 파일이 있습니다. "Cloud"는 색인을 읽고 가장 일반적인 용어 목록을 반환합니다. "BuildTagCloud"는 I_CmsScheduledJob 인터페이스를 구현하고 예약 된 작업으로 등록됩니다.

BuildTagCloud.java :

package mypackage; 

import org.opencms.file.*; 
import org.opencms.main.*; 
import org.opencms.scheduler.I_CmsScheduledJob; 
import java.text.SimpleDateFormat; 
import java.util.*; 


public class BuildTagCloud implements I_CmsScheduledJob { 

    private final String indexaddress = "address/of/your/index/folder"; // something like ../webapps/opencms/WEB-INF/index/nameOfIndex 
    private final String tagsFile = "address"; // part of my template; it's where I store the tag cloud 
    private final int numTerms = 10; // number of terms in the tag cloud                 


    public String launch(CmsObject object, java.util.Map parameters) throws java.lang.Exception { 
     Cloud cloud = new Cloud(indexaddress, numTerms); 

     Calendar cal = Calendar.getInstance(); 
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
     String data; 
     data = "<div style=\"border-top: 3px solid #000099; padding-top: 6px; margin-top: 17px;\"><span style=\"font-weight: bold; font-size: 11px; color: #000099;\">Tag cloud</span><br />"; 
     data += sdf.format(cal.getTime()) + "<br />"; 

     try { 
      List<TermInfo> list = cloud.getCloud(); 

      for(int i = 0; i<list.size(); i++) { 
       data += "<br />" + i + ". " + list.get(i).term.text() + " ... " + list.get(i).docFreq; // list.get(i).docFreq 
      } 

     } catch (Exception e) { 

      data += e.getMessage(); 
      data += "<br />"; 

     } finally { 

      data+="</div>"; 
     } 

     writeAndPublishResource(object, tagsFile, data); 

     return "OK"; 
    } 

    private void writeAndPublishResource(CmsObject object, String resouce, String data) throws java.lang.Exception { 
     object.loginUser("administrator's user name", "and his password"); 

     CmsRequestContext cmsContext = object.getRequestContext(); 
     CmsProject curProject = cmsContext.currentProject(); 

     if(curProject.isOnlineProject()){ 
      CmsProject offlineProject = object.readProject("Name of the project"); 
      cmsContext.setCurrentProject(offlineProject); 
     } 
     CmsResource res = object.readResource(resouce); 
     object.lockResource(resouce); 
     CmsFile file = object.readFile(res); 
     file.setContents(data.getBytes()); 
     object.writeFile(file); 
     OpenCms.getPublishManager().publishResource(object, resouce); 
     object.unlockResource(resouce); 
    } 

} 

Cloud.java : 나는 그것을 알아내는 시간이 엄청 많이 소비로이 사람을 도울 수

package mypackage; 

import java.io.*; 
import java.util.*; 
import org.apache.lucene.index.*; 

public class Cloud { 

    private String indexaddress; 
    private int numTerms; 

    private int max; 
    private int sum; 

    public Cloud(String indexaddress, int numTerms) { 
     this.indexaddress = indexaddress; 
     this.numTerms = numTerms; 
     max = 0; 
     sum = 0; 
    } 

    public List<TermInfo> getCloud() throws Exception { 

     TermInfoQueue termQ = new TermInfoQueue(numTerms); 

     IndexReader reader = IndexReader.open(new File(indexaddress)); 
     TermEnum terms = reader.terms(); 



     int minFreq = 0; 
     while (terms.next()) { 

      if (!terms.term().field().equals("keywords")) continue; 

      if (terms.docFreq() > minFreq) { 
       if (termQ.size() >= numTerms)   // if tiq overfull 
       { 
        termQ.pop();     // remove lowest in tiq 
        termQ.put(new TermInfo(terms.term(), terms.docFreq())); 
        minFreq = ((TermInfo)termQ.top()).docFreq; // reset minFreq 
       } else { 
        termQ.put(new TermInfo(terms.term(), terms.docFreq())); 
       } 
      } 
     } 

     terms.close(); 
     reader.close(); 

     ArrayList<TermInfo> res = new ArrayList<TermInfo>(termQ.size()); 
     while (termQ.size() > 0) { 
      TermInfo ti = (TermInfo)termQ.pop(); 
      max = Math.max(max, ti.docFreq); 
      sum += ti.docFreq; 
      res.add(ti); 
     } 

     // Shuffles the results up, since a sorted cloud would be predictiable. 
     //Collections.shuffle(res); 

     return res; 
     } 

     public int getMaxFrequency() { 
      return max; 
     } 
} 

class TermInfo { 

    TermInfo(Term t, int df) { 
     term = t; 
     docFreq = df; 
    } 

    public int docFreq; 
    public Term term; 
} 

class TermInfoQueue extends org.apache.lucene.util.PriorityQueue { 

    TermInfoQueue(int size) { 
     initialize(size); 
    } 

    protected final boolean lessThan(Object a, Object b) { 
     TermInfo termInfoA = (TermInfo)a; 
     TermInfo termInfoB = (TermInfo)b; 
     return termInfoA.docFreq < termInfoB.docFreq; 
    } 
} 

희망!

0

나는 tagsFile에 설정 한 정보의 유형을 알고 있습니다. 내 템플릿의 요소 이름에?