org.eclipse.osee.orcs.db.internal.search.tagger.XmlTagger.java Source code

Java tutorial

Introduction

Here is the source code for org.eclipse.osee.orcs.db.internal.search.tagger.XmlTagger.java

Source

/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.orcs.db.internal.search.tagger;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import org.eclipse.osee.framework.core.enums.QueryOption;
import org.eclipse.osee.framework.jdk.core.type.MatchLocation;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.jdk.core.util.io.xml.XmlTextInputStream;
import com.google.common.io.InputSupplier;

/**
 * @author Roberto E. Escobar
 */
public class XmlTagger extends AbstractTagger {

    public XmlTagger(TagProcessor tagProcessor, StreamMatcher matcher) {
        super(tagProcessor, matcher);
    }

    @Override
    public void tagIt(InputSupplier<? extends InputStream> provider, TagCollector collector) throws Exception {
        InputStream inputStream = null;
        try {
            inputStream = getStream(provider);
            getTagProcessor().collectFromInputStream(inputStream, collector);
        } finally {
            Lib.close(inputStream);
        }
    }

    @Override
    public List<MatchLocation> find(InputSupplier<? extends InputStream> provider, String toSearch,
            boolean matchAllLocations, QueryOption... options) throws Exception {
        List<MatchLocation> toReturn;
        if (Strings.isValid(toSearch)) {
            InputStream inputStream = null;
            try {
                inputStream = getStream(provider);
                toReturn = getMatcher().findInStream(inputStream, toSearch, matchAllLocations, options);
            } finally {
                Lib.close(inputStream);
            }
        } else {
            toReturn = Collections.emptyList();
        }
        return toReturn;
    }

    private InputStream getStream(InputSupplier<? extends InputStream> provider) throws IOException {
        return new XmlTextInputStream(provider.getInput());
    }
}