⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xml.java

📁 发送短信 接收短信 多种接口com/net/modem 开发库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// SMSLib for Java v3
// A Java API library for sending and receiving SMS via a GSM modem
// or other supported gateways.
// Web Site: http://www.smslib.org
//
// Copyright (C) 2002-2008, Thanasis Delenikas, Athens/GREECE.
// SMSLib is distributed under the terms of the Apache License version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.smslib.smsserver.interfaces;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.smslib.InboundMessage;
import org.smslib.OutboundMessage;
import org.smslib.Message.MessageEncodings;
import org.smslib.Message.MessageTypes;
import org.smslib.smsserver.SMSServer;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXParseException;

/**
 * This interface uses xml-files to read outgoing messages and write inbound
 * messages.<br />
 * Every file contains neither <u>ONE</u> inbound <b>or</b> <u>ONE</u>
 * outbound message.
 * <hr />
 * The DTDs for the xml files containing a inbound message:<br />
 * 
 * <pre>
 * &lt;!ELEMENT message (originator, text, receive_date)&gt;
 * &lt;!ATTLIST message
 *   id		ID	#REQUIRED
 *   gateway_id	CDATA	#REQUIRED
 *   type		CDATA	#IMPLIED
 *   encoding	CDATA	#IMPLIED &gt;
 * &lt;!ELEMENT originator (#PCDATA)&gt;
 * &lt;!ELEMENT text (#PCDATA)&gt;
 * &lt;!ELEMENT receive_date (#PCDATA)&gt; 
 * </pre>
 * 
 * <hr />
 * The DTDs for the xml files containing a outgoing message:<br />
 * 
 * <pre>
 * &lt;!ELEMENT message (recipient, text, originator, create_date?)&gt;
 * &lt;!ATTLIST message 
 *    id	 	 ID      #REQUIRED
 *    gateway_id	 CDATA	#IMPLIED
 *    status         CDATA  &quot;U&quot; 
 *    encoding       CDATA	&quot;7&quot;
 *    priority       CDATA	&quot;N&quot;
 *    ref_no         CDATA	#IMPLIED
 *    status_report  CDATA	#IMPLIED
 *    flash_sms      CDATA	#IMPLIED
 *    src_port       CDATA	#IMPLIED
 *    dst_port       CDATA	#IMPLIED &gt; 
 * &lt;!ELEMENT recipient (#PCDATA)&gt;
 * &lt;!ELEMENT text (#PCDATA)&gt;
 * &lt;!ELEMENT create_date (#PCDATA)&gt;
 * &lt;!ELEMENT originator (#PCDATA)&gt;
 * </pre>
 * 
 * @author Sebastian Just
 */
public class Xml extends Interface<File>
{
	public static final String sOutSentDirectory = "sent";

	public static final String sOutFailedDirectory = "failed";

	public static final String sOutBrokenDirectory = "broken";

	private static final SimpleDateFormat fileSdf = new SimpleDateFormat("yyyyMMddHHmmss-S");

	private static final SimpleDateFormat iso8601Sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

	/**
	 * Formats a string ISO8601 compliant.
	 * 
	 * @param date
	 *            the date to format
	 * @return a string with a ISO8601 compliant date
	 */
	protected String getDateAsISO8601(Date date)
	{
		String result = iso8601Sdf.format(date);
		StringBuilder sb = new StringBuilder(result.length() + 1);
		sb.append(result.substring(0, result.length() - 2));
		sb.append(":");
		sb.append(result.substring(result.length() - 2));
		return sb.toString();
	}

	/**
	 * Creates a date from a ISO8601 string
	 * 
	 * @param string
	 *            The string to parse
	 * @return A date
	 */
	protected Date getISO8601AsDate(String string)
	{
		StringBuilder sb = new StringBuilder(string);
		sb.replace(string.length() - 3, string.length() - 2, "");
		try
		{
			return iso8601Sdf.parse(sb.toString());
		}
		catch (ParseException e)
		{
			getService().getLogger().logWarn("Can't parse " + string + " as ISO8601 date!", null, null);
			return null;
		}
	}

	/* The used directory which contains all inbound messages */
	private File inDirectory;

	/* The used directory which contains all outbound messages */
	private File outDirectory;

	/* The used directory which contains all failed outbound messages */
	private File outFailedDirectory;

	/* The used directory which contains all sent outbound messages */
	private File outSentDirectory;

	private File outBrokenDirectory;

	public Xml(String myInterfaceId, Properties myProps, SMSServer myServer, InterfaceTypes myType)
	{
		super(myInterfaceId, myProps, myServer, myType);
		setDescription("Interface for xml input/output files");
		/* Read arguments */
		this.inDirectory = new File(getProperty("in") == null ? "." : getProperty("in"));
		this.outDirectory = new File(getProperty("out") == null ? "." : getProperty("out"));
		/* Check given arguments */
		if (isInbound())
		{
			if (!this.inDirectory.isDirectory() || !this.inDirectory.canWrite()) { throw new IllegalArgumentException(myInterfaceId + ".in isn't a directory or isn't write-/readable!"); }
			try
			{
				writeInboundDTD(this.inDirectory);
			}
			catch (IOException e)
			{
				throw new IllegalArgumentException(e);
			}
		}
		if (isOutbound())
		{
			if (!this.outDirectory.isDirectory() || !this.outDirectory.canRead() || !this.outDirectory.canWrite()) { throw new IllegalArgumentException(myInterfaceId + ".out isn't a directory or isn't write-/readable!"); }
			/* Check directory structure */
			this.outSentDirectory = new File(this.outDirectory, sOutSentDirectory);
			if (!this.outSentDirectory.isDirectory())
			{
				if (!this.outSentDirectory.mkdir()) { throw new IllegalArgumentException("Can't create directory '" + this.outSentDirectory); }
			}
			this.outFailedDirectory = new File(this.outDirectory, sOutFailedDirectory);
			if (!this.outFailedDirectory.isDirectory())
			{
				if (!this.outFailedDirectory.mkdir()) { throw new IllegalArgumentException("Can't create directory '" + this.outFailedDirectory); }
			}
			this.outBrokenDirectory = new File(this.outDirectory, sOutBrokenDirectory);
			if (!this.outBrokenDirectory.isDirectory())
			{
				if (!this.outBrokenDirectory.mkdir()) { throw new IllegalArgumentException("Can't create directory '" + this.outBrokenDirectory); }
			}
			try
			{
				writeOutboundDTD(this.outDirectory);
			}
			catch (IOException e)
			{
				throw new IllegalArgumentException(e);
			}
		}
	}

	private void writeInboundDTD(File in) throws IOException
	{
		File dtd = new File(in, "smssvr_in.dtd");
		if (!dtd.exists())
		{
			Writer w = new BufferedWriter(new FileWriter(dtd));
			String CRLF = System.getProperty("line.separator");
			w.write(" <!ELEMENT message (originator, text, receive_date)>");
			w.write(CRLF);
			w.write("   <!ATTLIST message");
			w.write(CRLF);
			w.write("       id		ID	#REQUIRED");
			w.write(CRLF);
			w.write("       gateway_id	CDATA	#REQUIRED");
			w.write(CRLF);
			w.write("       type		CDATA	#IMPLIED");
			w.write(CRLF);
			w.write("       encoding	CDATA	#IMPLIED >");
			w.write(CRLF);
			w.write("     <!ELEMENT originator (#PCDATA)>");
			w.write(CRLF);
			w.write("     <!ELEMENT text (#PCDATA)>");
			w.write(CRLF);
			w.write("     <!ELEMENT receive_date (#PCDATA)>");
			w.write(CRLF);
			w.flush();
			w.close();
		}
	}

	private void writeOutboundDTD(File out) throws IOException
	{
		File dtd = new File(out, "smssvr_out.dtd");
		if (!dtd.exists())
		{
			Writer w = new BufferedWriter(new FileWriter(dtd));
			String CRLF = System.getProperty("line.separator");
			w.write(" <!ELEMENT message (recipient, text, originator, create_date?)>");
			w.write(CRLF);
			w.write("   <!ATTLIST message ");
			w.write(CRLF);
			w.write("      id	 	 ID      #REQUIRED");
			w.write(CRLF);
			w.write("      gateway_id	 CDATA	#IMPLIED");
			w.write(CRLF);
			w.write("      status         CDATA  \"U\" ");
			w.write(CRLF);
			w.write("      encoding       CDATA	\"7\"");
			w.write(CRLF);
			w.write("      priority       CDATA	\"N\"");
			w.write(CRLF);
			w.write("      ref_no         CDATA	#IMPLIED");
			w.write(CRLF);
			w.write("      status_report  CDATA	#IMPLIED");
			w.write(CRLF);
			w.write("      flash_sms      CDATA	#IMPLIED");
			w.write(CRLF);
			w.write("      src_port       CDATA	#IMPLIED");
			w.write(CRLF);
			w.write("      dst_port       CDATA	#IMPLIED> ");
			w.write(CRLF);
			w.write("   <!ELEMENT recipient (#PCDATA)>");
			w.write(CRLF);
			w.write("   <!ELEMENT text (#PCDATA)>");
			w.write(CRLF);
			w.write("   <!ELEMENT create_date (#PCDATA)>");
			w.write(CRLF);
			w.write("   <!ELEMENT originator (#PCDATA)>");
			w.write(CRLF);
			w.flush();
			w.close();
		}
	}

	/**
	 * Adds the given InboundMessage to the given document.
	 * 
	 * @param xmldoc
	 *            The document in which the message is written
	 * @param m
	 *            The message to add to the docment
	 */
	private void addMessageToDocument(Document xmldoc, org.smslib.InboundMessage m)
	{
		Element message = null;
		Element originatorElement = null;
		Node originatorNode = null;
		Element textElement = null;
		Node textNode = null;
		Element timeElement = null;
		Node timeNode = null;
		message = xmldoc.createElement("message");
		message.setAttribute("gateway_id", m.getGatewayId());
		/* Type */
		String msgType = null;
		switch (m.getType())
		{
			case INBOUND:
				msgType = "I";
				break;
			case STATUSREPORT:
				msgType = "S";
				break;
			case OUTBOUND:
				msgType = "O";
				break;
			case UNKNOWN:
				msgType = "U";
				break;
			case WAPSI:
				msgType = "W";
				break;
		}
		if (msgType != null)
		{
			message.setAttributeNS(null, "type", msgType);
		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -