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 pipeline
039      csvExample();
040
041      // Example 2: JSON pipeline
042      jsonExample();
043
044      // Example 3: XML pipeline
045      xmlExample();
046
047      // Example 4: Multi-step 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 pipeline */
058  private static void csvExample() throws IOException {
059    log.info("📊 CSV Pipeline Example");
060    log.info("========================");
061
062    String csvData = "name,age,city\nJohn,30,NYC\nJane,25,LA\n";
063
064    // Create pipeline: Extract name column + Process
065    // Note: SampleStreamCommand is a placeholder for demonstration.
066    // In real applications, replace with actual processing commands.
067    IStreamCommand[] pipeline = {
068      CsvNavigateCommand.create(new CSVPath("name"), new PassThroughRule()),
069      new SampleStreamCommand("csv-processor")
070    };
071    String result = processData(csvData, pipeline);
072
073    log.info("Input CSV:");
074    log.info(csvData);
075    log.info("Pipeline result (name extraction + processing):");
076    log.info(result);
077    log.info("");
078  }
079
080  /** Example 2: JSON pipeline */
081  private static void jsonExample() throws IOException {
082    log.info("🔍 JSON Pipeline Example");
083    log.info("=========================");
084
085    String jsonData = "{\"name\":\"John\",\"age\":30,\"city\":\"NYC\"}";
086
087    // Create pipeline: Extract name property + Process
088    // Note: SampleStreamCommand is a placeholder for demonstration.
089    // In real applications, replace with actual processing commands.
090    IStreamCommand[] pipeline = {
091      JsonNavigateCommand.create(TreePath.fromJson("$.name"), new PassThroughRule()),
092      new SampleStreamCommand("json-processor")
093    };
094    String result = processData(jsonData, pipeline);
095
096    log.info("Input JSON:");
097    log.info(jsonData);
098    log.info("Pipeline result (name extraction + processing):");
099    log.info(result);
100    log.info("");
101  }
102
103  /** Example 3: XML pipeline */
104  private static void xmlExample() throws IOException {
105    log.info("🌲 XML Pipeline Example");
106    log.info("========================");
107
108    String xmlData =
109        """
110        <?xml version="1.0"?>
111        <person>
112          <name>John</name>
113          <age>30</age>
114          <city>NYC</city>
115        </person>
116        """;
117
118    // Create pipeline: Extract name element + Process
119    // Note: SampleStreamCommand is a placeholder for demonstration.
120    // In real applications, replace with actual processing commands.
121    IStreamCommand[] pipeline = {
122      XmlNavigateCommand.create(TreePath.fromXml("person/name"), new PassThroughRule()),
123      new SampleStreamCommand("xml-processor")
124    };
125    String result = processData(xmlData, pipeline);
126
127    log.info("Input XML:");
128    log.info(xmlData);
129    log.info("Pipeline result (name extraction + processing):");
130    log.info(result);
131    log.info("");
132  }
133
134  /** Example 4: Multi-step pipeline */
135  private static void pipelineExample() throws IOException {
136    log.info("🔗 Multi-Step Pipeline Example");
137    log.info("================================");
138
139    String csvData = "id,name,status\n1,John,active\n2,Jane,inactive\n";
140
141    // Create processing pipeline
142    IStreamCommand[] pipeline = {
143      CsvNavigateCommand.create(new CSVPath("name"), new PassThroughRule()), // Extract names
144      new SampleStreamCommand("processor") // Process names
145    };
146
147    String result = processData(csvData, pipeline);
148
149    log.info("Input CSV:");
150    log.info(csvData);
151    log.info("Pipeline result (name extraction + processing):");
152    log.info(result);
153    log.info("");
154  }
155
156  /** Helper method to process data with command pipeline */
157  private static String processData(String inputData, IStreamCommand[] commands)
158      throws IOException {
159    try (InputStream inputStream =
160            new ByteArrayInputStream(inputData.getBytes(StandardCharsets.UTF_8));
161        ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
162
163      StreamConverter converter = new StreamConverter(commands);
164      converter.run(inputStream, outputStream);
165
166      return outputStream.toString(StandardCharsets.UTF_8);
167    }
168  }
169}