001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 the ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016+-------------------------------------------------------------------------*/
017package org.conqat.engine.index.shared;
018
019import java.io.Serializable;
020import java.util.ArrayList;
021import java.util.Collection;
022import java.util.Collections;
023import java.util.List;
024
025import org.conqat.engine.index.shared.element_details.TokenElementDetailBase;
026import org.conqat.engine.resource.text.filter.base.Deletion;
027import org.conqat.engine.resource.text.filter.util.StringOffsetTransformer;
028import org.conqat.lib.commons.collections.CollectionUtils;
029import org.conqat.lib.commons.collections.UnmodifiableList;
030import org.conqat.lib.commons.js_export.ExportToJavaScript;
031
032import com.fasterxml.jackson.annotation.JsonProperty;
033
034import eu.cqse.check.framework.scanner.ELanguage;
035
036/**
037 * Transport object with the information needed for a token element.
038 * 
039 * This class is immutable.
040 */
041@ExportToJavaScript
042public class BasicTokenElementInfo implements Serializable {
043
044        /** Serial version UID. */
045        private static final long serialVersionUID = 1;
046
047        /** The uniform path. */
048        @JsonProperty("uniformPath")
049        protected final String uniformPath;
050
051        /** The language. */
052        @JsonProperty("language")
053        protected final ELanguage language;
054
055        /** The text content. */
056        @JsonProperty("text")
057        protected final String text;
058
059        /** The list of filtered regions */
060        @JsonProperty("filterDeletions")
061        protected final ArrayList<Deletion> filterDeletions;
062
063        /** Additional details of this info object. */
064        @JsonProperty("details")
065        protected final ArrayList<TokenElementDetailBase> details;
066
067        /** Constructor. */
068        public BasicTokenElementInfo(String uniformPath, ELanguage language, String text,
069                        Collection<Deletion> filterDeletions, Collection<TokenElementDetailBase> details) {
070                this.uniformPath = uniformPath;
071                this.language = language;
072                this.text = text;
073                this.filterDeletions = new ArrayList<>(filterDeletions);
074                Collections.sort(this.filterDeletions);
075                this.details = new ArrayList<>(details);
076        }
077
078        /** Copy constructor. */
079        protected BasicTokenElementInfo(BasicTokenElementInfo other) {
080                this(other.uniformPath, other.language, other.text, other.filterDeletions, other.details);
081        }
082
083        /** Returns the uniform path. */
084        public String getUniformPath() {
085                return uniformPath;
086        }
087
088        /** Returns the language. */
089        public ELanguage getLanguage() {
090                return language;
091        }
092
093        /**
094         * Returns the text content. Note that this is the <i>raw</i> content,
095         * <b>not</b> taking into account content filters stored in
096         * {@link #filterDeletions}. To obtain the filtered content, transform this into
097         * an ITokenElement.
098         */
099        public String getText() {
100                return text;
101        }
102
103        /**
104         * Returns the filtered text of this token element. Please note that the
105         * filtered content is computed on the fly.
106         */
107        public String getFilteredText() {
108                return new StringOffsetTransformer(filterDeletions).filterString(text);
109        }
110
111        /** Returns the filter deletions. */
112        public UnmodifiableList<Deletion> getFilterDeletions() {
113                return CollectionUtils.asUnmodifiable(filterDeletions);
114        }
115
116        /** @see #details */
117        public UnmodifiableList<TokenElementDetailBase> getDetails() {
118                return CollectionUtils.asUnmodifiable(details);
119        }
120
121        /** Returns the first detail of given type (or null). */
122        public <T extends TokenElementDetailBase> T getFirstDetailOfType(Class<T> type) {
123                return getFirstDetailOfType(type, details);
124        }
125
126        /** Returns the first detail of given type (or null) in the given list. */
127        @SuppressWarnings("unchecked")
128        public static <T extends TokenElementDetailBase> T getFirstDetailOfType(Class<T> type,
129                        List<TokenElementDetailBase> details) {
130                for (TokenElementDetailBase detail : details) {
131                        if (type.isInstance(detail)) {
132                                return (T) detail;
133                        }
134                }
135                return null;
136        }
137
138        /** {@inheritDoc} */
139        @Override
140        public String toString() {
141                return uniformPath;
142        }
143}