Example #1
0
 public OptionalEntity<DictionaryFile<? extends DictionaryItem>> getDictionaryFile(
     final String id) {
   for (final DictionaryFile<? extends DictionaryItem> dictFile : getDictionaryFiles()) {
     if (dictFile.getId().equals(id)) {
       return OptionalEntity.of(dictFile);
     }
   }
   return OptionalEntity.empty();
 }
Example #2
0
 public InputStream getContentInputStream(
     final DictionaryFile<? extends DictionaryItem> dictFile) {
   final FessConfig fessConfig = ComponentUtil.getFessConfig();
   try {
     return Curl.get(fessConfig.getElasticsearchUrl() + "/_configsync/file")
         .param("path", dictFile.getPath())
         .execute()
         .getContentAsStream();
   } catch (final IOException e) {
     throw new DictionaryException("Failed to access " + dictFile.getPath(), e);
   }
 }
 /** Creates the location definitions */
 private void createLocations() {
   ArrayList<DictionaryFileRef> files = new ArrayList<DictionaryFileRef>();
   LocationStore.startLocations();
   for (TraceGroup group : dictionaryFile.getOwner().getModel()) {
     for (Trace trace : group) {
       writeLocation(files, trace);
     }
   }
   // Build XML and assign ID's to refs
   Collections.sort(files, new FileRefComparator());
   String lastpath = null;
   for (DictionaryFileRef ref : files) {
     if (!ref.path.equals(lastpath)) {
       if (lastpath != null) {
         LocationStore.endPath();
       }
       LocationStore.startPath(ref.path);
       lastpath = ref.path;
     }
     LocationStore.writeFile(++fileref, ref.file);
     ref.refid = fileref;
   }
   if (lastpath != null) {
     LocationStore.endPath();
   }
   LocationStore.endLocations();
 }
  /**
   * Writes the data type definitions
   *
   * @param typeList the list of data types
   */
  private void writeTypeDefinitions(ArrayList<String> typeList) {
    for (String type : typeList) {
      DataType dt = mapTypeToDataType(type);
      if (dt != null) {
        // Normal parameters
        int size = mapParameterTypeToSize(type);
        String formatChar = SourceUtils.mapNormalTypeToFormat(type);
        if (formatChar != null && formatChar.length() > 1 && formatChar.charAt(0) == '%') {
          formatChar = formatChar.substring(1);
          TypeDefStore.writeTypeDef(type, size, formatChar, dt);

          // Write alternative format characters
          writeAlternativeFormatChars(formatChar, type, size, dt);

        } else {
          TypeDefStore.writeTypeDef(type, size, null, dt);
        }
      } else {
        // Enum parameters
        TraceModel model = dictionaryFile.getOwner().getModel();
        TraceConstantTable table = model.findConstantTableByName(type);
        if (table != null) {
          TypeDefStore.startTypeDef(
              table.getName(), mapParameterTypeToSize(table.getType()), null, DataType.ENUM);
          for (TraceConstantTableEntry entry : table) {
            TypeDef.writeTypeMember(entry.getID(), entry.getName(), null);
          }
          TypeDefStore.endTypeDef();
        }
      }
    }
  }
Example #5
0
  public void store(final DictionaryFile<? extends DictionaryItem> dictFile, final File file) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    getDictionaryFile(dictFile.getId())
        .ifPresent(
            currentFile -> {
              if (currentFile.getTimestamp().getTime() > dictFile.getTimestamp().getTime()) {
                throw new DictionaryException(dictFile.getPath() + " was updated.");
              }

              // TODO use stream
              try (CurlResponse response =
                  Curl.post(fessConfig.getElasticsearchUrl() + "/_configsync/file")
                      .param("path", dictFile.getPath())
                      .body(FileUtil.readUTF8(file))
                      .execute()) {
                final Map<String, Object> contentMap = response.getContentAsMap();
                if (!Constants.TRUE.equalsIgnoreCase(contentMap.get("acknowledged").toString())) {
                  throw new DictionaryException("Failed to update " + dictFile.getPath());
                }
              } catch (final IOException e) {
                throw new DictionaryException("Failed to update " + dictFile.getPath(), e);
              }
            })
        .orElse(
            () -> {
              throw new DictionaryException(dictFile.getPath() + " does not exist.");
            });
  }
 /** Creates the component definition */
 private void createComponent() {
   TraceModel model = dictionaryFile.getOwner().getModel();
   int compid = model.getID();
   // Component prefix and suffix are in property file.
   // If not there, the default values are used
   String prefix = TraceObjectUtils.findProperty(model, PropertyNames.PREFIX);
   if (prefix == null || prefix.length() == 0) {
     prefix = FormattingUtils.getDefaultComponentPrefix(model);
   }
   String suffix = TraceObjectUtils.findProperty(model, PropertyNames.SUFFIX);
   if (suffix == null || suffix.length() == 0) {
     suffix = FormattingUtils.getDefaultComponentSuffix(model);
   }
   Dictionary.startComponent(compid, dictionaryFile.getProjectName(), prefix, suffix);
   for (TraceGroup group : model) {
     createGroup(group);
   }
   Dictionary.endComponent();
 }
 /** Creates the trace definitions */
 private void createDefs() {
   // This should check for duplicates
   TraceDataStore.startDataStore();
   TraceModel model = dictionaryFile.getOwner().getModel();
   TraceProjectAPI api = model.getExtension(TraceProjectAPI.class);
   for (TraceGroup group : model) {
     for (Trace trace : group) {
       trace.addExtension(new DictionaryDefRef(++defref));
       TraceFormatFlags flags = new TraceFormatFlags();
       flags.isFormattingSupported = true;
       String data = api.formatTraceForExport(trace, flags);
       data = replaceUnescapeQuotes(data);
       TraceDataStore.writeData(defref, DataType.STRING, data);
     }
   }
   TraceDataStore.endDataStore();
 }
 /**
  * Builds the list of parameter types
  *
  * @return the list of types found from the model
  */
 private ArrayList<String> buildTypeList() {
   TraceModel model = dictionaryFile.getOwner().getModel();
   ArrayList<String> typeList = new ArrayList<String>();
   for (TraceGroup group : model) {
     for (Trace trace : group) {
       for (TraceParameter parameter : trace) {
         if ((parameter.getExtension(HiddenTraceObjectRule.class) == null)
             || (parameter.getExtension(FillerParameterRule.class) != null)) {
           String type = parameter.getType();
           if (!typeList.contains(type)) {
             typeList.add(type);
           }
         }
       }
     }
   }
   return typeList;
 }
  /** Writes the dictionary file */
  void write() {
    defref = 0;
    fileref = 0;
    File file = new File(dictionaryFile.getAbsolutePathWithID());
    // Delete removes possible read-only flags
    if (file.exists()) {
      file.delete();
    }

    TraceDictionaryEncoder encoder = new TraceDictionaryEncoder();
    File path = file.getParentFile();
    if (!path.exists()) {
      FileUtils.createDirectories(path);
    }
    encoder.createNewDictionary(file.getAbsolutePath());
    Dictionary.startDictionary();
    createTypedefs();
    createDefs();
    createLocations();
    createComponent();
    Dictionary.endDictionary();
  }