001package com.streamconverter.examples;
002
003import com.streamconverter.StreamConverter;
004import com.streamconverter.command.IStreamCommand;
005import com.streamconverter.command.impl.SampleStreamCommand;
006import com.streamconverter.command.impl.csv.CsvNavigateCommand;
007import com.streamconverter.command.impl.json.JsonNavigateCommand;
008import com.streamconverter.command.impl.xml.XmlNavigateCommand;
009import com.streamconverter.command.rule.PassThroughRule;
010import com.streamconverter.path.CSVPath;
011import com.streamconverter.path.TreePath;
012import java.io.ByteArrayInputStream;
013import java.io.ByteArrayOutputStream;
014import java.io.IOException;
015import java.io.InputStream;
016import java.nio.charset.StandardCharsets;
017import org.slf4j.Logger;
018import org.slf4j.LoggerFactory;
019
020/**
021 * Quick start examples for StreamConverter.
022 *
023 * <p>This provides simple, runnable examples of core functionality.
024 */
025public class QuickStart {
026  private static final Logger log = LoggerFactory.getLogger(QuickStart.class);
027
028  /**
029   * アプリケーションのエントリーポイント。StreamConverterの基本的な使用例を実行します。
030   *
031   * @param args コマンドライン引数(使用されません)
032   */
033  public static void main(String[] args) {
034    log.info("🚀 StreamConverter Quick Start Examples");
035    log.info("========================================\n");
036
037    try {
038      // Example 1: CSV column extraction
039      csvExample();
040
041      // Example 2: JSON property extraction
042      jsonExample();
043
044      // Example 3: XML element extraction
045      xmlExample();
046
047      // Example 4: Command pipeline
048      pipelineExample();
049
050      log.info("✅ All examples completed successfully!");
051
052    } catch (Exception e) {
053      log.error("❌ Example failed: {}", e.getMessage(), e);
054    }
055  }
056
057  /** Example 1: CSV column extraction */
058  private static void csvExample() throws IOException {
059    log.info("📊 CSV Example");
060    log.info("===============");
061
062    String csvData = "name,age,city\nJohn,30,NYC\nJane,25,LA\n";
063
064    // Extract name column
065    IStreamCommand csvCommand =
066        CsvNavigateCommand.create(new CSVPath("name"), new PassThroughRule());
067    String result = processData(csvData, csvCommand);
068
069    log.info("Input CSV:");
070    log.info(csvData);
071    log.info("Extracted 'name' column:");
072    log.info(result);
073    log.info("");
074  }
075
076  /** Example 2: JSON property extraction */
077  private static void jsonExample() throws IOException {
078    log.info("🔍 JSON Example");
079    log.info("================");
080
081    String jsonData = "{\"name\":\"John\",\"age\":30,\"city\":\"NYC\"}";
082
083    // Extract name property (JSONPath style)
084    IStreamCommand jsonCommand =
085        JsonNavigateCommand.create(TreePath.fromJson("$.name"), new PassThroughRule());
086    String result = processData(jsonData, jsonCommand);
087
088    log.info("Input JSON:");
089    log.info(jsonData);
090    log.info("Extracted 'name' property:");
091    log.info(result);
092    log.info("");
093  }
094
095  /** Example 3: XML element extraction */
096  private static void xmlExample() throws IOException {
097    log.info("🌲 XML Example");
098    log.info("===============");
099
100    String xmlData =
101        """
102        <?xml version="1.0"?>
103        <person>
104          <name>John</name>
105          <age>30</age>
106          <city>NYC</city>
107        </person>
108        """;
109
110    // Extract name element
111    IStreamCommand xmlCommand =
112        XmlNavigateCommand.create(TreePath.fromXml("person/name"), new PassThroughRule());
113    String result = processData(xmlData, xmlCommand);
114
115    log.info("Input XML:");
116    log.info(xmlData);
117    log.info("Extracted 'name' element:");
118    log.info(result);
119    log.info("");
120  }
121
122  /** Example 4: Command pipeline */
123  private static void pipelineExample() throws IOException {
124    log.info("🔗 Pipeline Example");
125    log.info("====================");
126
127    String csvData = "id,name,status\n1,John,active\n2,Jane,inactive\n";
128
129    // Create processing pipeline
130    IStreamCommand[] pipeline = {
131      CsvNavigateCommand.create(new CSVPath("name"), new PassThroughRule()), // Extract names
132      new SampleStreamCommand("processor") // Process names
133    };
134
135    String result = processData(csvData, pipeline);
136
137    log.info("Input CSV:");
138    log.info(csvData);
139    log.info("Pipeline result (name extraction + processing):");
140    log.info(result);
141    log.info("");
142  }
143
144  /** Helper method to process data with a single command */
145  private static String processData(String inputData, IStreamCommand command) throws IOException {
146    return processData(inputData, new IStreamCommand[] {command});
147  }
148
149  /** Helper method to process data with command pipeline */
150  private static String processData(String inputData, IStreamCommand[] commands)
151      throws IOException {
152    try (InputStream inputStream =
153            new ByteArrayInputStream(inputData.getBytes(StandardCharsets.UTF_8));
154        ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
155
156      StreamConverter converter = new StreamConverter(commands);
157      converter.run(inputStream, outputStream);
158
159      return outputStream.toString(StandardCharsets.UTF_8);
160    }
161  }
162}