/** * Finds the requested script. * * <p> * If no such script, returns null. * </p> * * @param name name of the script to find. * @return the found script, or <i>null</i> if not found. */ @Nullable public final TextFileFragment findText(@NotNull String name) { TextFileFragment fragment; final String nameWithDialect = myDialect == null ? null : name + '+' + myDialect; for (int i = myResources.length-1; i >= 0; i--) { ScriptumResource r = myResources[i]; if (nameWithDialect != null) { fragment = r.find(nameWithDialect); if (fragment != null) return fragment; } fragment = r.find(name); if (fragment != null) return fragment; } return null; }
/** * Lists name of all fragments. * @return names, preserving the original order. */ @NotNull public final List<String> listFragmentNames() { ArrayList<String> names = new ArrayList<String>(); for (ScriptumResource resource : myResources) names.addAll(Arrays.asList(resource.getExistentNames())); return names; }
int count() { loadIfNeeded(); return myFragments.length; }
protected void loadIfNeeded() { if (myFragments == null) { myFragments = loadFragments(); myFragmentsMap = new TreeMap<String, TextFileFragment>(String.CASE_INSENSITIVE_ORDER); for (TextFileFragment fragment : myFragments) { if (fragment.getFragmentName() != null) { myFragmentsMap.put(fragment.getFragmentName(), fragment); } } } }
@Nullable TextFileFragment find(@NotNull final String name) { loadIfNeeded(); return myFragmentsMap.get(name); }
boolean was = false; for (ScriptumResource r : myResources) { for (String existentName : r.getExistentNames()) { if (!was) { b.append("There are scripts: ");
@NotNull TextFileFragment get(@NotNull final String name) { TextFileFragment fragment = find(name); if (fragment == null) { if (myFragmentsMap == null) { throw new IllegalStateException("The scriptum resource is not loaded yet."); } else { String msg = "No such fragment with name: " + name + '\n' + collectionToString(myFragmentsMap.keySet(), ", ", "There are fragments: ", ".", "This resource is empty"); throw new IllegalArgumentException(msg); } } return fragment; }
/** * Lists name of all fragments. * @return names, preserving the original order. */ @NotNull public final List<TextFileFragment> listFragments() { ArrayList<TextFileFragment> fragments = new ArrayList<TextFileFragment>(); for (ScriptumResource resource : myResources) { resource.loadIfNeeded(); fragments.addAll(Arrays.asList(resource.myFragments)); } return fragments; }
@NotNull String[] getExistentNames() { loadIfNeeded(); int n = myFragments.length; String[] names = new String[n]; for (int i = 0; i < n; i++) names[i] = myFragments[i].getFragmentName(); return names; }