Question

Must comment on the code at every "/* Comment Here */" section about what the code...

Must comment on the code at every "/* Comment Here */" section about what the code is doing.

import java.io.*;

import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;

import java.util.Map;
import java.util.HashMap;

public class Configuration extends DefaultHandler
{
private Map map;
private String configurationFile;

/* Comment Here */
public Configuration(String configurationFile) throws ConfigurationException {
   this.configurationFile = configurationFile;

   map = new HashMap();

   try {
   // Use the default (non-validating) parser
   SAXParserFactory factory = SAXParserFactory.newInstance();

   // Parse the input
   SAXParser saxParser = factory.newSAXParser();
   saxParser.parse( new File(configurationFile), this);
   }
   catch (javax.xml.parsers.ParserConfigurationException pce) {
       throw new ConfigurationException("javax.xml.parsers.ParserConfigurationException");
   }
   catch (org.xml.sax.SAXException se) {
       throw new ConfigurationException("org.xml.sax.SAXException");
   }
   catch (java.io.IOException ioe) {
       throw new ConfigurationException("java.io.IOException");
   }
}


   /* Comment Here */
public void startElement(String namespaceURI,
String lName,   
String qName,   
Attributes attrs)  
throws SAXException
{
String elementName = lName; // element name
if ("".equals(elementName))
       elementName = qName; // namespaceAware = false

   /* Comment Here */
if (attrs != null) {
for (int i = 0; i < attrs.getLength(); i++) {
String aName = attrs.getLocalName(i); // Attr name
if ("".equals(aName))
           aName = attrs.getQName(i);

       /* Comment Here */
       map.put(elementName+"."+aName,attrs.getValue(i));
}
}
}

   /* Comment Here */
   public String getLogFile() {
       return (String)map.get("logfile.log");
   }

   /* Comment Here */
   public String getDocBase() {
       return (String)map.get("context.docBase");
   }

   /* Comment Here */
   public String getServerName() {
       return (String)map.get("webserver.title");
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.*;

import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;

import java.util.Map;
import java.util.HashMap;

public class Configuration extends DefaultHandler
{
private Map map;
private String configurationFile;

/* Constructor is created which takes input string from parameter and stores into this class private data type string which is configurationFile as it's a private variable of the class it cannot be accessed by any other class except Configuration classs itself. If any Exception occurs the it is handled by ConfigurationException itself */

/* Map instance is also created & as we can see XML is also parsed in this section of class and its expections are properlly handled */
public Configuration(String configurationFile) throws ConfigurationException {
   this.configurationFile = configurationFile;

   map = new HashMap();

   try {
   // Use the default (non-validating) parser
   SAXParserFactory factory = SAXParserFactory.newInstance();

   // Parse the input
   SAXParser saxParser = factory.newSAXParser();
   saxParser.parse( new File(configurationFile), this);
   }
   catch (javax.xml.parsers.ParserConfigurationException pce) {
       throw new ConfigurationException("javax.xml.parsers.ParserConfigurationException");
   }
   catch (org.xml.sax.SAXException se) {
       throw new ConfigurationException("org.xml.sax.SAXException");
   }
   catch (java.io.IOException ioe) {
       throw new ConfigurationException("java.io.IOException");
   }
}


   /* This funtion takes parameters and stores it to its local variable and exceptions are properly handled by its exceptionm handlers */
public void startElement(String namespaceURI,
String lName,   
String qName,   
Attributes attrs)  
throws SAXException
{
String elementName = lName; // element name
if ("".equals(elementName))
       elementName = qName; // namespaceAware = false

   /* We check for attris if its not null then the condition is fulfilled and it enter the if condition */
if (attrs != null) {
for (int i = 0; i < attrs.getLength(); i++) {
String aName = attrs.getLocalName(i); // Attr name
if ("".equals(aName))
           aName = attrs.getQName(i);

       /* aName and its respective values are inserted into the map which has been defiened into the constructor. */
       map.put(elementName+"."+aName,attrs.getValue(i));
}
}
}

   /* Returns the local name from Log File */
   public String getLogFile() {
       return (String)map.get("logfile.log");
   }

   /* The docBase attribute is a path to the WAR file or exploded deployment directory. It is relative to the webapps directory,. it return the value from map */
   public String getDocBase() {
       return (String)map.get("context.docBase");
   }

   /* Returns the server name which is in the map */
   public String getServerName() {
       return (String)map.get("webserver.title");
   }
}

Add a comment
Know the answer?
Add Answer to:
Must comment on the code at every "/* Comment Here */" section about what the code...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • // can someone explain the code line by line shortly.thanks import java.security.KeyPair; import ...

    // can someone explain the code line by line shortly.thanks import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import java.util.Base64; import java.util.HashMap; import java.util.Map; import javax.crypto.Cipher; // Java 8 example for RSA encryption/decryption. // Uses strong encryption with 2048 key size. public class GlobalMembers { public static void main(String[] args) throws Exception { String plainText = "Hello World!"; // Generate public and private keys using RSA Map<String, Object> keys = getRSAKeys(); PrivateKey privateKey = (PrivateKey) keys.get("private"); PublicKey publicKey = (PublicKey)...

  • ANNOTATE BRIEFLY LINE BY LINE THE FOLLOWING CODE: package shop.data; import junit.framework.Assert; import junit.framework.TestCase; public class...

    ANNOTATE BRIEFLY LINE BY LINE THE FOLLOWING CODE: package shop.data; import junit.framework.Assert; import junit.framework.TestCase; public class DataTEST extends TestCase { public DataTEST(String name) { super(name); } public void testConstructorAndAttributes() { String title1 = "XX"; String director1 = "XY"; String title2 = " XX "; String director2 = " XY "; int year = 2002; Video v1 = Data.newVideo(title1, year, director1); Assert.assertSame(title1, v1.title()); Assert.assertEquals(year, v1.year()); Assert.assertSame(director1, v1.director()); Video v2 = Data.newVideo(title2, year, director2); Assert.assertEquals(title1, v2.title()); Assert.assertEquals(director1, v2.director()); } public void testConstructorExceptionYear()...

  • Can someone help me with this code, I not really understanding how to do this? import...

    Can someone help me with this code, I not really understanding how to do this? import java.util.ArrayList; import java.util.HashMap; import java.util.Map; /** * @version Spring 2019 * @author Kyle */ public class MapProblems { /** * Modify and return the given map as follows: if the key "a" has a value, set the key "b" to * have that value, and set the key "a" to have the value "". Basically "b" is confiscating the * value and replacing it...

  • Convert Hex To Dec in java with HashMap and put in .txt file I'm making a...

    Convert Hex To Dec in java with HashMap and put in .txt file I'm making a compiler for assembler in JAVA. My program after several processes leaves a text file as follows. 02TLIGHT2.ASM file with this: CLO Start: MOV AL,0 OUT 01 MOV AL,FC OUT 01 JMP Start END Im trying by means of my hashmap convert the commands to his assigned hexadecimal value and put it in a text file? The output of my actual program in a 02TLIGHTHexa.ASM...

  • What is the time and space complexity of this code with explanation? public static int getv(int...

    What is the time and space complexity of this code with explanation? public static int getv(int num, List<Integer> rating, int target) { int count 0; HashMap<Integer, Integer> map = new HashMap<>(); for(int i = 0; i < num; i++){ if(!map.containsKey(rating.get(i))){ map.put(rating.-get(i), 0); } map.put(rating.get(i), map.get(rating.get(i))+1); } for(int i = 0; i < num; i++){ if(map.get(target -rating.get(i)) != null){ count += map.get(target-rating.get(i)); } if(target-rating.get(i) == rating.get(i)){ count--; } return count/2; }

  • Please Modify TestPart2 to test the correctness and efficiency of FasterDefaultList. Thanks import java.util.List; import java.util.AbstractList;...

    Please Modify TestPart2 to test the correctness and efficiency of FasterDefaultList. Thanks import java.util.List; import java.util.AbstractList; import java.util.Map; import java.util.HashMap; public class DumbDefaultList<T> extends AbstractList<T> { Map<Integer,T> map; public DumbDefaultList() { map = new HashMap<Integer,T>(); } public int size() { return Integer.MAX_VALUE; } public T get(int i) { return map.get(i); } public T set(int i, T x) { return map.put(i, x); } public void add(int i, T x) { Map<Integer, T> map2 = new HashMap<Integer,T>(); for (Integer k : map.keySet())...

  • Problem: Use the code I have provided to start writing a program that accepts a large...

    Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...

  • Here is my assignment: --------------------------------------------------- Consider the following client class: import java.util.Collection; import java.util.Collections; import java.util.HashMap;...

    Here is my assignment: --------------------------------------------------- Consider the following client class: import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; public class PresidentsMain {       public static void main(String[] args) {             Map<String, String> PresidentsOfTheUnitedStates = new HashMap<String, String>();             PresidentsOfTheUnitedStates.put("George Washington", "Unaffiliated");             PresidentsOfTheUnitedStates.put("John Adams", "Federalist");             PresidentsOfTheUnitedStates.put("Thomas Jefferson", "Democratic-Republican");             PresidentsOfTheUnitedStates.put("James Madison", "Democratic-Republican");             PresidentsOfTheUnitedStates.put("James Monroe", "Democratic-Republican");             PresidentsOfTheUnitedStates.put("John Quincy Adams", "Democratic-Republican");             PresidentsOfTheUnitedStates.put("Andrew Jackson", "Democratic");             PresidentsOfTheUnitedStates.put("Martin Van Buren", "Democratic");             PresidentsOfTheUnitedStates.put("William Henry Harrison", "Whig");             PresidentsOfTheUnitedStates.put("John Tyler", "Whig");            }       } } Extend given client class: Implement a static...

  • Modify the below Java program to use exceptions if the password is wrong (WrongCredentials excpetion). import java.util....

    Modify the below Java program to use exceptions if the password is wrong (WrongCredentials excpetion). import java.util.HashMap; import java.util.Map; import java.util.Scanner; class Role { String user, password, role; public Role(String user, String password, String role) { super(); this.user = user; this.password = password; this.role = role; } /** * @return the user */ public String getUser() { return user; } /** * @param user the user to set */ public void setUser(String user) { this.user = user; } /** *...

  • Convert Code from Java to C++ Convert : import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class...

    Convert Code from Java to C++ Convert : import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Template { public static void main(String args[]) { Scanner sc1 = new Scanner(System.in); //Standard input data String[] line1 = sc1.nextLine().split(","); //getting input and spliting on basis of "," String[] line2 = sc1.nextLine().split(" "); //getting input and spiliting on basis of " "    Map<String, String> mapping = new HashMap<String, String>(); for(int i=0;i<line1.length;i++) { mapping.put(line1[i].split("=")[0], line1[i].split("=")[1]); //string in map where in [] as key and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT