• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

opensrp / opensrp-server-core / #162

pending completion
#162

Pull #621

Github

web-flow
Merge b5d4652eb into 68d7f3ff3
Pull Request #621: update selectStructureAndFamilyWithinRadius query

8286 of 10652 relevant lines covered (77.79%)

0.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/org/opensrp/util/FileCreator.java
1
/**
2
 * Contributors: muhammad.ahmed@ihsinformatics.com
3
 */
4
package org.opensrp.util;
5

6
import java.io.File;
7
import java.io.FileNotFoundException;
8
import java.io.FileOutputStream;
9
import java.io.IOException;
10
import java.io.StringReader;
11
import java.io.StringWriter;
12

13
import javax.xml.parsers.DocumentBuilderFactory;
14
import javax.xml.transform.OutputKeys;
15
import javax.xml.transform.Source;
16
import javax.xml.transform.Transformer;
17
import javax.xml.transform.TransformerFactory;
18
import javax.xml.transform.dom.DOMSource;
19
import javax.xml.transform.stream.StreamResult;
20
import javax.xml.transform.stream.StreamSource;
21

22
import org.w3c.dom.Node;
23
import org.xml.sax.InputSource;
24

25
public class FileCreator {
×
26
        
27
        // private static String directory=null;//= System.getProperty("user.home");
28
        
29
        // public FileCreator() {
30
        // directory = System.getProperty("user.home");
31
        //
32
        // }
33
        
34
        public void createFile(String filename, String directory, byte[] content) throws FileNotFoundException, IOException {
35
                
36
                File f = new File(directory);
×
37
                if (f.mkdirs()) {
×
38
                        
39
                }
40
                //System.out.println(s);
41
                FileOutputStream fos2 = new FileOutputStream(f.getPath() + System.getProperty("file.separator") + filename);
×
42
                fos2.write(content);
×
43
                fos2.close();
×
44
                
45
        }
×
46
        
47
        public String createDirectory(String directory) {
48
                
49
                File file = new File(osDirectorySet(directory));
×
50
                if (!file.exists()) {
×
51
                        if (file.mkdir()) {
×
52
                                System.out.println("Directory is created!");
×
53
                        } else {
54
                                System.out.println("Failed to create directory!");
×
55
                        }
56
                }
57
                
58
                return file.getAbsolutePath();
×
59
        }
60
        
61
        public boolean createFormFiles(String directory, String formId, byte[] form, byte[] model, byte[] formjson) {
62
                
63
                try {
64
                        //        System.out.println("before creating files "+directory);
65
                        createFile("form.xml", directory, form);
×
66
                        createFile("model.xml", directory, model);
×
67
                        createFile("form.json", directory, formjson);
×
68
                        //        System.out.println("before creating files "+directory);
69
                        return true;
×
70
                }
71
                catch (Exception e) {
×
72
                        e.printStackTrace();
×
73
                }
74
                return false;
×
75
        }
76
        
77
        public boolean createTextFile(String directory, byte[] context, String formId) {
78
                try {
79
                        directory = createDirectory(directory);
×
80
                        createFile(formId + ".txt", directory, context);
×
81
                        
82
                        return true;
×
83
                }
84
                catch (Exception e) {
×
85
                        e.printStackTrace();
×
86
                }
87
                return false;
×
88
        }
89
        
90
        public boolean createModelFile(String directory, String formId, byte[] context) {
91
                try {
92
                        directory = createDirectory(directory);
×
93
                        createFile("model.xml", directory, context);
×
94
                        
95
                        return true;
×
96
                }
97
                catch (Exception e) {
×
98
                        e.printStackTrace();
×
99
                }
100
                return false;
×
101
                
102
        }
103
        
104
        public boolean createFormFile(String directory, String formId, byte[] context) {
105
                try {
106
                        directory = createDirectory(directory);
×
107
                        createFile("form.xml", directory, context);
×
108
                        
109
                        return true;
×
110
                }
111
                catch (Exception e) {
×
112
                        e.printStackTrace();
×
113
                }
114
                return false;
×
115
        }
116
        
117
        public boolean createFormJsonFile(String directory, String formId, byte[] context) {
118
                try {
119
                        directory = createDirectory(directory);
×
120
                        createFile("form.json", directory, context);
×
121
                        
122
                        return true;
×
123
                }
124
                catch (Exception e) {
×
125
                        e.printStackTrace();
×
126
                }
127
                return false;
×
128
        }
129
        
130
        public String osDirectorySet(String name) {
131
                
132
                if (name.startsWith("/")) {
×
133
                        name += "/";
×
134
                        // directory += "/"+name+"/";
135
                } else {
136
                        // directory += "\\"+name+"\\";
137
                        name += "\\";
×
138
                }
139
                return name;
×
140
        }
141
        
142
        private String prettyFormat(String input, int indent) {
143
                try {
144
                        //                        Source xmlInput = new StreamSource(new StringReader(input));
145
                        //                        StringWriter stringWriter = new StringWriter();
146
                        //                        StreamResult xmlOutput = new StreamResult(new OutputStreamWriter(System.out));
147
                        //                        
148
                        final InputSource src = new InputSource(new StringReader(input));
×
149
                        final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
×
150
                        
151
                        Transformer transformer = TransformerFactory.newInstance().newTransformer();
×
152
                        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
×
153
                        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
×
154
                        //initialize StreamResult with File object to save to file
155
                        StreamResult result = new StreamResult(new StringWriter());
×
156
                        DOMSource source = new DOMSource(document);
×
157
                        transformer.transform(source, result);
×
158
                        String xmlString = result.getWriter().toString();
×
159
                        System.out.println(xmlString);
×
160
                        
161
                        return xmlString;
×
162
                }
163
                catch (Throwable e) {
×
164
                        e.printStackTrace();
×
165
                        // You'll come here if you are using JDK 1.5
166
                        // you are getting an the following exeption
167
                        // java.lang.IllegalArgumentException: Not supported: indent-number
168
                        // Use this code (Set the output property in transformer.
169
                        try {
170
                                Source xmlInput = new StreamSource(new StringReader(input));
×
171
                                StringWriter stringWriter = new StringWriter();
×
172
                                StreamResult xmlOutput = new StreamResult(stringWriter);
×
173
                                TransformerFactory transformerFactory = TransformerFactory.newInstance();
×
174
                                Transformer transformer = transformerFactory.newTransformer();
×
175
                                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
×
176
                                transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
×
177
                                transformer.transform(xmlInput, xmlOutput);
×
178
                                return xmlOutput.getWriter().toString();
×
179
                        }
180
                        catch (Throwable t) {
×
181
                                return input;
×
182
                        }
183
                }
184
        }
185
        
186
        public String prettyFormat(String input) {
187
                return prettyFormat(input, 2);
×
188
        }
189
        
190
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc