Java Code Examples for nu.xom.Element#appendChild()

The following examples show how to use nu.xom.Element#appendChild() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XmlAuthUtil.java    From rest-client with Apache License 2.0 6 votes vote down vote up
static Element getNtlmAuthElement(NtlmAuth auth) {
    Element e = new Element("ntlm");
    
    if(StringUtil.isNotEmpty(auth.getDomain())) {
        Element eDomain = new Element("domain");
        eDomain.appendChild(auth.getDomain());
        e.appendChild(eDomain);
    }
    
    if(StringUtil.isNotEmpty(auth.getWorkstation())) {
        Element eWorkstation = new Element("workstation");
        eWorkstation.appendChild(auth.getWorkstation());
        e.appendChild(eWorkstation);
    }
    
    populateUsernamePasswordElement(e, auth);
    
    return e;
}
 
Example 2
Source File: XmlAuthUtil.java    From rest-client with Apache License 2.0 6 votes vote down vote up
static void populateBasicDigestElement(Element eParent, BasicDigestAuth auth) {
    if(StringUtil.isNotEmpty(auth.getHost())) {
        Element eHost = new Element("host");
        eHost.appendChild(auth.getHost());
        eParent.appendChild(eHost);
    }
    
    if(StringUtil.isNotEmpty(auth.getRealm())) {
        Element eRealm = new Element("realm");
        eRealm.appendChild(auth.getRealm());
        eParent.appendChild(eRealm);
    }
    
    if(auth.isPreemptive()) {
        Element ePreemptive = new Element("preemptive");
        eParent.appendChild(ePreemptive);
    }
    
    populateUsernamePasswordElement(eParent, auth);
}
 
Example 3
Source File: XomCreateXML.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * 屬性
 */
@Test
public void test04() {
	BigInteger low = BigInteger.ONE;
	BigInteger high = BigInteger.ONE;

	Element root = new Element("Fibonacci_Numbers");
	for (int i = 1; i <= 10; i++) {
		Element fibonacci = new Element("fibonacci");
		fibonacci.appendChild(low.toString());
		Attribute index = new Attribute("index", String.valueOf(i));
		fibonacci.addAttribute(index);
		root.appendChild(fibonacci);

		BigInteger temp = high;
		high = high.add(low);
		low = temp;
	}
	Document doc = new Document(root);
	try {
		Serializer serializer = new Serializer(System.out, "ISO-8859-1");
		serializer.setIndent(4);
		serializer.setMaxLength(64);
		serializer.write(doc);
	} catch (IOException ex) {
		System.err.println(ex);
	}
}
 
Example 4
Source File: XOMTreeBuilder.java    From caja with Apache License 2.0 5 votes vote down vote up
@Override
protected void appendCharacters(Element parent, String text) throws SAXException {
    try {
        parent.appendChild(nodeFactory.makeText(text));
    } catch (XMLException e) {
        fatal(e);
    }
}
 
Example 5
Source File: XomCreateXML.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * 格式化
 */
@Test
public void test03() {
	BigInteger low  = BigInteger.ONE;
    BigInteger high = BigInteger.ONE;      

    Element root = new Element("Fibonacci_Numbers");  
    for (int i = 1; i <= 10; i++) {
        Element fibonacci = new Element("fibonacci");
        fibonacci.appendChild(low.toString());
        root.appendChild(fibonacci);
        
        BigInteger temp = high;
        high = high.add(low);
        low = temp;
    }
    Document doc = new Document(root);
      
    try {
      Serializer serializer = new Serializer(System.out, "ISO-8859-1");
      serializer.setIndent(4);
      serializer.setMaxLength(64);
      serializer.write(doc);  
    }
    catch (IOException ex) {
       System.err.println(ex); 
    }   
}
 
Example 6
Source File: XomCreateXML.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * Create elements in namespaces
 */
@Test
public void test07() throws Exception{
	BigInteger low  = BigInteger.ONE;
      BigInteger high = BigInteger.ONE;      

      String namespace = "http://www.w3.org/1998/Math/MathML";
      Element root = new Element("mathml:math", namespace);  
      for (int i = 1; i <= 10; i++) {
        Element mrow = new Element("mathml:mrow", namespace);
        Element mi = new Element("mathml:mi", namespace);
        Element mo = new Element("mathml:mo", namespace);
        Element mn = new Element("mathml:mn", namespace);
        mrow.appendChild(mi);
        mrow.appendChild(mo);
        mrow.appendChild(mn);
        root.appendChild(mrow);
        mi.appendChild("f(" + i + ")");
        mo.appendChild("=");
        mn.appendChild(low.toString());
        
        BigInteger temp = high;
        high = high.add(low);
        low = temp;
      }
      Document doc = new Document(root);

      try {
        Serializer serializer = new Serializer(System.out, "ISO-8859-1");
        serializer.setIndent(4);
        serializer.setMaxLength(64);
        serializer.write(doc);  
      }
      catch (IOException ex) {
        System.err.println(ex); 
      }  
	
}
 
Example 7
Source File: XomCreateXML.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * 最簡單的例子
 */
@Test
public void test01() {
	Element root = new Element("root");
	root.appendChild("Hello World!");
	Document doc = new Document(root);
	String result = doc.toXML();
	System.out.println(result);
}
 
Example 8
Source File: SettingsManager.java    From ciscorouter with MIT License 5 votes vote down vote up
/**
 * Sets the username and password for authenticating to the application
 * @param _username the username to save
 * @param _password the password to save
 */
public void setAuth(String _username, String _password) {
    String hashedPassword;
    String hashedUsername;
    try {
        hashedPassword = PasswordHash.createHash(_password);
        hashedUsername = PasswordHash.createHash(_username);
        File settingsFile = new File(System.getProperty("user.dir") + "/settings/settings.xml");
        FileWriter fw = new FileWriter(settingsFile, false);

        Element root = new Element("Settings");

        Element user = new Element("Username");
        user.appendChild(hashedUsername);
        root.appendChild(user);

        Element pass = new Element("Password");
        pass.appendChild(hashedPassword);
        root.appendChild(pass);

        Document doc = new Document(root);
        fw.write(doc.toXML());
        fw.flush();
        fw.close();

    } catch (NoSuchAlgorithmException | InvalidKeySpecException | IOException ex) {
        Logger.getLogger(SettingsManager.class.getName()).log(Level.SEVERE, null, ex);
    }
    requiresAuth = true;
    username = _username;
    password = _password;
}
 
Example 9
Source File: XMLOutputRenderer.java    From ciscorouter with MIT License 5 votes vote down vote up
/**
 * Adds a host report to the end of the output buffer
 * @param hostReport The report to add to the output
 */
@Override
public void addHostReport(HostReport hostReport) {
    ArrayList<Rule> rules = hostReport.getMatchedRules();
    Element host = new Element("Host");

    Element ip = new Element("IP");
    ip.appendChild(String.valueOf(hostReport.getHost()));
    host.appendChild(ip);

    Element scanType = new Element("ScanType");
    scanType.appendChild("Full Scan");
    host.appendChild(scanType);

    Element eRules = new Element("Rules");
    for (Rule rule : rules) {
        Element eRule = new Element("Rule");
        Element ruleName = new Element("RuleName");
        Element severity = new Element("Severity");
        Element description = new Element("Description");

        ruleName.appendChild(rule.getName());
        severity.appendChild(rule.getSeverity());
        description.appendChild(rule.getDescription());

        eRule.appendChild(ruleName);
        eRule.appendChild(severity);
        eRule.appendChild(description);
        eRules.appendChild(eRule);
    }
    host.appendChild(eRules);
    hostsRoot.appendChild(host);
}
 
Example 10
Source File: XMLOutputRenderer.java    From ciscorouter with MIT License 5 votes vote down vote up
/**
 * Readies the output renderer
 * @param f the file to save to NOTE: This assumes _file has correct extension
 */
public XMLOutputRenderer(File f) {
    super(f); //File is now in protected class variable file
    root = new Element("FullReport");
    hostsRoot = new Element("Hosts");
    root.appendChild(hostsRoot);
}
 
Example 11
Source File: PermissionUtils.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static byte[] createPermissionsXml(Permission... permissions) {
    final Element permissionsElement = new Element("permissions");
    permissionsElement.setNamespaceURI("http://xmlns.jcp.org/xml/ns/javaee");
    permissionsElement.addAttribute(new Attribute("version", "7"));
    for (Permission permission : permissions) {
        final Element permissionElement = new Element("permission");

        final Element classNameElement = new Element("class-name");
        final Element nameElement = new Element("name");
        classNameElement.appendChild(permission.getClass().getName());
        nameElement.appendChild(permission.getName());
        permissionElement.appendChild(classNameElement);
        permissionElement.appendChild(nameElement);

        final String actions = permission.getActions();
        if (actions != null && ! actions.isEmpty()) {
            final Element actionsElement = new Element("actions");
            actionsElement.appendChild(actions);
            permissionElement.appendChild(actionsElement);
        }
        permissionsElement.appendChild(permissionElement);
    }
    Document document = new Document(permissionsElement);
    try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
        final NiceSerializer serializer = new NiceSerializer(stream);
        serializer.setIndent(4);
        serializer.setLineSeparator("\n");
        serializer.write(document);
        serializer.flush();
        return stream.toByteArray();
    } catch (IOException e) {
        throw new IllegalStateException("Generating permissions.xml failed", e);
    }
}
 
Example 12
Source File: XmlAuthUtil.java    From rest-client with Apache License 2.0 5 votes vote down vote up
static void populateUsernamePasswordElement(Element eParent, UsernamePasswordAuth auth) {
    if(StringUtil.isNotEmpty(auth.getUsername())) {
        Element eUsername = new Element("username");
        eUsername.appendChild(auth.getUsername());
        eParent.appendChild(eUsername);
    }
    
    if(auth.getPassword() != null && auth.getPassword().length > 0) {
        Element ePassword = new Element("password");
        ePassword.appendChild(Util.base64encode(new String(auth.getPassword())));
        eParent.appendChild(ePassword);
    }
}
 
Example 13
Source File: XmlAuthUtil.java    From rest-client with Apache License 2.0 5 votes vote down vote up
static Element getOAuth2BearerElement(OAuth2BearerAuth auth) {
    Element e = new Element("oauth2-bearer");
    
    if(StringUtil.isNotEmpty(auth.getOAuth2BearerToken())) {
        Element eToken = new Element("token");
        eToken.appendChild(auth.getOAuth2BearerToken());
        e.appendChild(eToken);
    }
    
    return e;
}
 
Example 14
Source File: XomCreateXML.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * 聲明Document Type
 */
@Test
public void test05() {
	BigInteger low = BigInteger.ONE;
	BigInteger high = BigInteger.ONE;

	Element root = new Element("Fibonacci_Numbers");
	for (int i = 1; i <= 10; i++) {
		Element fibonacci = new Element("fibonacci");
		fibonacci.appendChild(low.toString());
		Attribute index = new Attribute("index", String.valueOf(i));
		fibonacci.addAttribute(index);
		root.appendChild(fibonacci);

		BigInteger temp = high;
		high = high.add(low);
		low = temp;
	}
	Document doc = new Document(root);
	DocType doctype = new DocType("Fibonacci_Numbers", "fibonacci.dtd");
	doc.insertChild(doctype, 0);
	try {
		Serializer serializer = new Serializer(System.out, "ISO-8859-1");
		serializer.setIndent(4);
		serializer.setMaxLength(64);
		serializer.write(doc);
	} catch (IOException ex) {
		System.err.println(ex);
	}
}
 
Example 15
Source File: XMLCollectionUtil.java    From rest-client with Apache License 2.0 5 votes vote down vote up
public static void writeRequestCollectionXML(final List<Request> requests, final File f)
        throws IOException, XMLException {
    XmlPersistenceWrite xUtl = new XmlPersistenceWrite();
    
    Element eRoot = new Element("request-collection");
    eRoot.addAttribute(new Attribute("version", Versions.CURRENT));
    for(Request req: requests) {
        Element e = xUtl.getRequestElement(req);
        eRoot.appendChild(e);
    }
    Document doc = new Document(eRoot);
    xUtl.writeXML(doc, f);
}
 
Example 16
Source File: XOMTreeBuilder.java    From caja with Apache License 2.0 5 votes vote down vote up
@Override protected void insertFosterParentedChild(Element child,
        Element table, Element stackParent) throws SAXException {
    try {
        Node parent = table.getParent();
        if (parent != null) { // always an element if not null
            ((ParentNode)parent).insertChild(child, indexOfTable(table, stackParent));
            cachedTableIndex++;
        } else {
            stackParent.appendChild(child);
        }
    } catch (XMLException e) {
        fatal(e);
    }
}
 
Example 17
Source File: XOMTreeBuilder.java    From caja with Apache License 2.0 5 votes vote down vote up
@Override protected void insertFosterParentedCharacters(String text,
        Element table, Element stackParent) throws SAXException {
    try {
        Node child = nodeFactory.makeText(text);
        Node parent = table.getParent();
        if (parent != null) { // always an element if not null
            ((ParentNode)parent).insertChild(child, indexOfTable(table, stackParent));
            cachedTableIndex++;
        } else {
            stackParent.appendChild(child);
        }            
    } catch (XMLException e) {
        fatal(e);
    }
}
 
Example 18
Source File: XOMTreeBuilder.java    From caja with Apache License 2.0 5 votes vote down vote up
@Override
protected void appendElement(Element child,
        Element newParent) throws SAXException {
    try {
        child.detach();
        newParent.appendChild(child);
    } catch (XMLException e) {
        fatal(e);
    }
}
 
Example 19
Source File: XOMTreeBuilder.java    From caja with Apache License 2.0 5 votes vote down vote up
@Override
protected void appendComment(Element parent, String comment) throws SAXException {
    try {
        parent.appendChild(nodeFactory.makeComment(comment));
    } catch (XMLException e) {
        fatal(e);
    }
}
 
Example 20
Source File: XOMTreeBuilder.java    From caja with Apache License 2.0 5 votes vote down vote up
@Override
protected void appendChildrenToNewParent(Element oldParent,
        Element newParent) throws SAXException {
    try {
        Nodes children = oldParent.removeChildren();
        for (int i = 0; i < children.size(); i++) {
            newParent.appendChild(children.get(i));
        }
    } catch (XMLException e) {
        fatal(e);
    }
}