View Javadoc
1   /*
2    * Created on Jun 24, 2004
3    *  
4    */
5   package org.kuali.coeus.s2sgen.impl.hash;
6   
7   import org.kuali.coeus.s2sgen.impl.util.XPathExecutor;
8   import org.w3c.dom.Attr;
9   import org.w3c.dom.Element;
10  import org.w3c.dom.Node;
11  
12  import javax.xml.transform.TransformerException;
13  
14  /**
15   * Grant application Xpath class.
16   */
17  public class GrantApplicationXpath
18  {
19  
20      private static final String HASH_ALGORITHM = "glob:hashAlgorithm";
21      private static final String SHA_1 = "SHA";
22  
23      public static final String GS_HEADER_XPATH = "/*[namespace-uri()='http://apply.grants.gov/system/MetaGrantApplication' and local-name()='GrantApplication']/*[namespace-uri()='http://apply.grants.gov/system/Header-V1.0' and local-name()='GrantSubmissionHeader']";
24      public static final String OPPORTUNITY_ID_XPATH = GS_HEADER_XPATH
25          + "/*[namespace-uri()='http://apply.grants.gov/system/Header-V1.0' and local-name()='OpportunityID']";
26      public static final String CFDA_NUMBER_XPATH = GS_HEADER_XPATH
27          + "/*[namespace-uri()='http://apply.grants.gov/system/Header-V1.0' and local-name()='CFDANumber']";
28      public static final String OPPORTUNITY_TITLE_XPATH = GS_HEADER_XPATH
29          + "/*[namespace-uri()='http://apply.grants.gov/system/Header-V1.0' and local-name()='OpportunityTitle']";
30      public static final String CLOSING_DATE_XPATH = GS_HEADER_XPATH
31          + "/*[namespace-uri()='http://apply.grants.gov/system/Header-V1.0' and local-name()='ClosingDate']";
32      public static final String AGENCY_NAME_XPATH = GS_HEADER_XPATH
33          + "/*[namespace-uri()='http://apply.grants.gov/system/Header-V1.0' and local-name()='AgencyName']";
34      public static final String COMPETITION_ID_XPATH = GS_HEADER_XPATH
35          + "/*[namespace-uri()='http://apply.grants.gov/system/Header-V1.0' and local-name()='CompetitionID']";
36      public static final String SUBMISSION_TITLE_XPATH = GS_HEADER_XPATH
37          + "/*[namespace-uri()='http://apply.grants.gov/system/Header-V1.0' and local-name()='SubmissionTitle']";
38      public static final String HASHVALUE = "glob:HashValue";
39      public static final String FORMS_XPATH = "//*[local-name(.) = 'Forms' and namespace-uri(.) = 'http://apply.grants.gov/system/MetaGrantApplication']";
40      public static final String HEADER_XPATH = "//*[local-name(.) = 'GrantSubmissionHeader' and namespace-uri(.) = 'http://apply.grants.gov/system/Header-V1.0']";
41      public static final String HASH_XPATH = "//*[local-name(.) = 'GrantSubmissionHeader' and namespace-uri(.) = 'http://apply.grants.gov/system/Header-V1.0']/*[local-name(.) = 'HashValue' and namespace-uri(.) = 'http://apply.grants.gov/system/Global-V1.0']";
42      public static final String GLOBAL_NS = "http://apply.grants.gov/system/Global-V1.0";
43  
44      private XPathExecutor executor;
45  
46      public GrantApplicationXpath(String xmlDoc)
47          throws Exception
48      {
49          setExecutor(createExecutor(xmlDoc));
50  
51      }
52  
53      /**
54       * @return the XPathExecutor.
55       */
56      public XPathExecutor getExecutor()
57      {
58          return executor;
59      }
60  
61      /**
62       * @param executor
63       *            the XPathExecutor.
64       */
65      public void setExecutor(XPathExecutor executor)
66      {
67          this.executor = executor;
68      }
69  
70      private XPathExecutor createExecutor(String xmlDoc)
71          throws Exception
72      {
73          return new XPathExecutor(xmlDoc);
74      }
75  
76      /**
77       * Get the <code>&lt;grant:Forms&gt;</code> Node.
78       * 
79       * @return A DOM Node representing the <code>&lt;grant:Forms&gt;</code>
80       *         tag in the XML document.
81       * @throws TransformerException
82       */
83      public Node getFormsNode()
84          throws TransformerException
85      {
86          return getExecutor().getNode(FORMS_XPATH);
87      }
88  
89      /**
90       * Get the <code>&lt;header:GrantSubmissionHeader&gt;</code> Node.
91       * 
92       * @return A DOM Node representing the
93       *         <code>&lt;header:GrantSubmissionHeader&gt;</code> tag in the
94       *         XML document.
95       * @throws TransformerException
96       */
97      public Node getHeaderNode()
98          throws TransformerException
99      {
100         return getExecutor().getNode(HEADER_XPATH);
101     }
102 
103     /**
104      * Get the <code>&lt;glob:HashValue&gt;</code> Node. If the Node does not
105      * exist, create one and add it to the Document.
106      * 
107      * @return A DOM Node representing the <code>&lt;glob:HashValue&gt;</code>
108      *         tag in the XML document.
109      * @throws TransformerException
110      */
111     public Node getHashNode()
112         throws TransformerException
113     {
114         Node hashNode = getExecutor().getNode(HASH_XPATH);
115         if (hashNode == null)
116         {
117             hashNode = getExecutor().getDoc().createElementNS(GLOBAL_NS,
118                 HASHVALUE);
119             Attr algorithm = getExecutor().getDoc().createAttributeNS(GLOBAL_NS, HASH_ALGORITHM);
120             algorithm.setValue(SHA_1);
121             ((Element) hashNode).setAttributeNodeNS(algorithm);
122             hashNode.appendChild(getExecutor().getDoc().createTextNode(""));
123             Node header = getHeaderNode();
124             header.insertBefore(hashNode, header.getFirstChild());
125         }
126         return hashNode;
127     }
128 
129     /**
130      * Get the value in the <code>&lt;glob:HashValue&gt;</code> element.
131      * 
132      * @return The value that is currently in the
133      *         <code>&lt;glob:HashValue&gt;</code> element or
134      *         <code>null</code> if the element does not exist or is empty.
135      * @throws Exception
136      */
137     public String getHeaderHashValue()
138         throws Exception
139     {
140         return getExecutor().execute(HASH_XPATH);
141     }
142 
143     /**
144      * @return Returns the agencyName.
145      */
146     public String getAgencyName()
147         throws Exception
148     {
149         return getExecutor().execute(AGENCY_NAME_XPATH);
150     }
151 
152     /**
153      * @return the string of the CFDA Number.
154      */
155     public String getCfdaNumber()
156         throws Exception
157     {
158         return getExecutor().execute(CFDA_NUMBER_XPATH);
159     }
160 
161     /**
162      * @return the string of the closing date.
163      */
164     public String getClosingDate()
165         throws Exception
166     {
167         return getExecutor().execute(CLOSING_DATE_XPATH);
168     }
169 
170     /**
171      * @return the string of the opportunity ID.
172      */
173     public String getOpportunityId()
174         throws Exception
175     {
176         return getExecutor().execute(OPPORTUNITY_ID_XPATH);
177     }
178 
179     /**
180      * @return the string of the opportunity title.
181      * @throws Exception
182      */
183     public String getOpportunityTitle()
184         throws Exception
185     {
186         return getExecutor().execute(OPPORTUNITY_TITLE_XPATH);
187     }
188 
189     /**
190      * @return the string of the competition ID.
191      * @throws Exception
192      */
193     public String getCompetitionId()
194         throws Exception
195     {
196         return getExecutor().execute(COMPETITION_ID_XPATH);
197     }
198 
199     /**
200      * @return returns the submissionTitle.
201      * @throws Exception
202      */
203     public String getSubmissionTitle()
204         throws Exception
205     {
206         return getExecutor().execute(SUBMISSION_TITLE_XPATH);
207     }
208 }