From 8e7db232ee169c8d8513c8e8a84921d905e4af2d Mon Sep 17 00:00:00 2001 From: adrianocrestani Date: Mon, 10 Aug 2009 01:40:02 +0000 Subject: applying tuscany_2552_domain_search_phillipe_ramalho_08_08_2009.patch and tuscany_2552_domain_manager_phillipe_ramalho_08_08_2009.patch submitted to TUSCANY-2552 git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@802637 13f79535-47bb-0310-9956-ffa450edef68 --- .../tuscany/sca/domain/manager/impl/Searcher.java | 375 ++++++++++++++++----- 1 file changed, 290 insertions(+), 85 deletions(-) (limited to 'branches/sca-java-1.x/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/Searcher.java') diff --git a/branches/sca-java-1.x/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/Searcher.java b/branches/sca-java-1.x/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/Searcher.java index 5a2873b35f..0fb1812975 100644 --- a/branches/sca-java-1.x/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/Searcher.java +++ b/branches/sca-java-1.x/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/Searcher.java @@ -2,10 +2,12 @@ package org.apache.tuscany.sca.domain.manager.impl; import java.io.IOException; import java.io.StringWriter; +import java.io.Writer; import java.util.Arrays; -import java.util.Collection; +import java.util.Map; import org.apache.commons.lang.StringEscapeUtils; +import org.apache.tuscany.sca.contribution.Contribution; import org.apache.tuscany.sca.data.collection.Entry; import org.apache.tuscany.sca.data.collection.Item; import org.apache.tuscany.sca.data.collection.ItemCollection; @@ -13,9 +15,9 @@ import org.apache.tuscany.sca.data.collection.LocalItemCollection; import org.apache.tuscany.sca.data.collection.NotFoundException; import org.apache.tuscany.sca.domain.search.DomainSearch; import org.apache.tuscany.sca.domain.search.Result; -import org.apache.tuscany.sca.domain.search.impl.ArtifactResult; -import org.apache.tuscany.sca.domain.search.impl.FileContentResult; +import org.apache.tuscany.sca.domain.search.impl.DomainSearchFormatter; import org.apache.tuscany.sca.domain.search.impl.HighlightingUtil; +import org.apache.tuscany.sca.domain.search.impl.SearchFields; import org.osoa.sca.annotations.Reference; import org.osoa.sca.annotations.Scope; import org.osoa.sca.annotations.Service; @@ -25,20 +27,29 @@ import org.osoa.sca.annotations.Service; public class Searcher implements ItemCollection, LocalItemCollection { static char[] characters = { '\u0001'/*  */, '\u0003'/*  */, - '\u0004'/*  */, - - }; + '\u0004'/*  */, }; final private static String HTML_NEW_LINE = "
"; final private static String PATH_SEPARATOR = "/"; + final private static int MAX_CONTENT_LINE_WIDTH = 300; + + final private static String HIGHLIGHT_START = ""; + + final private static String HIGHLIGHT_END = ""; + @Reference - public WorkspaceReader contributionCollection; + public ContributionsReader contributionReader; + + @Reference + public LocalItemCollection contributionCollection; @Reference public DomainSearch domainSearch; + private int elementCounter; + public void delete(String key) throws NotFoundException { System.out.println("delete"); } @@ -56,35 +67,159 @@ public class Searcher implements ItemCollection, LocalItemCollection { } public Item get(String key) throws NotFoundException { - System.out.println("get"); - this.domainSearch.contributionAdded(null, this.contributionCollection - .getWorkspace()); - Result[] results = this.domainSearch.parseAndSearch(key, false); + + try { + + if (key.startsWith("highlight")) { + int lastSemicolonIndex = key.lastIndexOf(";"); + String artifact = key.substring(lastSemicolonIndex + 1); + int secondLastSemicolonIndex = key.lastIndexOf(";", + lastSemicolonIndex - 1); + String contribution = key.substring( + secondLastSemicolonIndex + 1, lastSemicolonIndex); + String query = key.substring("highlight".length(), + secondLastSemicolonIndex); + + return highlightArtifact(contribution, artifact, query); + + } else if (key.startsWith("query")) { + return executeQuery(key.substring("query".length())); + + } else { + throw new NotFoundException("Invalid operation!"); + } + + } catch (Exception t) { + + if (t instanceof NotFoundException) { + throw (NotFoundException) t; + } + + throw new NotFoundException("Internal error!"); + + } + + } + + private Item highlightArtifact(String contribution, String artifact, + String query) throws NotFoundException { + + // Item item = this.contributionCollection.get(contribution); + // + // if (item == null) { + // throw new NotFoundException("contribution not found: " + + // contribution); + // } + // + // String location = item.getAlternate(); + // + // if (location.endsWith(".jar") || location.endsWith(".zip")) { + // location = "jar:" + (location.startsWith("file:") ? "" : "file:") + + // location + '!' + (artifact.startsWith("/") ? "" : "/") + artifact; + // + // } else { + // location += (location.endsWith("/") ? "" : "/") + artifact; + // } + // + // try { + // Reader reader = new InputStreamReader(new + // URL(location).openStream()); + // StringBuilder sb = new StringBuilder(); + // int c; + // + // // TODO: load the chars into an array buffer instead of one + // // at a + // // time + // while ((c = reader.read()) != -1) { + // char character = (char) c; + // + // if (!Character.isIdentifierIgnorable(character)) { + // sb.append(character); + // } + // + // } + // + // String highlightedText = + // this.domainSearch.highlight(SearchFields.FILE_CONTENT_FIELD, + // sb.toString(), query); + // highlightedText = + // HighlightingUtil.replaceHighlightMarkupBy(highlightedText, + // HIGHLIGHT_START, HIGHLIGHT_END); + // + // item = new Item(); + // item.setTitle("Highlighted Artifact"); + // item.setContents(highlightedText); + // + // return item; + // + // } catch (Exception e) { + // throw new NotFoundException("Could not highlight artifact: " + + // e.getMessage(), e); + // } + + return null; + + } + + private Item executeQuery(String query) throws NotFoundException { + + if (!this.domainSearch.indexExists()) { + + Contribution[] contributions = this.contributionReader + .readContributions(); + + for (Contribution contribution : contributions) { + + if (!contribution.getURI().equals( + DomainManagerUtil.DEPLOYMENT_CONTRIBUTION_URI)) { + + this.domainSearch.contributionUpdated(contribution, + contribution); + + } + + } + + } + + Result[] results; + try { + results = this.domainSearch.parseAndSearch(query, true); + + } catch (Exception e1) { + + throw new NotFoundException("Exception while searching: " + + e1.getMessage(), e1); + + } Item item = new Item(); item.setTitle("Results"); StringWriter sw = new StringWriter(); + this.elementCounter = 0; - for (Result result : results) { + if (results.length > 0) { - try { - writeToHTML(0, result, sw); + for (Result result : results) { - } catch (IOException e) { - // ignore result - } + try { + writeToHTML(0, result, sw); - sw.write(HTML_NEW_LINE); + } catch (IOException e) { + // ignore result + } - } + } + + String contents = HighlightingUtil.replaceHighlightMarkupBy(sw + .getBuffer(), HIGHLIGHT_START, HIGHLIGHT_END); - String contents = HighlightingUtil.replaceHighlightMarkupBy(sw - .getBuffer(), "", ""); + item.setContents(replaceAll(contents, 40) + "end"); - item.setContents(replaceAll(contents, 40) + "end"); - - System.out.println(item.getContents().indexOf("@Referencevar")); + } else { + item.setContents("No results match: " + query + ""); + } System.out.println(item.getContents()); @@ -148,7 +283,9 @@ public class Searcher implements ItemCollection, LocalItemCollection { if (actual > less || actual == '&' || actual == '#' || actual == '\'' || actual == ' ' || actual == '%' - || actual == ':') { + || actual == ':' || actual == '(' || actual == ')' + || actual == '"') { + sb.append(actual); } else { @@ -171,100 +308,168 @@ public class Searcher implements ItemCollection, LocalItemCollection { } - private static Result writeFileContentResultToHTML(int indentation, - Result result, StringWriter writer) throws IOException { + private static String getIconLocationForResult(Result result) { - String content = ((FileContentResult) result).getContent(); - writer.write("file:"); + if (SearchFields.COMPONENT_FIELD.equals(result.getField())) { + return "icons/component.png' title='Component"; - if (content != null && content.length() > 0) { + } else if (SearchFields.COMPOSITE_FIELD.equals(result.getField())) { + return "icons/composite.png' title='Composite"; - StringEscapeUtils.escapeHtml(writer, result.getName()); - writer.write(HTML_NEW_LINE); + } else if (SearchFields.ARTIFACT_FIELD.equals(result.getField())) { + return "icons/artifact.png' title='Artifact"; + } - startIndentation(indentation, writer); - writer.write(""); - StringEscapeUtils.escapeHtml(writer, content); - writer.write(""); - endIndentation(writer); - writer.write(HTML_NEW_LINE); + return "icons/feed-icon.png"; - } else { + } - writer.write(result.getName()); - Collection contents = result.getContents().values(); + private static void writeResultIcon(Writer writer, Result result) + throws IOException { + writer.write(" "); - while (contents.size() == 1) { - result = contents.iterator().next(); + } - if (result instanceof FileContentResult) { - writer.write(PATH_SEPARATOR); - writer.write(result.getName()); + private static Result writeFileContentResultToHTML(int indentation, + Result result, StringWriter writer) throws IOException { - contents = result.getContents().values(); + Map contents = result.getContents(); + writeResultIcon(writer, result); - } else { - break; - } + if (contents.size() == 1 + && SearchFields.ARTIFACT_FIELD.equals(contents.values() + .iterator().next().getField())) { - } - - if (result instanceof FileContentResult) { - FileContentResult fileResult = (FileContentResult) result; - content = fileResult.getContent(); + writer.write(result.getValue()); + contents = result.getContents(); - if (content != null && content.length() > 0) { + while (contents.size() == 1 + && SearchFields.ARTIFACT_FIELD.equals(contents.values() + .iterator().next().getField())) { - writer.write(HTML_NEW_LINE); + result = contents.values().iterator().next(); - startIndentation(indentation, writer); - writer.write(""); - StringEscapeUtils.escapeHtml(writer, content); - writer.write(""); - endIndentation(writer); - writer.write(HTML_NEW_LINE); + writer.write(PATH_SEPARATOR); + StringEscapeUtils.escapeHtml(writer, result.getValue()); - } + contents = result.getContents(); } + } else { + StringEscapeUtils.escapeHtml(writer, result.getValue()); } - + return result; } - private static void writeToHTML(int indentation, Result result, - StringWriter writer) throws IOException { + private static String removeHighlighting(String text) { + return HighlightingUtil.replaceHighlightMarkupBy(text, "", ""); + } - startIndentation(indentation++, writer); + private static void writeResultName(Result result, StringWriter writer) + throws IOException { + + if (SearchFields.CONTRIBUTION_FIELD.equals(result.getField())) { + writer.write(""); + writer.write(StringEscapeUtils.escapeHtml(result.getValue())); + writer.write(""); + + } else if (SearchFields.COMPOSITE_FIELD.equals(result.getField())) { + writer.write(""); + writer.write(StringEscapeUtils.escapeHtml(result.getValue())); + writer.write(""); - if (result instanceof FileContentResult) { - result = writeFileContentResultToHTML(indentation, result, writer); + } else { + StringEscapeUtils.escapeHtml(writer, result.getValue()); + } + + } + + private static String getContributionURI(Result result) { + + if (result == null) { + return ""; + + } else if (SearchFields.CONTRIBUTION_FIELD.equals(result.getField())) { + return removeHighlighting(result.getValue()); } else { + return getContributionURI(result.getContainer()); + } + + } + + private void writeToHTML(int indentation, Result result, StringWriter writer) + throws IOException { - StringEscapeUtils.escapeHtml(writer, result.getClass() - .getSimpleName().substring(0, - result.getClass().getSimpleName().length() - 6)); - writer.write(':'); - StringEscapeUtils.escapeHtml(writer, result.getName()); - writer.write(HTML_NEW_LINE); - - if (result instanceof ArtifactResult) { - String location = ((ArtifactResult) result).getLocation(); - - if (location != null) { - startIndentation(indentation, writer); - writer.write("location: "); - StringEscapeUtils.escapeHtml(writer, location); - endIndentation(writer); + startIndentation(indentation++, writer); + String field = result.getField(); + + if (SearchFields.FILE_CONTENT_FIELD.equals(field)) { + String content = result.getValue(); + + if (content != null && content.length() > 0 + && DomainSearchFormatter.isHighlighted(content)) { + + writer.write(HTML_NEW_LINE); + + this.elementCounter++; + writer.write("
" + + "

" + + "view all  view all  download

"); + + int i = 0; + while (i < content.length()) { + StringEscapeUtils.escapeHtml(writer, content.substring(i, + Math.min(i + MAX_CONTENT_LINE_WIDTH, content + .length()))); writer.write(HTML_NEW_LINE); + i += MAX_CONTENT_LINE_WIDTH; } + writer.write("

"); + } + } else if (SearchFields.ARTIFACT_FIELD.equals(field)) { + result = writeFileContentResultToHTML(indentation, result, writer); + + } else { + + writeResultIcon(writer, result); + writeResultName(result, writer); + } endIndentation(writer); -- cgit v1.2.3