All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.day.util.diff.WordsElementsFactory Maven / Gradle / Ivy

/*
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2017 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 */
package com.day.util.diff;

import java.io.Reader;
import java.io.StringReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.ArrayList;

/**
 * Implements an element factory that creates the elements out from words of
 * the given input stream.
 */
public class WordsElementsFactory implements ElementsFactory {

    private static final int MAX_ELEMENTS = 100000;

    private final Document.Element[] elements;

    public WordsElementsFactory(Document.Element[] elements) {
        this.elements = elements;
    }

    public Document.Element[] getElements() {
        return elements;
    }

    public static WordsElementsFactory create(DocumentSource source, String text) {
        try {
            Reader reader = new StringReader(text);
            return create(source, reader);
        } catch (IOException e) {
            throw new IllegalArgumentException(e.toString());
        }
    }

    public static WordsElementsFactory create(DocumentSource source, Reader text)
            throws IOException {
        Document.Element[] elements = getElements(source, text);
        return new WordsElementsFactory(elements);
    }

    private static Document.Element[] getElements(DocumentSource source, Reader text)
            throws IOException {
        BufferedReader r;
        if (text instanceof BufferedReader) {
            r = (BufferedReader) text;
        } else {
            r = new BufferedReader(text);
        }
        ArrayList lines = new ArrayList();
        StringBuffer gutter = new StringBuffer();
        StringBuffer word = new StringBuffer();
        int c;
        while ((c=r.read()) >=0 && lines.size() 0) {
                    lines.add(new WordElement(source, word.toString(), gutter.toString()));
                    gutter.setLength(0);
                    word.setLength(0);
                }
                word.append((char) c);
            } else {
                gutter.append((char) c);
            }
        }
        if (word.length() > 0) {
            lines.add(new WordElement(source, word.toString(), gutter.toString()));
        }
        return (WordElement[]) lines.toArray(new WordElement[lines.size()]);
    }

    public static class WordElement implements Document.Element {

        private final DocumentSource source;

        private final String word;

        private final String gutter;

        public WordElement(DocumentSource source, String word, String gutter) {
            this.source = source;
            this.word = word;
            this.gutter = gutter;
        }

        public String getString() {
            return word + gutter;
        }

        public DocumentSource getDocumentSource() {
            return source;
        }

        public int hashCode() {
            return word.hashCode();
        }

        public String toString() {
            return getString();
        }

        public boolean equals(Object obj) {
            if (obj == this) {
                return true;
            }
            if (obj instanceof WordElement) {
                return ((WordElement) obj).word.equals(word);
            }
            return false;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy