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

📄 xml.java

📁 使用smslib和GSP 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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.OutboundMessage;
import org.smslib.smsserver.InterfaceTypes;
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.SAXException;
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 org.smslib.smsserver.AInterface
{
	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)
		{
			logWarn("Can't parse " + string + " as ISO8601 date!");
			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;

	/* Used to store all processed files */
	private Map/* <OutboundMessage, File> */processedFiles;

	private File outBrokenDirectory;

	public Xml(String infId, Properties props, org.smslib.smsserver.SMSServer server, InterfaceTypes type)
	{
		super(infId, props, server, type);
		description = "Interface for xml input/output files";
		processedFiles = new HashMap/* <OutboundMessage, File> */();
		/* Read arguments */
		inDirectory = new File(getProperty("in") == null ? "." : getProperty("in"));
		outDirectory = new File(getProperty("out") == null ? "." : getProperty("out"));
		/* Check given arguments */
		if (isInbound())
		{
			if (!inDirectory.isDirectory() || !inDirectory.canWrite()) { throw new IllegalArgumentException(infId + ".in isn't a directory or isn't write-/readable!"); }
			try
			{
				writeInboundDTD(inDirectory);
			}
			catch (IOException e)
			{
				throw new IllegalArgumentException(e);
			}
		}
		if (isOutbound())
		{
			if (!outDirectory.isDirectory() || !outDirectory.canRead() || !outDirectory.canWrite()) { throw new IllegalArgumentException(infId + ".out isn't a directory or isn't write-/readable!"); }
			/* Check directory structure */
			outSentDirectory = new File(outDirectory, sOutSentDirectory);
			if (!outSentDirectory.isDirectory())
			{
				if (!outSentDirectory.mkdir()) { throw new IllegalArgumentException("Can't create directory '" + outSentDirectory); }
			}
			outFailedDirectory = new File(outDirectory, sOutFailedDirectory);
			if (!outFailedDirectory.isDirectory())
			{
				if (!outFailedDirectory.mkdir()) { throw new IllegalArgumentException("Can't create directory '" + outFailedDirectory); }
			}
			outBrokenDirectory = new File(outDirectory, sOutBrokenDirectory);
			if (!outBrokenDirectory.isDirectory())
			{
				if (!outBrokenDirectory.mkdir()) { throw new IllegalArgumentException("Can't create directory '" + outBrokenDirectory); }
			}
			try
			{
				writeOutboundDTD(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("id", m.getId());
		message.setAttribute("gateway_id", m.getGatewayId());
		/* Type */
		String type = null;
		if (m.getType() == org.smslib.MessageTypes.INBOUND)
		{
			type = "I";
		}
		else if (m.getType() == org.smslib.MessageTypes.STATUSREPORT)
		{
			type = "S";
		}
		if (type != null)
		{
			message.setAttributeNS(null, "type", type);
		}
		/* Encoding */
		String encoding = null;
		if (m.getEncoding() == org.smslib.MessageEncodings.ENC7BIT)
		{
			encoding = "7";
		}

⌨️ 快捷键说明

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