5
\$\begingroup\$

I am attempting to create XML forms using user input to populate certain areas that would appear when loaded into a STIG viewer. Right now everything is working enough for me to create this and have it work just fine.

Is there a way to throw the lower portion which contains the rules and whatnot into a method and have it loop through multiple times while storing the previous values still? I want to use the one STIG item I've made and use that as a means to build more and more until eventually they person would type exit or something to break the loop.

package helloxml;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import java.util.Scanner;

public class HelloXML {

  public static void main(String[] args) {

    try {
        HelloXML.writeToXML();
    } catch (ParserConfigurationException | IOException ex) {
        Logger.getLogger(HelloXML.class.getName()).log(Level.SEVERE, null, ex);
    }

}

public static void writeToXML() throws ParserConfigurationException, FileNotFoundException, IOException {

    String stigListTitle = stigListTitle();
    String listCreationDate = listCreationDate();
    String ruleTitle = ruleTitle();
    String ruleDescription = ruleDescription();
    String fixText = fixText();

    //DocumentBuilderFactory
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    //DocumentBuilder
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    //Document
    Document xmlDoc = docBuilder.newDocument();

    //XML header
    Element rootElement = xmlDoc.createElement("Benchmark");
    rootElement.setAttribute("xmlns:dsig", "http://www.w3.org/2000/09/xmldsig#");
    rootElement.setAttribute("xmlns:xhtml", "http://www.w3.org/1999/xhtml");
    rootElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    rootElement.setAttribute("xmlns:cpe", "http://cpe.mitre.org/language/2.0");
    rootElement.setAttribute("xmlns:dc", "http://purl.org/dc/elements/1.1/");
    rootElement.setAttribute("id", "TASKORDS created POAMs");
    rootElement.setAttribute("xml:lang", "en");
    rootElement.setAttribute("xsi:schemaLocation", "http://checklists.nist.gov/xccdf/1.1 http://nvd.nist.gov/schema/xccdf-1.1.4.xsd http://cpe.mitre.org/dictionary/2.0 http://cpe.mitre.org/files/cpe-dictionary_2.1.xsd");
    rootElement.setAttribute("xmlns", "http://checklists.nist.gov/xccdf/1.1");

    //XML Format
    //Element rootElement = xmlDoc.createElement("benchmark");
    //status date
    Element mainStatusDate = xmlDoc.createElement("status");
    mainStatusDate.setAttribute("date", "2015-11-16");
    Text statusDate = xmlDoc.createTextNode("accepted");

    //title
    Element mainTitleTop = xmlDoc.createElement("title");
    Text titleTopText = xmlDoc.createTextNode(stigListTitle);

    //description
    Element mainDescriptionTop = xmlDoc.createElement("description");
    Text descriptionTopText = xmlDoc.createTextNode("A created list of STIG items");

    //notice
    Element mainNoticeTop = xmlDoc.createElement("notice");
    mainNoticeTop.setAttribute("id", "terms-of-use");
    mainNoticeTop.setAttribute("xml:lang", "en");
    Text noticeTopText = xmlDoc.createTextNode("Developed by -- for --");

    //reference
    Element mainReferenceTop = xmlDoc.createElement("reference");
    Element childReferenceTopPublisher = xmlDoc.createElement("dc:publisher");
    Text referenceTopPublisherText = xmlDoc.createTextNode("--");
    Element childReferenceTopSource = xmlDoc.createElement("dc:source");
    Text referenceTopSourceText = xmlDoc.createTextNode("--");

    //plaint-text
    Element mainPlainTextTop = xmlDoc.createElement("plain-text");
    mainPlainTextTop.setAttribute("id", "release-info");
    Text plainTextTopText = xmlDoc.createTextNode(listCreationDate);

    //version
    Element mainVersionTop = xmlDoc.createElement("version");
    Text versionTopText = xmlDoc.createTextNode("1");

    //profile MAC1 Classified
    Element mainProfileMac1Classified = xmlDoc.createElement("Profile");
    mainProfileMac1Classified.setAttribute("id", "MAC-1_Classified");
    Element childTitleProfileMac1Classified = xmlDoc.createElement("title");
    Text titleProfileMac1ClassifiedText = xmlDoc.createTextNode("I - Mission Critical Classified");
    Element childDescriptionProfileMac1Classified = xmlDoc.createElement("description");
    Text descriptionProfileMac1Classified = xmlDoc.createTextNode("<ProfileroupDescription></ProfileDescription>");
    Element childSelectProfileMac1Classified1 = xmlDoc.createElement("select");
    childSelectProfileMac1Classified1.setAttribute("idref", "V-001");
    childSelectProfileMac1Classified1.setAttribute("selected", "true");
    Element childSelectProfileMac1Classified2 = xmlDoc.createElement("select");
    childSelectProfileMac1Classified2.setAttribute("idref", "V-002");
    childSelectProfileMac1Classified2.setAttribute("selected", "true");

    //profile MAC1 Public
    Element mainProfileMac1Public = xmlDoc.createElement("Profile");
    mainProfileMac1Public.setAttribute("id", "MAC-1_Public");
    Element childTitleProfileMac1Public = xmlDoc.createElement("title");
    Text titleProfileMac1PublicText = xmlDoc.createTextNode("I - Mission Critical Public");
    Element childDescriptionProfileMac1Public = xmlDoc.createElement("description");
    Text descriptionProfileMac1public = xmlDoc.createTextNode("<ProfileroupDescription></ProfileDescription>");
    Element childSelectProfileMac1Public1 = xmlDoc.createElement("select");
    childSelectProfileMac1Public1.setAttribute("idref", "V-001");
    childSelectProfileMac1Public1.setAttribute("selected", "true");
    Element childSelectProfileMac1Public2 = xmlDoc.createElement("select");
    childSelectProfileMac1Public2.setAttribute("idref", "V-002");
    childSelectProfileMac1Public2.setAttribute("selected", "true");

    //profile MAC1 Sensitive
    Element mainProfileMac1Sensitive = xmlDoc.createElement("Profile");
    mainProfileMac1Sensitive.setAttribute("id", "MAC-1_Sensitive");
    Element childTitleProfileMac1Sensitive = xmlDoc.createElement("title");
    Text titleProfileMac1SensitiveText = xmlDoc.createTextNode("I - Mission Critical Sensitive");
    Element childDescriptionProfileMac1Sensitive = xmlDoc.createElement("description");
    Text descriptionProfileMac1Sensitive = xmlDoc.createTextNode("<ProfileroupDescription></ProfileDescription>");
    Element childSelectProfileMac1Sensitive1 = xmlDoc.createElement("select");
    childSelectProfileMac1Sensitive1.setAttribute("idref", "V-001");
    childSelectProfileMac1Sensitive1.setAttribute("selected", "true");
    Element childSelectProfileMac1Sensitive2 = xmlDoc.createElement("select");
    childSelectProfileMac1Sensitive2.setAttribute("idref", "V-002");
    childSelectProfileMac1Sensitive2.setAttribute("selected", "true");

    //profile MAC2 Classified
    Element mainProfileMac2Classified = xmlDoc.createElement("Profile");
    mainProfileMac2Classified.setAttribute("id", "MAC-2_Classified");
    Element childTitleProfileMac2Classified = xmlDoc.createElement("title");
    Text titleProfileMac2ClassifiedText = xmlDoc.createTextNode("II - Mission Support Classified");
    Element childDescriptionProfileMac2Classified = xmlDoc.createElement("description");
    Text descriptionProfileMac2Classified = xmlDoc.createTextNode("<ProfileroupDescription></ProfileDescription>");
    Element childSelectProfileMac2Classified1 = xmlDoc.createElement("select");
    childSelectProfileMac2Classified1.setAttribute("idref", "V-001");
    childSelectProfileMac2Classified1.setAttribute("selected", "true");
    Element childSelectProfileMac2Classified2 = xmlDoc.createElement("select");
    childSelectProfileMac2Classified2.setAttribute("idref", "V-002");
    childSelectProfileMac2Classified2.setAttribute("selected", "true");

    //profile MAC2 Public
    Element mainProfileMac2Public = xmlDoc.createElement("Profile");
    mainProfileMac2Public.setAttribute("id", "MAC-2_Public");
    Element childTitleProfileMac2Public = xmlDoc.createElement("title");
    Text titleProfileMac2PublicText = xmlDoc.createTextNode("II - Mission Support Public");
    Element childDescriptionProfileMac2Public = xmlDoc.createElement("description");
    Text descriptionProfileMac2Public = xmlDoc.createTextNode("<ProfileroupDescription></ProfileDescription>");
    Element childSelectProfileMac2Public1 = xmlDoc.createElement("select");
    childSelectProfileMac2Public1.setAttribute("idref", "V-001");
    childSelectProfileMac2Public1.setAttribute("selected", "true");
    Element childSelectProfileMac2Public2 = xmlDoc.createElement("select");
    childSelectProfileMac2Public2.setAttribute("idref", "V-002");
    childSelectProfileMac2Public2.setAttribute("selected", "true");

    //profile MAC2 Sensitive
    Element mainProfileMac2Sensitive = xmlDoc.createElement("Profile");
    mainProfileMac2Sensitive.setAttribute("id", "MAC-2_Sensitive");
    Element childTitleProfileMac2Sensitive = xmlDoc.createElement("title");
    Text titleProfileMac2SensitiveText = xmlDoc.createTextNode("II - Mission Support Sensitive");
    Element childDescriptionProfileMac2Sensitive = xmlDoc.createElement("description");
    Text descriptionProfileMac2Sensitive = xmlDoc.createTextNode("<ProfileroupDescription></ProfileDescription>");
    Element childSelectProfileMac2Sensitive1 = xmlDoc.createElement("select");
    childSelectProfileMac2Sensitive1.setAttribute("idref", "V-001");
    childSelectProfileMac2Sensitive1.setAttribute("selected", "true");
    Element childSelectProfileMac2Sensitive2 = xmlDoc.createElement("select");
    childSelectProfileMac2Sensitive2.setAttribute("idref", "V-002");
    childSelectProfileMac2Sensitive2.setAttribute("selected", "true");

    //profile MAC3 Classified
    Element mainProfileMac3Classified = xmlDoc.createElement("Profile");
    mainProfileMac3Classified.setAttribute("id", "MAC-3_Classified");
    Element childTitleProfileMac3Classified = xmlDoc.createElement("title");
    Text titleProfileMac3ClassifiedText = xmlDoc.createTextNode("III - Administrative Classified");
    Element childDescriptionProfileMac3Classified = xmlDoc.createElement("description");
    Text descriptionProfileMac3Classified = xmlDoc.createTextNode("<ProfileroupDescription></ProfileDescription>");
    Element childSelectProfileMac3Classified1 = xmlDoc.createElement("select");
    childSelectProfileMac3Classified1.setAttribute("idref", "V-001");
    childSelectProfileMac3Classified1.setAttribute("selected", "true");
    Element childSelectProfileMac3Classified2 = xmlDoc.createElement("select");
    childSelectProfileMac3Classified2.setAttribute("idref", "V-002");
    childSelectProfileMac3Classified2.setAttribute("selected", "true");

    //profile MAC3 Public
    Element mainProfileMac3Public = xmlDoc.createElement("Profile");
    mainProfileMac3Public.setAttribute("id", "MAC-3_Public");
    Element childTitleProfileMac3Public = xmlDoc.createElement("title");
    Text titleProfileMac3PublicText = xmlDoc.createTextNode("III - Administrative Public");
    Element childDescriptionProfileMac3Public = xmlDoc.createElement("description");
    Text descriptionProfileMac3Public = xmlDoc.createTextNode("<ProfileroupDescription></ProfileDescription>");
    Element childSelectProfileMac3Public1 = xmlDoc.createElement("select");
    childSelectProfileMac3Public1.setAttribute("idref", "V-001");
    childSelectProfileMac3Public1.setAttribute("selected", "true");
    Element childSelectProfileMac3Public2 = xmlDoc.createElement("select");
    childSelectProfileMac3Public2.setAttribute("idref", "V-002");
    childSelectProfileMac3Public2.setAttribute("selected", "true");

    //profile MAC3 Sensitive
    Element mainProfileMac3Sensitive = xmlDoc.createElement("Profile");
    mainProfileMac3Sensitive.setAttribute("id", "MAC-3_Sensitive");
    Element childTitleProfileMac3Sensitive = xmlDoc.createElement("title");
    Text titleProfileMac3SensitiveText = xmlDoc.createTextNode("III - Administrative Sensitive");
    Element childDescriptionProfileMac3Sensitive = xmlDoc.createElement("description");
    Text descriptionProfileMac3Sensitive = xmlDoc.createTextNode("<ProfileroupDescription></ProfileDescription>");
    Element childSelectProfileMac3Sensitive1 = xmlDoc.createElement("select");
    childSelectProfileMac3Sensitive1.setAttribute("idref", "V-001");
    childSelectProfileMac3Sensitive1.setAttribute("selected", "true");
    Element childSelectProfileMac3Sensitive2 = xmlDoc.createElement("select");
    childSelectProfileMac3Sensitive2.setAttribute("idref", "V-002");
    childSelectProfileMac3Sensitive2.setAttribute("selected", "true");

    //Group something? Not sure what this is exactly, yet
    Element mainGroupID = xmlDoc.createElement("Group");
    mainGroupID.setAttribute("id", "V-001");
    Element childTitleGroupID = xmlDoc.createElement("title");
    Text titleGroupIDText = xmlDoc.createTextNode(ruleTitle);
    Element childDescriptionGroupID = xmlDoc.createElement("Description");
    Text descriptionGroupID = xmlDoc.createTextNode("A description of something goes here.");
    Element childRuleID18075 = xmlDoc.createElement("Rule");
    childRuleID18075.setAttribute("id", "Rule number 1");
    childRuleID18075.setAttribute("severity", "medium");
    childRuleID18075.setAttribute("weight", "10.0");
    Element babyVersion18075 = xmlDoc.createElement("version");
    Text version18075Text = xmlDoc.createTextNode("Version 1");
    Element babyTitleRuleID18075 = xmlDoc.createElement("title");
    Text titleRuleID18075Text = xmlDoc.createTextNode(ruleTitle);
    Element babyDescriptionRuleID18075 = xmlDoc.createElement("description");
    Text descriptionRuleID18075Text = xmlDoc.createTextNode("A description of the rule goes here, in this case there is no real description because I'm just playing around.");
    Element babyReference01 = xmlDoc.createElement("reference");
    Element ideaDcTitle01 = xmlDoc.createElement("dc:title");
    Text dcTitle01Text = xmlDoc.createTextNode("DPMS Target");
    Element ideaDcPublisher01 = xmlDoc.createElement("dc:publisher");
    Text dcPublisher01Text = xmlDoc.createTextNode("--");
    Element ideaDcType01 = xmlDoc.createElement("dc:type");
    Text dcType01Text = xmlDoc.createTextNode("--");
    Element ideaDcSubject01 = xmlDoc.createElement("dc:subject");
    Text dcSubject01Text = xmlDoc.createTextNode("--");
    Element ideaDcIdentifier01 = xmlDoc.createElement("dc:identifier");
    Text dcIdentifier01Text = xmlDoc.createTextNode("1176");
    Element childFixTextRef01 = xmlDoc.createElement("fixtext");
    childFixTextRef01.setAttribute("fixref", "F-13948r4_fix");
    Text fixTextRef01Text = xmlDoc.createTextNode(fixText);
    Element childFixTextRef02 = xmlDoc.createElement("fix");
    childFixTextRef02.setAttribute("id", "F-13948r4_fix");
    Element childCheckSystem01 = xmlDoc.createElement("check");
    childCheckSystem01.setAttribute("system", "C-20792r3_chk");
    Element ideaCheckContent01 = xmlDoc.createElement("check-content-ref");
    ideaCheckContent01.setAttribute("name", "M");
    ideaCheckContent01.setAttribute("href", "XCCDF_STIG_TaskOrders.xml");
    Element ideaCheckContent1 = xmlDoc.createElement("check-content");
    Text checkContent01Text = xmlDoc.createTextNode(ruleDescription);

    //Lets build some stuff here
    xmlDoc.appendChild(rootElement);
    rootElement.appendChild(mainStatusDate);
    mainStatusDate.appendChild(statusDate);
    rootElement.appendChild(mainTitleTop);
    mainTitleTop.appendChild(titleTopText);
    rootElement.appendChild(mainDescriptionTop);
    mainDescriptionTop.appendChild(descriptionTopText);
    rootElement.appendChild(mainNoticeTop);
    mainNoticeTop.appendChild(noticeTopText);
    rootElement.appendChild(mainReferenceTop);
    mainReferenceTop.appendChild(childReferenceTopPublisher);
    childReferenceTopPublisher.appendChild(referenceTopPublisherText);
    mainReferenceTop.appendChild(childReferenceTopSource);
    childReferenceTopSource.appendChild(referenceTopSourceText);
    rootElement.appendChild(mainPlainTextTop);
    mainPlainTextTop.appendChild(plainTextTopText);
    rootElement.appendChild(mainVersionTop);
    mainVersionTop.appendChild(versionTopText);

    // MAC -- 1
    rootElement.appendChild(mainProfileMac1Classified);
    mainProfileMac1Classified.appendChild(childTitleProfileMac1Classified);
    childTitleProfileMac1Classified.appendChild(titleProfileMac1ClassifiedText);
    mainProfileMac1Classified.appendChild(childDescriptionProfileMac1Classified);
    childDescriptionProfileMac1Classified.appendChild(descriptionProfileMac1Classified);
    mainProfileMac1Classified.appendChild(childSelectProfileMac1Classified1);
    mainProfileMac1Classified.appendChild(childSelectProfileMac1Classified2);
    rootElement.appendChild(mainProfileMac1Public);
    mainProfileMac1Public.appendChild(childTitleProfileMac1Public);
    childTitleProfileMac1Public.appendChild(titleProfileMac1PublicText);
    mainProfileMac1Public.appendChild(childDescriptionProfileMac1Public);
    childDescriptionProfileMac1Public.appendChild(descriptionProfileMac1public);
    mainProfileMac1Public.appendChild(childSelectProfileMac1Public1);
    mainProfileMac1Public.appendChild(childSelectProfileMac1Public2);
    rootElement.appendChild(mainProfileMac1Sensitive);
    mainProfileMac1Sensitive.appendChild(childTitleProfileMac1Sensitive);
    childTitleProfileMac1Sensitive.appendChild(titleProfileMac1SensitiveText);
    mainProfileMac1Sensitive.appendChild(childDescriptionProfileMac1Sensitive);
    childDescriptionProfileMac1Sensitive.appendChild(descriptionProfileMac1Sensitive);
    mainProfileMac1Sensitive.appendChild(childSelectProfileMac1Sensitive1);
    mainProfileMac1Sensitive.appendChild(childSelectProfileMac1Sensitive2);

    // MAC -- 2
    rootElement.appendChild(mainProfileMac2Classified);
    mainProfileMac2Classified.appendChild(childTitleProfileMac2Classified);
    childTitleProfileMac2Classified.appendChild(titleProfileMac2ClassifiedText);
    mainProfileMac2Classified.appendChild(childDescriptionProfileMac2Classified);
    childDescriptionProfileMac2Classified.appendChild(descriptionProfileMac2Classified);
    mainProfileMac2Classified.appendChild(childSelectProfileMac2Classified1);
    mainProfileMac2Classified.appendChild(childSelectProfileMac2Classified2);
    rootElement.appendChild(mainProfileMac2Public);
    mainProfileMac2Public.appendChild(childTitleProfileMac2Public);
    childTitleProfileMac2Public.appendChild(titleProfileMac2PublicText);
    mainProfileMac2Public.appendChild(childDescriptionProfileMac2Public);
    childDescriptionProfileMac2Public.appendChild(descriptionProfileMac2Public);
    mainProfileMac2Public.appendChild(childSelectProfileMac2Public1);
    mainProfileMac2Public.appendChild(childSelectProfileMac2Public2);
    rootElement.appendChild(mainProfileMac2Sensitive);
    mainProfileMac2Sensitive.appendChild(childTitleProfileMac2Sensitive);
    childTitleProfileMac2Sensitive.appendChild(titleProfileMac2SensitiveText);
    mainProfileMac2Sensitive.appendChild(childDescriptionProfileMac2Sensitive);
    childDescriptionProfileMac2Sensitive.appendChild(descriptionProfileMac2Sensitive);
    mainProfileMac2Sensitive.appendChild(childSelectProfileMac2Sensitive1);
    mainProfileMac2Sensitive.appendChild(childSelectProfileMac2Sensitive2);

    // MAC -- 3
    rootElement.appendChild(mainProfileMac3Classified);
    mainProfileMac3Classified.appendChild(childTitleProfileMac3Classified);
    childTitleProfileMac3Classified.appendChild(titleProfileMac3ClassifiedText);
    mainProfileMac3Classified.appendChild(childDescriptionProfileMac3Classified);
    childDescriptionProfileMac3Classified.appendChild(descriptionProfileMac3Classified);
    mainProfileMac3Classified.appendChild(childSelectProfileMac3Classified1);
    mainProfileMac3Classified.appendChild(childSelectProfileMac3Classified2);
    rootElement.appendChild(mainProfileMac3Public);
    mainProfileMac3Public.appendChild(childTitleProfileMac3Public);
    childTitleProfileMac3Public.appendChild(titleProfileMac3PublicText);
    mainProfileMac3Public.appendChild(childDescriptionProfileMac3Public);
    childDescriptionProfileMac3Public.appendChild(descriptionProfileMac3Public);
    mainProfileMac3Public.appendChild(childSelectProfileMac3Public1);
    mainProfileMac3Public.appendChild(childSelectProfileMac3Public2);
    rootElement.appendChild(mainProfileMac3Sensitive);
    mainProfileMac3Sensitive.appendChild(childTitleProfileMac3Sensitive);
    childTitleProfileMac3Sensitive.appendChild(titleProfileMac3SensitiveText);
    mainProfileMac3Sensitive.appendChild(childDescriptionProfileMac3Sensitive);
    childDescriptionProfileMac3Sensitive.appendChild(descriptionProfileMac3Sensitive);
    mainProfileMac3Sensitive.appendChild(childSelectProfileMac3Sensitive1);
    mainProfileMac3Sensitive.appendChild(childSelectProfileMac3Sensitive2);

    // first made up STIG item
    rootElement.appendChild(mainGroupID);
    mainGroupID.appendChild(childTitleGroupID);
    childTitleGroupID.appendChild(titleGroupIDText);
    mainGroupID.appendChild(childDescriptionGroupID);
    childDescriptionGroupID.appendChild(descriptionGroupID);
    mainGroupID.appendChild(childRuleID18075);
    childRuleID18075.appendChild(babyVersion18075);
    babyVersion18075.appendChild(version18075Text);
    childRuleID18075.appendChild(babyTitleRuleID18075);
    babyTitleRuleID18075.appendChild(titleRuleID18075Text);
    childRuleID18075.appendChild(babyDescriptionRuleID18075);
    babyDescriptionRuleID18075.appendChild(descriptionRuleID18075Text);
    childRuleID18075.appendChild(babyReference01);
    babyReference01.appendChild(ideaDcTitle01);
    ideaDcTitle01.appendChild(dcTitle01Text);
    babyReference01.appendChild(ideaDcPublisher01);
    ideaDcPublisher01.appendChild(dcPublisher01Text);
    babyReference01.appendChild(ideaDcType01);
    ideaDcType01.appendChild(dcType01Text);
    babyReference01.appendChild(ideaDcSubject01);
    ideaDcSubject01.appendChild(dcSubject01Text);
    babyReference01.appendChild(ideaDcIdentifier01);
    ideaDcIdentifier01.appendChild(dcIdentifier01Text);
    childRuleID18075.appendChild(childFixTextRef01);
    childFixTextRef01.appendChild(fixTextRef01Text);
    childRuleID18075.appendChild(childFixTextRef02);
    childRuleID18075.appendChild(childCheckSystem01);
    childCheckSystem01.appendChild(ideaCheckContent01);
    childCheckSystem01.appendChild(ideaCheckContent1);
    ideaCheckContent1.appendChild(checkContent01Text);

    //Set OutputFormat
    OutputFormat outFormat = new OutputFormat(xmlDoc);
    outFormat.setIndenting(true);

    //Declare the File 
    File xmlFile = new File("XCCDF_STIG_TaskOrders.xml");

    //Declare the FileOutputStream
    FileOutputStream outStream = new FileOutputStream(xmlFile);

    //XML Serialize to Serialize the XML data with
    XMLSerializer serializer = new XMLSerializer(outStream, outFormat);

    //the specified OutputFormat
    serializer.serialize(xmlDoc);

}

public static String stigListTitle() {
    Scanner in = new Scanner(System.in);
    System.out.println("What is the title of this STIG list?");
    String stigListTitle = in.nextLine();
    return stigListTitle;
}

public static String listCreationDate() {
    Scanner in = new Scanner(System.in);
    System.out.println("What date was this list created on?");
    String listCreationDate = in.nextLine();
    return listCreationDate;
}

public static String ruleTitle() {
    Scanner in = new Scanner(System.in);
    System.out.println("What is the title of the rule?");
    String ruleTitle = in.nextLine();
    return ruleTitle;
}

public static String ruleDescription() {
    Scanner in = new Scanner(System.in);
    System.out.println("What is a description of the rule? What is checked?");
    String ruleDescription = in.nextLine();
    return ruleDescription;
}

public static String fixText() {
    Scanner in = new Scanner(System.in);
    System.out.println("What is a description of how vulnerabilty item is fixed?");
    String fixText = in.nextLine();
    return fixText;
  }

}
\$\endgroup\$
1
\$\begingroup\$

You have multiple static methods that all create a Scanner object to the same source just to accept user input. Better to create a single static instance to utilize this source throughout the program.

static Scanner in = new Scanner(System.in);

Additionally, all these methods follow this pattern:

public static String methodName() {
    Scanner in = new Scanner(System.in);
    System.out.println("message");
    String variable = in.nextLine();
    return variable;
}

barring the aforementioned Scanner issue, why not have a helper method that is meant to achieve the same purpose? The only real difference between these is the message to display.

Something along the lines:

private static String helperMethod(String message) {
  System.out.println(message);
  return in.nextLine();
}

Of course, you would name these something meaningful and most fitting to your purpose.

\$\endgroup\$
0
\$\begingroup\$

Craft an object containing stigListTitle, ruleDescription and other information and store object instances within an ordered collection.

Suggest MultiMap usage in object. Terse ruleDescription as String key, "What is a desc..." as verbose question, and third String contains response given by user.

main routine opens Scanner only once. While loop displays retrieves and displays each Map question text, stores input response.

User exits the loop when finished. Iterate the collection of objects in order, write their Map values as XML output.

\$\endgroup\$
  • \$\begingroup\$ I'm sorry, but I just can't quite follow your answer .. you seem to make quite many points in an extremely terse fashion. Would you mind adding some more "plain text" so people like me can follow along better? \$\endgroup\$ – Vogel612 Oct 2 '16 at 11:02
  • \$\begingroup\$ Can start with a simple refactoring of the 'writeToXML' method: note the number of String variables inside 'writeToXML'. Suggest making these into objects of their own. Then examine the numerous other variables and do the same. But how to access those? An ordered collection would be one approach. Guava had some of the more useful, think have since been incorporated into standard Java. \$\endgroup\$ – kph0x1 Oct 2 '16 at 15:24
  • \$\begingroup\$ Note the 'childSelectProfileMac1Sensitive2' sequentially named variables. Have instead one such object for each and store them in a collection. Iterate the collection in a loop to do whatever is needed, such as constructing an XML tree / output. \$\endgroup\$ – kph0x1 Oct 3 '16 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.