|
| 1 | +import 'dart:typed_data'; |
| 2 | +import 'package:archive/archive.dart'; |
| 3 | +import 'dart:convert'; |
| 4 | + |
| 5 | +/// Helper class to generate test DOCX files for testing purposes |
| 6 | +class TestDocxGenerator { |
| 7 | + /// Creates a simple DOCX file with the given text content |
| 8 | + static Uint8List createSimpleDocx(String text) { |
| 9 | + final archive = Archive(); |
| 10 | + |
| 11 | + // Create the required DOCX structure |
| 12 | + _addContentTypes(archive); |
| 13 | + _addRels(archive); |
| 14 | + _addDocument(archive, text); |
| 15 | + |
| 16 | + // Encode the archive as a ZIP file |
| 17 | + final zipEncoder = ZipEncoder(); |
| 18 | + final zipBytes = zipEncoder.encode(archive); |
| 19 | + return Uint8List.fromList(zipBytes!); |
| 20 | + } |
| 21 | + |
| 22 | + /// Creates a DOCX file with numbered list |
| 23 | + static Uint8List createDocxWithNumbering(List<String> items) { |
| 24 | + final archive = Archive(); |
| 25 | + |
| 26 | + // Create the required DOCX structure |
| 27 | + _addContentTypes(archive); |
| 28 | + _addRels(archive); |
| 29 | + _addDocumentWithNumbering(archive, items); |
| 30 | + |
| 31 | + // Encode the archive as a ZIP file |
| 32 | + final zipEncoder = ZipEncoder(); |
| 33 | + final zipBytes = zipEncoder.encode(archive); |
| 34 | + return Uint8List.fromList(zipBytes!); |
| 35 | + } |
| 36 | + |
| 37 | + /// Creates an empty DOCX file |
| 38 | + static Uint8List createEmptyDocx() { |
| 39 | + return createSimpleDocx(''); |
| 40 | + } |
| 41 | + |
| 42 | + /// Creates a DOCX file with multiple paragraphs |
| 43 | + static Uint8List createDocxWithMultipleParagraphs(List<String> paragraphs) { |
| 44 | + final archive = Archive(); |
| 45 | + |
| 46 | + // Create the required DOCX structure |
| 47 | + _addContentTypes(archive); |
| 48 | + _addRels(archive); |
| 49 | + _addDocumentWithParagraphs(archive, paragraphs); |
| 50 | + |
| 51 | + // Encode the archive as a ZIP file |
| 52 | + final zipEncoder = ZipEncoder(); |
| 53 | + final zipBytes = zipEncoder.encode(archive); |
| 54 | + return Uint8List.fromList(zipBytes!); |
| 55 | + } |
| 56 | + |
| 57 | + static void _addContentTypes(Archive archive) { |
| 58 | + const contentTypesXml = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 59 | +<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"> |
| 60 | + <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/> |
| 61 | + <Default Extension="xml" ContentType="application/xml"/> |
| 62 | + <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/> |
| 63 | +</Types>'''; |
| 64 | + |
| 65 | + final file = ArchiveFile('[Content_Types].xml', contentTypesXml.length, |
| 66 | + utf8.encode(contentTypesXml)); |
| 67 | + archive.addFile(file); |
| 68 | + } |
| 69 | + |
| 70 | + static void _addRels(Archive archive) { |
| 71 | + const relsXml = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 72 | +<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> |
| 73 | + <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/> |
| 74 | +</Relationships>'''; |
| 75 | + |
| 76 | + final file = |
| 77 | + ArchiveFile('_rels/.rels', relsXml.length, utf8.encode(relsXml)); |
| 78 | + archive.addFile(file); |
| 79 | + } |
| 80 | + |
| 81 | + static void _addDocument(Archive archive, String text) { |
| 82 | + final documentXml = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 83 | +<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> |
| 84 | + <w:body> |
| 85 | + <w:p> |
| 86 | + <w:r> |
| 87 | + <w:t>$text</w:t> |
| 88 | + </w:r> |
| 89 | + </w:p> |
| 90 | + </w:body> |
| 91 | +</w:document>'''; |
| 92 | + |
| 93 | + final file = ArchiveFile( |
| 94 | + 'word/document.xml', documentXml.length, utf8.encode(documentXml)); |
| 95 | + archive.addFile(file); |
| 96 | + } |
| 97 | + |
| 98 | + static void _addDocumentWithNumbering(Archive archive, List<String> items) { |
| 99 | + final paragraphs = items.asMap().entries.map((entry) { |
| 100 | + return ''' |
| 101 | + <w:p> |
| 102 | + <w:pPr> |
| 103 | + <w:numPr> |
| 104 | + <w:ilvl w:val="0"/> |
| 105 | + <w:numId w:val="1"/> |
| 106 | + </w:numPr> |
| 107 | + </w:pPr> |
| 108 | + <w:r> |
| 109 | + <w:t>${entry.value}</w:t> |
| 110 | + </w:r> |
| 111 | + </w:p>'''; |
| 112 | + }).join('\n'); |
| 113 | + |
| 114 | + final documentXml = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 115 | +<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> |
| 116 | + <w:body> |
| 117 | +$paragraphs |
| 118 | + </w:body> |
| 119 | +</w:document>'''; |
| 120 | + |
| 121 | + final file = ArchiveFile( |
| 122 | + 'word/document.xml', documentXml.length, utf8.encode(documentXml)); |
| 123 | + archive.addFile(file); |
| 124 | + } |
| 125 | + |
| 126 | + static void _addDocumentWithParagraphs( |
| 127 | + Archive archive, List<String> paragraphs) { |
| 128 | + final paragraphsXml = paragraphs.map((text) { |
| 129 | + return ''' |
| 130 | + <w:p> |
| 131 | + <w:r> |
| 132 | + <w:t>$text</w:t> |
| 133 | + </w:r> |
| 134 | + </w:p>'''; |
| 135 | + }).join('\n'); |
| 136 | + |
| 137 | + final documentXml = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 138 | +<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> |
| 139 | + <w:body> |
| 140 | +$paragraphsXml |
| 141 | + </w:body> |
| 142 | +</w:document>'''; |
| 143 | + |
| 144 | + final file = ArchiveFile( |
| 145 | + 'word/document.xml', documentXml.length, utf8.encode(documentXml)); |
| 146 | + archive.addFile(file); |
| 147 | + } |
| 148 | +} |
0 commit comments