2010-12-14 8 views
8

मैं Google के परिणामों के माध्यम से मुक्त (मुक्त स्रोत) जावा diff libaries के लिए खुदाई कर रहा हूं, और उनमें से कुछ का एक समूह लगता है (उनमें से कुछ सामान्य वस्तुओं के साथ भी काम करते हैं न केवल स्ट्रिंग्स के साथ)।क्या जावा के लिए एक डिफ लाइब्रेरी है जो एनोटेट/दोष का समर्थन करती है?

इससे पहले कि मैं खोज परिणामों के टन के माध्यम से खुदाई कर रहा हूँ और नहीं मिल रहा है कि मैं क्या खोज रहा हूँ, मैं यहाँ पहले कहेंगे:

उन diff पुस्तकालयों में से किसी सीवीएस व्याख्या या SVN दोष की तरह एक सुविधा का समर्थन करता है। करने के लिए

  • एक समारोह
  • एक समारोह को String[] के पुराने संस्करणों से गुजर रहा जारी रखने के लिए वर्तमान String[] पारित मैं, चाहते हैं जब तक या तो मैं उन सभी को ऊपर का इस्तेमाल किया है, या पुस्तकालय मुझे कि कोई मूल लाइन था बताता है String[] के पुराने संस्करणों को पुनर्प्राप्त करने के बाद से आखिरी बात वास्तव में एक जरूरी नहीं है, लेकिन बहुत उपयोगी है इसलिए मैं जितनी जल्दी हो सके रोकना चाहता हूं)
  • एक फ़ंक्शन कॉल करें जो मुझे ìnt[] देता है जो मुझे बताता है वर्तमान संस्करण की प्रत्येक पंक्ति, जिसमें संस्करण को आखिरी बार बदला गया था या फिर यह बिल्कुल नहीं बदला गया था (यानी लास टी पहले संस्करण में बदल गया)।

उन वस्तुओं के लिए समर्थन होने के कारण String एस अच्छा नहीं है, लेकिन कोई भी नहीं होना चाहिए। और अगर एपीआई बिल्कुल ठीक नहीं है, तो मुझे लगता है कि मैं इसके साथ रह सकता हूं।

यदि कोई नहीं है, तो क्या कोई भी एक एक्स्टेंसिबल डिफ लाइब्रेरी का सुझाव दे सकता है जहां उस सुविधा को आसानी से जोड़ा जा सकता है, प्राथमिक रूप से वह सुविधा जो योगदान के रूप में प्राप्त करना चाहती है (और उसे स्वीकार करने से पहले पेपरवर्क भरने की आवश्यकता नहीं होती है योगदान, जीएनयू परियोजना की तरह)? मैं वहां स्वयं को जोड़ने के लिए स्वयंसेवक (कम से कम कोशिश करने के लिए) चाहता हूं।

उत्तर

2

मैं दिमित्री नोमेंको के java-diff-utils पुस्तकालय के लिए इसे अपने आप को लागू करने का फैसला:

/* 
    Copyright 2010 Michael Schierl ([email protected]) 

    Licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 
    You may obtain a copy of the License at 

     http://www.apache.org/licenses/LICENSE-2.0 

    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License. 
*/ 
package difflib.annotate; 

import java.util.*; 

import difflib.*; 

/** 
* Generates an annotated version of a revision based on a list of older 
* revisions, like <tt>cvs annotate</tt> or <tt>svn blame</tt>. 
* 
* @author <a href="[email protected]">Michael Schierl</a> 
* 
* @param <R> 
*   Type of the revision metadata 
*/ 
public class Annotate<R> { 

    private final List<R> revisions; 
    private final int[] lineNumbers; 
    private R currentRevision; 
    private final List<Object> currentLines; 
    private final List<Integer> currentLineMap; 

    /** 
    * Creates a new annotation generator. 
    * 
    * @param revision 
    *   Revision metadata for the revision to be annotated 
    * @param targetLines 
    *   Lines of the revision to be annotated 
    */ 
    public Annotate(R revision, List<?> targetLines) { 
     revisions = new ArrayList<R>(); 
     lineNumbers = new int[targetLines.size()]; 
     currentRevision = revision; 
     currentLines = new ArrayList<Object>(targetLines); 
     currentLineMap = new ArrayList<Integer>(); 
     for (int i = 0; i < lineNumbers.length; i++) { 
      lineNumbers[i] = -1; 
      revisions.add(null); 
      currentLineMap.add(i); 
     } 
    } 

    /** 
    * Check whether there are still lines that are unannotated. In that case, 
    * more older revisions should be retrieved and passed to the function. Note 
    * that as soon as you pass an empty revision, all lines will be annotated 
    * (with a later revision), therefore if you do not have any more revisions, 
    * pass an empty revision to annotate the rest of the lines. 
    */ 
    public boolean areLinesUnannotated() { 
     for (int i = 0; i < lineNumbers.length; i++) { 
      if (lineNumbers[i] == -1 || revisions.get(i) == null) 
       return true; 
     } 
     return false; 
    } 

    /** 
    * Add the previous revision and update annotation info. 
    * 
    * @param revision 
    *   Revision metadata for this revision 
    * @param lines 
    *   Lines of this revision 
    */ 
    public void addRevision(R revision, List<?> lines) { 
     Patch patch = DiffUtils.diff(currentLines, lines); 
     int lineOffset = 0; // remember number of already deleted/added lines 
     for (Delta d : patch.getDeltas()) { 
      Chunk original = d.getOriginal(); 
      Chunk revised = d.getRevised(); 
      int pos = original.getPosition() + lineOffset; 
      // delete lines 
      for (int i = 0; i < original.getSize(); i++) { 
       int origLine = currentLineMap.remove(pos); 
       currentLines.remove(pos); 
       if (origLine != -1) { 
        lineNumbers[origLine] = original.getPosition() + i; 
        revisions.set(origLine, currentRevision); 
       } 
      } 
      for (int i = 0; i < revised.getSize(); i++) { 
       currentLines.add(pos + i, revised.getLines().get(i)); 
       currentLineMap.add(pos + i, -1); 
      } 
      lineOffset += revised.getSize() - original.getSize(); 
     } 

     currentRevision = revision; 
     if (!currentLines.equals(lines)) 
      throw new RuntimeException("Patch application failed"); 
    } 

    /** 
    * Return the result of the annotation. It will be a List of the same length 
    * as the target revision, for which every entry states the revision where 
    * the line appeared last. 
    */ 
    public List<R> getAnnotatedRevisions() { 
     return Collections.unmodifiableList(revisions); 
    } 

    /** 
    * Return the result of the annotation. It will be a List of the same length 
    * as the target revision, for which every entry states the line number in 
    * the revision where the line appeared last. 
    */ 
    public int[] getAnnotatedLineNumbers() { 
     return (int[]) lineNumbers.clone(); 
    } 
} 

मैं भी मामले में वह इसे शामिल करना चाहता है में दिमित्री नोमेंको को भेजा (कुछ परीक्षण मामलों के साथ)।

1

मैं गलत हो सकता हूं लेकिन मुझे लगता है कि एनोटेट/दोष को संस्करण नियंत्रण प्रणाली को काम करने की आवश्यकता है, क्योंकि इसे फ़ाइल के इतिहास तक पहुंचने की आवश्यकता है। एक सामान्य diff-पुस्तकालय ऐसा नहीं कर सकता है। तो यदि आपका लक्ष्य उन पुस्तकालयों की जांच करता है जो उन वीसीएस के साथ काम करते हैं, जैसे svnkit। यदि नहीं, तो ऐसी लाइब्रेरी एक अच्छा प्रारंभिक बिंदु हो सकता है कि एनोटेट/दोष कैसे किया जाता है, अक्सर इसमें फ़ाइल के सभी संस्करणों की श्रृंखला को अलग करना शामिल होता है।

+0

मैं फ़ाइल के सभी पुराने संस्करणों की क्या ज़रूरत है, वह यह है कि समस्या नहीं (जैसा कि प्रश्न में लिखा गया है)। सिर्फ वीसीएस में नहीं जो दोष का समर्थन करता है। और उन्हें एक फ़ाइल को दोष देने के लिए एक अस्थायी एसवीएन में करना है जो मैं बाद में नहीं हूं ... और एल्गोरिदम यहां कठिन हिस्सा नहीं है (हां इसमें सभी संस्करणों को अलग करना और ट्रैकिंग करना है कि लाइन नंबर कहां जा रहे हैं और जब वे "गायब हो जाते हैं), यह सिर्फ इतना है कि आपको इसे कार्यान्वित करने की आवश्यकता है :) – mihi

+1

गोटो। एक और विचार जावा आधारित विकी के स्रोत कोड को देखना होगा। चूंकि उन्हें आमतौर पर लागू करना होता है, इसलिए कोड वहां हो सकता है। http://c2.com/cgi/wiki?JavaWikiEngines कुछ सूचीबद्ध करता है। – vasquez

1

आप xwiki-commons-blame-api का उपयोग कर सकते हैं। यह वास्तव में उपयोग करता code from this thread's accepted answer (Thanks to Michael Schierl for sharing this code on StackOverflow)

आप कैसे की तरह it's unit tests.

में या स्काला में जावा में इसका इस्तेमाल करने के देख सकते हैं:

import java.util 
import org.xwiki.blame.AnnotatedContent 
import org.xwiki.blame.internal.DefaultBlameManager 

case class Revision(id: Int, 
        parentId: Option[Int] = None, 
        content: Option[String] = None) 

def createAnnotation(revisions: Seq[Revision]): Option[AnnotatedContent[Revision, String]] = { 
    val blameManager = new DefaultBlameManager() 

    val annotatedContent = revisions.foldLeft(null.asInstanceOf[AnnotatedContent[Revision, String]]){ 
     (annotation, revision) => 
     blameManager.blame(annotation, revision, splitByWords(revision.content)) 
    } 
    Option(annotatedContent) 
} 

def splitByWords(content: Option[String]): util.List[String] = { 
    val array = content.fold(Array[String]())(_.split("[^\\pL_\\pN]+")) 
    util.Arrays.asList(array:_*) 
} 
संबंधित मुद्दे

 संबंधित मुद्दे